You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

magic-judge-telegram-bot.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import logging
  2. import json
  3. import oracle
  4. import documents
  5. from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler, MessageHandler, Filters
  6. from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup
  7. #logging.basicConfig(level=logging.DEBUG,
  8. # format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  9. def format_card(card):
  10. mana = ''
  11. if 'manaCost' in card:
  12. mana = '\t' + card['manaCost']
  13. text = ''
  14. if 'text' in card:
  15. text = '\n' + card['text']
  16. footer = ''
  17. if "Creature" in card['type']:
  18. footer = '\n{}/{}'.format(card['power'], card['toughness'])
  19. if "Planeswalker" in card['type'] and 'loyalty' in card:
  20. footer = '\n{}'.format(card['loyalty'])
  21. return '<b>{}</b>{}\n<i>{}</i>{}{}'.format(
  22. card['name'],
  23. mana,
  24. card['type'],
  25. text,
  26. footer)
  27. def preview_card(card):
  28. mana = ''
  29. if 'manaCost' in card:
  30. mana = '\t' + card['manaCost']
  31. return '{}{}\n{}'.format(
  32. card['name'],
  33. mana,
  34. card['type'])
  35. def start_command(bot, update):
  36. commands = [
  37. '/o <card name or search strings> - oracle text for a card',
  38. '/q <question> - oracle text for cards mentioned in the question',
  39. '/cr <section> (coming soon)',
  40. '/ipg <section> (coming soon)',
  41. '/mtr <section> (coming soon)',
  42. ]
  43. update.message.reply_text('How can I help?\n{}'.format('\n'.join(commands)), quote = False)
  44. def oracle_command(bot, update, args):
  45. if not args:
  46. update.message.reply_text('I need some clues to search for, my master!', quote=False)
  47. return
  48. words = [word.casefold() for word in args]
  49. nameCandidates = oracle.get_matching_names(words)
  50. if not nameCandidates:
  51. update.message.reply_text('I searched very thoroughly, but returned empty-handed, my master!', quote=False)
  52. return
  53. if len(nameCandidates) > 20:
  54. update.message.reply_text('I need more specific clues, my master! This would return {} names'.format(len(nameCandidates)), quote=False)
  55. return
  56. if len(nameCandidates) > 1:
  57. reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton(name, callback_data=name)] for name in nameCandidates])
  58. update.message.reply_text('Which one?', reply_markup=reply_markup, quote=False)
  59. return
  60. reply = []
  61. for name in nameCandidates:
  62. for oracleName in oracle.get_oracle_names(name):
  63. reply.append(format_card(oracle.get_card(oracleName)))
  64. update.message.reply_text('\n'.join(reply), parse_mode='HTML', quote = False)
  65. def question_command(bot, update, args):
  66. text = ' '.join(args).casefold()
  67. names = oracle.get_names_in_text(text)
  68. reply = []
  69. for name in names:
  70. reply.append('"' + name + '":\n' + '\n'.join([format_card(oracle.get_card(oracleName)) for oracleName in oracle.get_oracle_names(name)]))
  71. if reply:
  72. update.message.reply_text('\n\n'.join(reply), parse_mode='HTML', quote = False)
  73. def inline_oracle(bot, update):
  74. query = update.inline_query.query.casefold()
  75. if not query:
  76. return
  77. if len(query) < 3:
  78. return
  79. words = query.split()
  80. nameCandidates = oracle.get_matching_names(words)
  81. if not nameCandidates:
  82. return
  83. results = list()
  84. for word in nameCandidates[:3]:
  85. for oracleName in oracle.get_oracle_names(word):
  86. card = oracle.get_card(oracleName)
  87. results.append(
  88. InlineQueryResultArticle(
  89. id=card['name'],
  90. title=word,
  91. description=preview_card(card),
  92. input_message_content=InputTextMessageContent(format_card(card), parse_mode='HTML')
  93. )
  94. )
  95. bot.answerInlineQuery(update.inline_query.id, results)
  96. def callback_name(bot, update):
  97. message_id = update.callback_query.message.message_id
  98. chat_id = update.callback_query.message.chat.id
  99. name = update.callback_query.data
  100. names = oracle.get_oracle_names(name)
  101. if not names:
  102. bot.answerCallbackQuery(update.callback_query.id)
  103. return
  104. bot.editMessageText(
  105. chat_id = chat_id,
  106. message_id = message_id,
  107. parse_mode = 'HTML',
  108. text = '\n'.join([format_card(oracle.get_card(oracleName)) for oracleName in names])
  109. )
  110. bot.answerCallbackQuery(update.callback_query.id)
  111. def text(bot, update):
  112. if update.message.chat.type != 'private':
  113. return
  114. text = update.message.text
  115. if len(text) < 30:
  116. oracle_command(bot, update, text.split())
  117. else:
  118. question(bot, update, text)
  119. def comp_rules_command(bot, update, args):
  120. update.message.reply_text(documents.cr_search(args), parse_mode='HTML', quote = False)
  121. def ask_command(bot, update, args):
  122. pass
  123. def dispatcher_setup(dispatcher):
  124. dispatcher.add_handler(CommandHandler('start', start_command))
  125. dispatcher.add_handler(CommandHandler('help', start_command))
  126. dispatcher.add_handler(CommandHandler('o', oracle_command, pass_args=True))
  127. dispatcher.add_handler(CommandHandler('q', question_command, pass_args=True))
  128. dispatcher.add_handler(CommandHandler('cr', comp_rules_command, pass_args=True))
  129. dispatcher.add_handler(CommandHandler('ask', ask_command, pass_args=True))
  130. dispatcher.add_handler(InlineQueryHandler(inline_oracle))
  131. dispatcher.add_handler(CallbackQueryHandler(callback_name))
  132. dispatcher.add_handler(MessageHandler(Filters.text, text))
  133. with open('config.json') as file:
  134. config = json.load(file)
  135. updater = Updater(config['token'])
  136. dispatcher_setup(updater.dispatcher)
  137. updater.start_polling()
  138. updater.idle()