Queer European MD passionate about IT

useful_tools.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """General purpose functions for Telegram bots."""
  2. # Standard library
  3. import json
  4. from collections import OrderedDict
  5. # Project modules
  6. from .api import TelegramError
  7. from .bot import Bot
  8. from .messages import default_useful_tools_messages
  9. from .utilities import get_cleaned_text, recursive_dictionary_update
  10. async def _message_info_command(bot: Bot, update: dict, language: str):
  11. """Provide information about selected update.
  12. Selected update: the message `update` is sent in reply to. If `update` is
  13. not a reply to anything, it gets selected.
  14. The update containing the command, if sent in reply, is deleted.
  15. """
  16. if 'reply_to_message' in update:
  17. selected_update = update['reply_to_message']
  18. else:
  19. selected_update = update
  20. await bot.send_message(
  21. text=bot.get_message(
  22. 'useful_tools', 'info_command', 'result',
  23. language=language,
  24. info=json.dumps(selected_update, indent=2)
  25. ),
  26. update=update,
  27. reply_to_message_id=selected_update['message_id'],
  28. )
  29. if selected_update != update:
  30. try:
  31. await bot.delete_message(update=update)
  32. except TelegramError:
  33. pass
  34. async def _length_command(bot: Bot, update: dict, user_record: OrderedDict):
  35. message_text = get_cleaned_text(
  36. update=update,
  37. bot=bot,
  38. replace=[
  39. alias
  40. for alias in bot.messages[
  41. 'useful_tools'
  42. ][
  43. 'length_command'
  44. ][
  45. 'language_labelled_commands'
  46. ].values()
  47. ]
  48. )
  49. if message_text:
  50. text = bot.get_message(
  51. 'useful_tools', 'length_command', 'result',
  52. user_record=user_record, update=update,
  53. n=len(message_text)
  54. )
  55. elif 'reply_to_message' not in update:
  56. text = bot.get_message(
  57. 'useful_tools', 'length_command', 'instructions',
  58. user_record=user_record, update=update
  59. )
  60. else:
  61. text = bot.get_message(
  62. 'useful_tools', 'length_command', 'result',
  63. user_record=user_record, update=update,
  64. n=len(update['reply_to_message']['text'])
  65. )
  66. update = update['reply_to_message']
  67. reply_to_message_id = update['message_id']
  68. return dict(
  69. chat_id=update['chat']['id'],
  70. text=text,
  71. parse_mode='HTML',
  72. reply_to_message_id=reply_to_message_id
  73. )
  74. async def _ping_command(bot: Bot, update: dict):
  75. """Return `pong` only in private chat."""
  76. chat_id = bot.get_chat_id(update=update)
  77. if chat_id < 0:
  78. return
  79. return "<i>Pong!</i>"
  80. def init(telegram_bot: Bot, useful_tools_messages=None):
  81. """Define commands for `telegram_bot`.
  82. You may provide customized `useful_tools_messages` that will overwrite
  83. `default_useful_tools_messages`. Missing entries will be kept default.
  84. """
  85. if useful_tools_messages is None:
  86. useful_tools_messages = dict()
  87. useful_tools_messages = recursive_dictionary_update(
  88. default_useful_tools_messages,
  89. useful_tools_messages
  90. )
  91. telegram_bot.messages['useful_tools'] = useful_tools_messages
  92. @telegram_bot.command(command='/info',
  93. aliases=None,
  94. reply_keyboard_button=None,
  95. show_in_keyboard=False,
  96. **{key: val for key, val
  97. in useful_tools_messages['info_command'].items()
  98. if key in ('description', 'help_section',
  99. 'language_labelled_commands')},
  100. authorization_level='moderator')
  101. async def message_info_command(bot, update, language):
  102. return await _message_info_command(bot=bot,
  103. update=update,
  104. language=language)
  105. @telegram_bot.command(command='/length',
  106. aliases=None,
  107. reply_keyboard_button=None,
  108. show_in_keyboard=False,
  109. **{key: val for key, val
  110. in useful_tools_messages['length_command'].items()
  111. if key in ('description', 'help_section',
  112. 'language_labelled_commands')},
  113. authorization_level='everybody')
  114. async def length_command(bot, update, user_record):
  115. return await _length_command(bot=bot, update=update, user_record=user_record)
  116. @telegram_bot.command(command='/ping',
  117. aliases=None,
  118. reply_keyboard_button=None,
  119. show_in_keyboard=False,
  120. **{key: val for key, val
  121. in useful_tools_messages['ping_command'].items()
  122. if key in ('description', 'help_section',
  123. 'language_labelled_commands')},
  124. authorization_level='everybody')
  125. async def ping_command(bot, update):
  126. return await _ping_command(bot=bot, update=update)