Browse Source

Card parts are now marked clearly, all text messages are processed in private chats

master
Mikhail Chernov 8 years ago
parent
commit
bde826857a
1 changed files with 22 additions and 13 deletions
  1. 22
    13
      magic-judge-telegram-bot.py

+ 22
- 13
magic-judge-telegram-bot.py View File

import logging import logging
import json import json
from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler
from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler, MessageHandler, Filters
from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup


#logging.basicConfig(level=logging.DEBUG, #logging.basicConfig(level=logging.DEBUG,
footer = '\n{}/{}'.format(card['power'], card['toughness']) footer = '\n{}/{}'.format(card['power'], card['toughness'])
if "Planeswalker" in card['type'] and 'loyalty' in card: if "Planeswalker" in card['type'] and 'loyalty' in card:
footer = '\n{}'.format(card['loyalty']) footer = '\n{}'.format(card['loyalty'])
return '{}{}\n{}{}{}'.format(
return '<b>{}</b>{}\n<i>{}</i>{}{}'.format(
card['name'], card['name'],
mana, mana,
card['type'], card['type'],
'/ipg <section> (coming soon)', '/ipg <section> (coming soon)',
'/mtr <section> (coming soon)', '/mtr <section> (coming soon)',
] ]
update.message.reply_text('How can I help?\n{}'.format('\n'.join(commands)))
update.message.reply_text('How can I help?\n{}'.format('\n'.join(commands)), quote = False)


def search_names(words): def search_names(words):
nameCandidates = [name for name in namesToSearch if all(word in name.casefold() for word in words)] nameCandidates = [name for name in namesToSearch if all(word in name.casefold() for word in words)]


def oracle(bot, update, args): def oracle(bot, update, args):
if not args: if not args:
update.message.reply_text('I need some clues to search for, my master!')
update.message.reply_text('I need some clues to search for, my master!', quote=False)
return return
words = [word.casefold() for word in args] words = [word.casefold() for word in args]


nameCandidates = search_names(words) nameCandidates = search_names(words)


if not nameCandidates: if not nameCandidates:
update.message.reply_text('I searched very thoroughly, but returned empty-handed, my master!')
update.message.reply_text('I searched very thoroughly, but returned empty-handed, my master!', quote=False)
return return


if len(nameCandidates) > 20: if len(nameCandidates) > 20:
update.message.reply_text('I need more specific clues, my master! This would return {} names'.format(len(nameCandidates)))
update.message.reply_text('I need more specific clues, my master! This would return {} names'.format(len(nameCandidates)), quote=False)
return return


if len(nameCandidates) > 1: if len(nameCandidates) > 1:
reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton(name, callback_data=name)] for name in nameCandidates]) reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton(name, callback_data=name)] for name in nameCandidates])
update.message.reply_text('Which one?', reply_markup=reply_markup)
update.message.reply_text('Which one?', reply_markup=reply_markup, quote=False)
return return


reply = [] reply = []
for name in nameCandidates: for name in nameCandidates:
for uniqueName in names[name]: for uniqueName in names[name]:
reply.append(format_card(oracleData[uniqueName])) reply.append(format_card(oracleData[uniqueName]))
update.message.reply_text('\n'.join(reply))
update.message.reply_text('\n'.join(reply), parse_mode='HTML', quote = False)


def question(bot, update, args): def question(bot, update, args):
words = args words = args
reply = [] reply = []
for name in namesToSearch: for name in namesToSearch:
if name.casefold() in text: if name.casefold() in text:
reply.append('"' + name + '":')
for uniqueName in names[name]:
reply.append(format_card(oracleData[uniqueName]))
reply.append('"' + name + '":\n' + '\n'.join([format_card(oracleData[uniqueName]) for uniqueName in names[name]]))
if reply: if reply:
update.message.reply_text('\n'.join(reply))
update.message.reply_text('\n\n'.join(reply), parse_mode='HTML', quote = False)


def inline_oracle(bot, update): def inline_oracle(bot, update):
query = update.inline_query.query.casefold() query = update.inline_query.query.casefold()
id=oracleData[uniqueName]['name'], id=oracleData[uniqueName]['name'],
title=word, title=word,
description=preview_card(oracleData[uniqueName]), description=preview_card(oracleData[uniqueName]),
input_message_content=InputTextMessageContent(format_card(oracleData[uniqueName]))
input_message_content=InputTextMessageContent(format_card(oracleData[uniqueName]), parse_mode='HTML')
) )
) )
bot.answerInlineQuery(update.inline_query.id, results) bot.answerInlineQuery(update.inline_query.id, results)
bot.editMessageText( bot.editMessageText(
chat_id = chat_id, chat_id = chat_id,
message_id = message_id, message_id = message_id,
parse_mode = 'HTML',
text = '\n'.join([format_card(oracleData[uniqueName]) for uniqueName in names[name]]) text = '\n'.join([format_card(oracleData[uniqueName]) for uniqueName in names[name]])
) )
bot.answerCallbackQuery(update.callback_query.id) bot.answerCallbackQuery(update.callback_query.id)


def text(bot, update):
if update.message.chat.type != 'private':
return
text = update.message.text
if len(text) < 30:
oracle(bot, update, text.split())
else:
question(bot, update, text)

def dispatcher_setup(dispatcher): def dispatcher_setup(dispatcher):
dispatcher.add_handler(CommandHandler('start', start)) dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('help', start)) dispatcher.add_handler(CommandHandler('help', start))
dispatcher.add_handler(CommandHandler('q', question, pass_args=True)) dispatcher.add_handler(CommandHandler('q', question, pass_args=True))
dispatcher.add_handler(InlineQueryHandler(inline_oracle)) dispatcher.add_handler(InlineQueryHandler(inline_oracle))
dispatcher.add_handler(CallbackQueryHandler(callback_name)) dispatcher.add_handler(CallbackQueryHandler(callback_name))
dispatcher.add_handler(MessageHandler(Filters.text, text))


with open('token') as file: with open('token') as file:
token = file.read().strip() token = file.read().strip()

Loading…
Cancel
Save