switch codebase to bot-api-2.0 and minor improvements

This commit is contained in:
Jannes Höke 2016-04-19 00:45:27 +02:00
parent b17dcf5cdc
commit 28863876da

224
bot.py
View file

@ -2,7 +2,10 @@ import logging
from datetime import datetime from datetime import datetime
from random import randint from random import randint
from telegram import Updater, InlineQueryResultArticle, ParseMode, Message, Chat from telegram import InlineQueryResultArticle, ParseMode, Message, Chat, \
Emoji, InputTextMessageContent
from telegram.ext import Updater, InlineQueryHandler, \
ChosenInlineResultHandler, CommandHandler
from telegram.utils.botan import Botan from telegram.utils.botan import Botan
from game_manager import GameManager from game_manager import GameManager
@ -67,13 +70,13 @@ def display_name(game):
def display_color(color): def display_color(color):
""" Convert a color code to actual color name """ """ Convert a color code to actual color name """
if color == "r": if color == "r":
return "Red" return Emoji.HEAVY_BLACK_HEART + " Red"
if color == "b": if color == "b":
return "Blue" return Emoji.BLUE_HEART + " Blue"
if color == "g": if color == "g":
return "Green" return Emoji.GREEN_HEART + " Green"
if color == "y": if color == "y":
return "Yellow" return Emoji.YELLOW_HEART + " Yellow"
def error(bot, update, error): def error(bot, update, error):
@ -158,14 +161,6 @@ def start_game(bot, update):
help(bot, update) help(bot, update)
def inline(bot, update):
""" Handler for all inline stuff """
if update.inline_query:
reply_to_query(bot, update)
else:
process_result(bot, update)
def help(bot, update): def help(bot, update):
""" Handler for the /help command """ """ Handler for the /help command """
bot.sendMessage(update.message.chat_id, bot.sendMessage(update.message.chat_id,
@ -214,9 +209,7 @@ def reply_to_query(bot, update):
if game.last_card.special == c.DRAW_FOUR and game.draw_counter: if game.last_card.special == c.DRAW_FOUR and game.draw_counter:
add_call_bluff(results) add_call_bluff(results)
add_other_cards(playable, player, results) add_other_cards(playable, player, results, game)
add_gameinfo(game, results)
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0) bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
@ -227,98 +220,17 @@ def add_choose_color(results):
InlineQueryResultArticle( InlineQueryResultArticle(
id=color, id=color,
title="Choose Color", title="Choose Color",
message_text=display_color(color), description=display_color(color),
description=display_color(color) input_message_content=
InputTextMessageContent(display_color(color))
) )
) )
def add_other_cards(playable, player, results): def add_other_cards(playable, player, results, game):
if not playable: if not playable:
playable = list() playable = list()
results.append(
InlineQueryResultArticle(
"hand",
title="Other cards:",
description=', '.join([repr(card) for card in
list_subtract(player.cards, playable)]),
message_text='Just checking cards'
)
)
def add_no_game(results):
results.append(
InlineQueryResultArticle(
"nogame",
title="You are not playing",
message_text='Not playing right now. Use /new to start a game or '
'/join to join the current game in this group'
)
)
def add_not_started(results):
results.append(
InlineQueryResultArticle(
"nogame",
title="The game wasn't started yet",
message_text='Start the game with /start'
)
)
def add_draw(player, results, could_play_card):
results.append(
InlineQueryResultArticle(
"draw",
title=("No suitable cards..." if not could_play_card else
"I don't want to play a card..."),
description="Draw!",
message_text='Drawing %d card(s)'
% (player.game.draw_counter or 1)
)
)
def add_pass(results):
results.append(
InlineQueryResultArticle(
"pass",
title="Pass",
description="Don't play a card",
message_text='Pass'
)
)
def add_call_bluff(results):
results.append(
InlineQueryResultArticle(
"call_bluff",
title="Call their bluff!",
description="Risk it!",
message_text="I'm calling your bluff!"
)
)
def add_play_card(card, results):
results.append(
InlineQueryResultArticle(str(card),
title="Play card",
message_text=
('<a href="%s">\xad</a>'
'Played card ' + repr(card))
% card.get_image_link(),
thumb_url=card.get_thumb_link(),
description=repr(card),
parse_mode=ParseMode.HTML)
)
def add_gameinfo(game, results):
players = list() players = list()
current_player = game.current_player current_player = game.current_player
itplayer = current_player.next itplayer = current_player.next
@ -330,17 +242,94 @@ def add_gameinfo(game, results):
results.append( results.append(
InlineQueryResultArticle( InlineQueryResultArticle(
"gameinfo", "hand",
title="Show game info", title="Not playable (tap for game state):",
description="Tap to see the current player, player order, " description=', '.join([repr(card) for card in
"card amounts and last played card", list_subtract(player.cards, playable)]),
message_text="Current player: " + display_name(game) + "\n" + input_message_content=InputTextMessageContent(
"Current player: " + display_name(game) + "\n" +
"Last card: " + repr(game.last_card) + "\n" + "Last card: " + repr(game.last_card) + "\n" +
"Players: " + " -> ".join(players) "Players: " + " -> ".join(players))
) )
) )
def add_no_game(results):
results.append(
InlineQueryResultArticle(
"nogame",
title="You are not playing",
input_message_content=
InputTextMessageContent('Not playing right now. Use /new to start '
'a game or /join to join the current game '
'in this group')
)
)
def add_not_started(results):
results.append(
InlineQueryResultArticle(
"nogame",
title="The game wasn't started yet",
input_message_content=
InputTextMessageContent('Start the game with /start')
)
)
def add_draw(player, results, could_play_card):
results.append(
InlineQueryResultArticle(
"draw",
title=("No suitable cards..." if not could_play_card else
"I don't want to play a card..."),
description="Draw!",
input_message_content=
InputTextMessageContent('Drawing %d card(s)'
% (player.game.draw_counter or 1))
)
)
def add_pass(results):
results.append(
InlineQueryResultArticle(
"pass",
title="Pass",
description="Don't play a card",
input_message_content=InputTextMessageContent('Pass')
)
)
def add_call_bluff(results):
results.append(
InlineQueryResultArticle(
"call_bluff",
title="Call their bluff!",
description="Risk it!",
input_message_content=
InputTextMessageContent("I'm calling your bluff!")
)
)
def add_play_card(card, results):
results.append(
InlineQueryResultArticle(str(card),
title="Play card",
thumb_url=card.get_thumb_link(),
description=repr(card),
input_message_content=
InputTextMessageContent(
('<a href="%s">\xad</a>'
'Played card ' + repr(card))
% card.get_image_link(),
parse_mode=ParseMode.HTML))
)
def add_player(itplayer, players): def add_player(itplayer, players):
players.append(itplayer.user.first_name + " (%d cards)" players.append(itplayer.user.first_name + " (%d cards)"
% len(itplayer.cards)) % len(itplayer.cards))
@ -348,11 +337,15 @@ def add_player(itplayer, players):
def process_result(bot, update): def process_result(bot, update):
""" Check the players actions and act accordingly """ """ Check the players actions and act accordingly """
try:
user = update.chosen_inline_result.from_user user = update.chosen_inline_result.from_user
game = gm.userid_game[user.id] game = gm.userid_game[user.id]
player = gm.userid_player[user.id] player = gm.userid_player[user.id]
result_id = update.chosen_inline_result.result_id result_id = update.chosen_inline_result.result_id
chat_id = gm.chatid_game[game] chat_id = gm.chatid_game[game]
except KeyError:
return
logger.debug("Selected result: " + result_id) logger.debug("Selected result: " + result_id)
if result_id in ('hand', 'gameinfo', 'nogame'): if result_id in ('hand', 'gameinfo', 'nogame'):
@ -368,7 +361,7 @@ def process_result(bot, update):
else: else:
do_play_card(bot, chat_id, game, player, result_id, user) do_play_card(bot, chat_id, game, player, result_id, user)
if game.current_player is not game.current_player.next: if game.current_player.next:
bot.sendMessage(chat_id, text="Next player: " + display_name(game)) bot.sendMessage(chat_id, text="Next player: " + display_name(game))
@ -425,13 +418,14 @@ def do_call_bluff(bot, chat_id, game, player):
# Add all handlers to the dispatcher and run the bot # Add all handlers to the dispatcher and run the bot
dp.addTelegramInlineHandler(inline) dp.addHandler(InlineQueryHandler(reply_to_query))
dp.addTelegramCommandHandler('start', start_game) dp.addHandler(ChosenInlineResultHandler(process_result))
dp.addTelegramCommandHandler('new', new_game) dp.addHandler(CommandHandler('start', start_game))
dp.addTelegramCommandHandler('join', join_game) dp.addHandler(CommandHandler('new', new_game))
dp.addTelegramCommandHandler('leave', leave_game) dp.addHandler(CommandHandler('join', join_game))
dp.addTelegramCommandHandler('help', help) dp.addHandler(CommandHandler('leave', leave_game))
dp.addTelegramCommandHandler('news', news) dp.addHandler(CommandHandler('help', help))
dp.addHandler(CommandHandler('news', news))
dp.addErrorHandler(error) dp.addErrorHandler(error)
start_bot(u) start_bot(u)