__main__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """Telegram bot"""
  2. import re
  3. from telegram import ParseMode
  4. from telegram.ext import MessageHandler, CommandHandler, Filters, ConversationHandler, RegexHandler
  5. from app import LOGGER, BOT, UPDATER
  6. from app import database
  7. from app.conversations.add_account import ADD_ACCOUNT_CONV
  8. def cmd_start(update, context):
  9. """Start command"""
  10. update.message.reply_text(
  11. 'Hello {},\ntype /help for a list of commands'.format(update.message.from_user.first_name))
  12. def cmd_help(update, context):
  13. """Help command"""
  14. message_list = [
  15. '**Command list**',
  16. '/accounts - list of accounts',
  17. '/add\\_account - add account to list',
  18. ]
  19. message = '\n'.join(message_list)
  20. print(message)
  21. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  22. def cmd_accounts(update, context):
  23. """Return account list"""
  24. accounts = database.get_rr_accounts(update.message.from_user.id)
  25. message_list = ['Accounts verified to this Telgeram account:']
  26. for account in accounts:
  27. # name = re.sub(r'\[.*\]\s', '', account.name)
  28. desktop_link = '[desktop](https://rivalregions.com/#slide/profile/{})'.format(account.id)
  29. mobile_link = '[mobile](https://m.rivalregions.com/#slide/profile/{})'.format(account.id)
  30. message_list.append(
  31. '• {} {} - {}'.format(
  32. escape_text(account.name),
  33. desktop_link,
  34. mobile_link,
  35. )
  36. )
  37. message = '\n'.join(message_list)
  38. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  39. def escape_text(text):
  40. """Escape text"""
  41. return text \
  42. .replace("_", "\\_") \
  43. .replace("*", "\\*") \
  44. .replace("[", "\\[") \
  45. .replace("`", "\\`")
  46. def main():
  47. """Main function"""
  48. dispatcher = UPDATER.dispatcher
  49. # general commands
  50. dispatcher.add_handler(CommandHandler('start', cmd_start))
  51. dispatcher.add_handler(CommandHandler('help', cmd_help))
  52. # account commaonds
  53. dispatcher.add_handler(CommandHandler('accounts', cmd_accounts))
  54. dispatcher.add_handler(ADD_ACCOUNT_CONV)
  55. UPDATER.start_polling()
  56. UPDATER.idle()
  57. if __name__ == '__main__':
  58. main()