Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

magic-judge-telegram-bot.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import logging
  2. import json
  3. from telegram.ext import Updater, CommandHandler, InlineQueryHandler
  4. from telegram import InlineQueryResultArticle, InputTextMessageContent
  5. #logging.basicConfig(level=logging.DEBUG,
  6. # format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  7. names = {}
  8. with open('data/names.json') as file:
  9. names = json.load(file)
  10. namesToSearch = names.keys()
  11. oracleData = {}
  12. with open('data/oracle.json') as file:
  13. oracleData = json.load(file)
  14. print('Registered {} card names and {} oracle entries'.format(len(names), len(oracleData)))
  15. def format_card(card):
  16. mana = ''
  17. if 'manaCost' in card:
  18. mana = '\t' + card['manaCost']
  19. text = ''
  20. if 'text' in card:
  21. text = '\n' + card['text']
  22. footer = ''
  23. if "Creature" in card['type']:
  24. footer = '\n{}/{}'.format(card['power'], card['toughness'])
  25. if "Planeswalker" in card['type'] and 'loyalty' in card:
  26. footer = '\n{}'.format(card['loyalty'])
  27. return '{}{}\n{}{}{}'.format(
  28. card['name'],
  29. mana,
  30. card['type'],
  31. text,
  32. footer)
  33. def preview_card(card):
  34. mana = ''
  35. if 'manaCost' in card:
  36. mana = '\t' + card['manaCost']
  37. return '{}{}\n{}'.format(
  38. card['name'],
  39. mana,
  40. card['type'])
  41. def format_names(names):
  42. return '\n'.join(sorted(names))
  43. def start(bot, update):
  44. commands = [
  45. '/o <card name or search strings> - oracle text for a card',
  46. '/q <question> - oracle text for cards mentioned in the question',
  47. '/cr <section> (coming soon)',
  48. '/ipg <section> (coming soon)',
  49. '/mtr <section> (coming soon)',
  50. ]
  51. update.message.reply_text('How can I help?\n{}'.format('\n'.join(commands)))
  52. def search_names(words):
  53. nameCandidates = [name for name in namesToSearch if all(word in name.casefold() for word in words)]
  54. if len(words) > 1:
  55. term = ' '.join(words)
  56. goodCandidates = [name for name in nameCandidates if term in name]
  57. if goodCandidates:
  58. return goodCandidates
  59. return nameCandidates
  60. def oracle(bot, update, args):
  61. if not args:
  62. update.message.reply_text('I need some clues to search for, my master!')
  63. return
  64. words = [word.casefold() for word in args]
  65. nameCandidates = search_names(words)
  66. if not nameCandidates:
  67. update.message.reply_text('I searched very thoroughly, but returned empty-handed, my master!')
  68. return
  69. if len(nameCandidates) > 20:
  70. update.message.reply_text('I need more specific clues, my master! This would return {} names'.format(len(nameCandidates)))
  71. return
  72. if len(nameCandidates) > 1:
  73. update.message.reply_text(format_names(nameCandidates))
  74. return
  75. reply = []
  76. for name in nameCandidates:
  77. for uniqueName in names[name]:
  78. reply.append(format_card(oracleData[uniqueName]))
  79. update.message.reply_text('\n'.join(reply))
  80. def question(bot, update, args):
  81. words = args
  82. text = ' '.join(words).casefold()
  83. reply = []
  84. for name in namesToSearch:
  85. if name.casefold() in text:
  86. reply.append('"' + name + '":')
  87. for uniqueName in names[name]:
  88. reply.append(format_card(oracleData[uniqueName]))
  89. if reply:
  90. update.message.reply_text('\n'.join(reply))
  91. def inline_oracle(bot, update):
  92. query = update.inline_query.query.casefold()
  93. if not query:
  94. return
  95. if len(query) < 3:
  96. return
  97. words = query.split()
  98. nameCandidates = search_names(words)
  99. if not nameCandidates:
  100. return
  101. results = list()
  102. for word in nameCandidates[:3]:
  103. for uniqueName in names[word]:
  104. results.append(
  105. InlineQueryResultArticle(
  106. id=oracleData[uniqueName]['name'],
  107. title=word,
  108. description=preview_card(oracleData[uniqueName]),
  109. input_message_content=InputTextMessageContent(format_card(oracleData[uniqueName]))
  110. )
  111. )
  112. bot.answerInlineQuery(update.inline_query.id, results)
  113. def dispatcher_setup(dispatcher):
  114. dispatcher.add_handler(CommandHandler('start', start))
  115. dispatcher.add_handler(CommandHandler('help', start))
  116. dispatcher.add_handler(CommandHandler('o', oracle, pass_args=True))
  117. dispatcher.add_handler(CommandHandler('q', question, pass_args=True))
  118. dispatcher.add_handler(InlineQueryHandler(inline_oracle))
  119. with open('token') as file:
  120. token = file.read().strip()
  121. updater = Updater(token)
  122. dispatcher_setup(updater.dispatcher)
  123. updater.start_polling()
  124. updater.idle()