add.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. """Add player conversation"""
  2. import random
  3. import string
  4. from telegram import ParseMode
  5. from telegram.ext import MessageHandler, CommandHandler, Filters, ConversationHandler
  6. from app import api, functions, database, HEADERS, BASE_URL, LOGGER
  7. PLAYER_ID, CHOOSE, CONFIRM, VERIFICATION = range(4)
  8. def conv_ask_player_id(update, context):
  9. """Ask player id"""
  10. LOGGER.info('"@%s" start add player conversation', update.message.from_user.username)
  11. update.message.reply_text(
  12. 'Starting add account conversation, use /cancel to stop.' +
  13. ' Send me your Rival Regions account name or ID.'
  14. )
  15. return PLAYER_ID
  16. def conv_player_choose(update, context):
  17. """Ask max resource"""
  18. player_name = update.message.text
  19. LOGGER.info(
  20. '"@%s" searching for player name "%s"',
  21. update.message.from_user.username,
  22. player_name
  23. )
  24. update.message.reply_text('searching for \'{}\', this may take some time'.format(player_name))
  25. players = api.get_players_by_name(player_name)
  26. if len(players) == 0:
  27. update.message.reply_text('No accounts found witht that name, try again')
  28. return PLAYER_ID
  29. context.user_data['player_list'] = players
  30. message = 'Choose from list:\n'
  31. for num, player in enumerate(players, start=1):
  32. message += '{}) {} ({})\n'.format(num, player['name'], player['level'])
  33. update.message.reply_text(message)
  34. return CHOOSE
  35. def conv_player_number_error(update, context):
  36. """Wrong input error"""
  37. incorrect_input = update.message.text
  38. LOGGER.info(
  39. '"@%s" incorrect number number "%s"',
  40. update.message.from_user.username,
  41. incorrect_input
  42. )
  43. update.message.reply_text(
  44. '{}, I don\'t recognize that. What number?'.format(
  45. incorrect_input
  46. )
  47. )
  48. return CHOOSE
  49. def conv_player_id_confirm(update, context):
  50. """Confirm player """
  51. player_id = int(update.message.text)
  52. if player_id <= 25:
  53. player_index = player_id-1
  54. if player_index >= len(context.user_data['player_list']):
  55. update.message.reply_text('{} is not an option, try again.'.format(player_id),)
  56. return CHOOSE
  57. player = context.user_data['player_list'][player_index]
  58. player_id = player['id']
  59. context.user_data['player_id'] = player_id
  60. update.message.reply_text(
  61. 'Retreiving player from Rival Regions, this might take a couple seconds.'
  62. )
  63. LOGGER.info(
  64. '"@%s" RR player id "%s"',
  65. update.message.from_user.username,
  66. player_id
  67. )
  68. player = api.get_rr_player(player_id)
  69. message_list = [
  70. '*Player details*',
  71. '*ID*: {}'.format(player_id),
  72. '*Name*: {}'.format(functions.escape_text(player['name'])),
  73. '*Region*: {}'.format(player['region']),
  74. '*Residency*: {}'.format(player['residency']),
  75. '*Registration date*: {}'.format(player['registation_date']),
  76. '\nPlease confirm this is your player by typing \'confirm\'.',
  77. ]
  78. update.message.reply_text(
  79. '\n'.join(message_list),
  80. parse_mode=ParseMode.MARKDOWN
  81. )
  82. return CONFIRM
  83. def conv_verification(update, context):
  84. """Sending announcement"""
  85. update.message.reply_text(
  86. 'Verification code will be send to your Rival Region player in a couple of secconds. ' + \
  87. 'Check your personal messages for a verification code and send it here.',
  88. parse_mode=ParseMode.MARKDOWN
  89. )
  90. letters = string.ascii_lowercase
  91. verification_code = ''.join(random.choice(letters) for i in range(5))
  92. LOGGER.info(
  93. '"@%s" verification code "%s"',
  94. update.message.from_user.username,
  95. verification_code
  96. )
  97. message = 'Your verification code:\n{}\n\n'.format(verification_code) + \
  98. 'Please don\'t share this code except with @rr_verification_bot on Telegram.'
  99. api.send_personal_message(context.user_data['player_id'], message)
  100. context.user_data['verification_code'] = verification_code
  101. return VERIFICATION
  102. def conv_finish(update, context):
  103. """Sending announcement"""
  104. verification_code = update.message.text
  105. if verification_code != context.user_data['verification_code']:
  106. LOGGER.info(
  107. '"@%s" wrong verification code try "%s"',
  108. update.message.from_user.username,
  109. verification_code,
  110. )
  111. if 'tries' not in context.user_data:
  112. context.user_data['tries'] = 0
  113. context.user_data['tries'] += 1
  114. if context.user_data['tries'] >= 3:
  115. LOGGER.info(
  116. '"@%s" too many wrong verification tries',
  117. update.message.from_user.username
  118. )
  119. update.message.reply_text(
  120. 'Failed verification to many times, canceled action.',
  121. parse_mode=ParseMode.MARKDOWN
  122. )
  123. context.user_data.clear()
  124. return ConversationHandler.END
  125. update.message.reply_text(
  126. 'Verificated code doesn\'t match, try again.',
  127. parse_mode=ParseMode.MARKDOWN
  128. )
  129. return VERIFICATION
  130. player_id = context.user_data['player_id']
  131. LOGGER.info(
  132. '"@%s" succesfully verified RR player "%s"',
  133. update.message.from_user.username,
  134. player_id,
  135. )
  136. database.verify_rr_player(update.message.from_user.id, player_id)
  137. update.message.reply_text(
  138. 'Verificated your Rival Region player to Telegram. Type /accounts to see your accounts',
  139. parse_mode=ParseMode.MARKDOWN
  140. )
  141. context.user_data.clear()
  142. return ConversationHandler.END
  143. def conv_error_finish(update, context):
  144. """Ask max resource"""
  145. incorrect_input = update.message.text
  146. update.message.reply_text(
  147. '"{}" not recognized. Send me the verification code.\n/cancel to cancel'.format(
  148. incorrect_input
  149. ),
  150. )
  151. return VERIFICATION
  152. def conv_cancel(update, context):
  153. """Cancel announcement"""
  154. update.message.reply_text('Canceled action.')
  155. context.user_data.clear()
  156. return ConversationHandler.END
  157. # announcement conversation
  158. ADD_CONV = ConversationHandler(
  159. entry_points=[CommandHandler('add', conv_ask_player_id)],
  160. states={
  161. PLAYER_ID: [
  162. MessageHandler(Filters.regex(r'^\d*$'), conv_player_id_confirm),
  163. MessageHandler(Filters.text, conv_player_choose),
  164. ],
  165. CHOOSE: [
  166. MessageHandler(Filters.regex(r'^\d*$'), conv_player_id_confirm),
  167. MessageHandler(Filters.text, conv_player_choose),
  168. ],
  169. CONFIRM: [
  170. MessageHandler(Filters.regex('confirm'), conv_verification),
  171. MessageHandler(Filters.text, conv_cancel),
  172. ],
  173. VERIFICATION: [
  174. MessageHandler(Filters.regex(r'^([a-z]|\d)+$'), conv_finish),
  175. MessageHandler(Filters.text, conv_error_finish),
  176. ],
  177. },
  178. fallbacks=[CommandHandler('cancel', conv_cancel)]
  179. )