Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

magic-judge-telegram-bot.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. # TODO: if len(name) < 64 is a quickfix for /o show, which fails to send correct callback data for un... card
  58. reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton(name, callback_data=name)] for name in nameCandidates if len(name) < 64])
  59. update.message.reply_text('Which one?', reply_markup=reply_markup, quote=False)
  60. return
  61. reply = []
  62. for name in nameCandidates:
  63. for oracleName in oracle.get_oracle_names(name):
  64. reply.append(format_card(oracle.get_card(oracleName)))
  65. update.message.reply_text('\n'.join(reply), parse_mode='HTML', quote = False)
  66. def question_command(bot, update, args):
  67. text = ' '.join(args).casefold()
  68. names = oracle.get_names_in_text(text)
  69. reply = []
  70. for name in names:
  71. reply.append('"' + name + '":\n' + '\n'.join([format_card(oracle.get_card(oracleName)) for oracleName in oracle.get_oracle_names(name)]))
  72. if reply:
  73. update.message.reply_text('\n\n'.join(reply), parse_mode='HTML', quote = False)
  74. def inline_oracle(bot, update):
  75. query = update.inline_query.query.casefold()
  76. if not query:
  77. return
  78. if len(query) < 3:
  79. return
  80. words = query.split()
  81. nameCandidates = oracle.get_matching_names(words)
  82. if not nameCandidates:
  83. return
  84. results = list()
  85. for word in nameCandidates[:3]:
  86. for oracleName in oracle.get_oracle_names(word):
  87. card = oracle.get_card(oracleName)
  88. results.append(
  89. InlineQueryResultArticle(
  90. id=card['name'],
  91. title=word,
  92. description=preview_card(card),
  93. input_message_content=InputTextMessageContent(format_card(card), parse_mode='HTML')
  94. )
  95. )
  96. bot.answerInlineQuery(update.inline_query.id, results)
  97. def callback_name(bot, update):
  98. message_id = update.callback_query.message.message_id
  99. chat_id = update.callback_query.message.chat.id
  100. name = update.callback_query.data
  101. names = oracle.get_oracle_names(name)
  102. if not names:
  103. bot.answerCallbackQuery(update.callback_query.id)
  104. return
  105. bot.editMessageText(
  106. chat_id = chat_id,
  107. message_id = message_id,
  108. parse_mode = 'HTML',
  109. text = '\n'.join([format_card(oracle.get_card(oracleName)) for oracleName in names])
  110. )
  111. bot.answerCallbackQuery(update.callback_query.id)
  112. def text(bot, update):
  113. if update.message.chat.type != 'private':
  114. return
  115. text = update.message.text
  116. if len(text) < 30:
  117. oracle_command(bot, update, text.split())
  118. else:
  119. question(bot, update, text)
  120. def comp_rules_command(bot, update, args):
  121. update.message.reply_text(documents.cr_search(args), parse_mode='HTML', quote = False)
  122. def ask_command(bot, update, args):
  123. pass
  124. def dispatcher_setup(dispatcher):
  125. dispatcher.add_handler(CommandHandler('start', start_command))
  126. dispatcher.add_handler(CommandHandler('help', start_command))
  127. dispatcher.add_handler(CommandHandler('o', oracle_command, pass_args=True))
  128. dispatcher.add_handler(CommandHandler('q', question_command, pass_args=True))
  129. dispatcher.add_handler(CommandHandler('cr', comp_rules_command, pass_args=True))
  130. dispatcher.add_handler(CommandHandler('ask', ask_command, pass_args=True))
  131. dispatcher.add_handler(InlineQueryHandler(inline_oracle))
  132. dispatcher.add_handler(CallbackQueryHandler(callback_name))
  133. dispatcher.add_handler(MessageHandler(Filters.text, text))
  134. with open('config.json') as file:
  135. config = json.load(file)
  136. updater = Updater(config['token'])
  137. dispatcher_setup(updater.dispatcher)
  138. updater.start_polling()
  139. updater.idle()