add.py 6.6 KB

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