def start(update, context): language = database.get_language(update.message.chat_id) response = '' response = response.join(responses[language]['start']) context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response)
def update_response(update, context): data = json.loads(update.callback_query.data) if data['type'] == types['moves']: table = "Moves" else: table = "IV" field = update.callback_query.data if update._effective_message.chat_id < 0: admins = (admin.user.id for admin in context.bot.get_chat_administrators( update._effective_message.chat.id)) if update._effective_user.id in admins: database.configure_iv_response(update._effective_chat.id, table, data["field"]) else: database.configure_iv_response(update._effective_chat.id, table, data["field"]) #Update the check boxes on the markup menu language = database.get_language(update._effective_chat.id) responses = jsonresponse[language] response = responses['iv_menu'] try: context.bot.edit_message_text( chat_id=update._effective_chat.id, message_id=update._effective_message.message_id, text=response, reply_markup=custom_keyboard(update._effective_message.chat.id, data['type'])) logger.info("Updated IV output for group " + str(update._effective_chat.id)) except: logger.info("Could not edit message in group " + str(update._effective_chat.id)) return
def silph_rank(update, context): try: context.bot.delete_message(chat_id=update.message.chat_id,message_id=update.message.message_id) except: logger.info("Cannot delete message Chat:%s MessageID:%s", update.message.chat_id, update.message.message_id) language = database.get_language(update.message.chat_id) bot_message = context.bot.send_message(chat_id=update.message.chat_id, text=responses[language]['rank_disabled']) job.run_once(delete_message, 30, context=(bot_message.chat_id, bot_message.message_id))
def pvp(update, context): #Load the language settings for this group language = database.get_language(update.message.chat_id) responses = jsonresponse[language] #Try to delete the /pvp command try: context.bot.delete_message( chat_id=update.message.chat_id, message_id=update._effective_message['message_id']) #If we cannot delete the command, the bot probably doesn't have admin rights except: context.bot.send_message(chat_id=update.message.chat_id, text=responses['pvp_cant_delete']) logger.info('Cannot delete message Chat:%s MessageID:%s', update.message.chat_id, update._effective_message['message_id']) #Check, if we have a name for this telegram user name = trainernames.get_trainername(update.effective_user.id) #Format the name properly if we have a user. Otherwise, we just take the users telegram name if name is not None: response = "[" + name + "](tg://user?id=" + str( update.effective_user.id) + ")" + responses['poll'] else: response = update.effective_user.name + responses['poll'] #Does the poll provide any arguments such as league if len(context.args) > 0: #Did the user specify a league that he wants to play in? - If so, we format it if context.args[0].lower( ) == responses['greatleague'] or context.args[0].lower( ) == responses['ultraleague'] or context.args[0].lower( ) == responses['masterleague']: response += responses['league'] + context.args[0] #Did he specify more information? We just ass "Info:" infront of it and return the same query if len(context.args) > 1: response += responses['pollinfo'] + ' '.join(context.args[1:]) #Did the user provide information without specifying the league else: response += responses['pollinfo'] + ' '.join(context.args) #Send the poll and add the buttons to it bot_message = context.bot.send_message( parse_mode='Markdown', chat_id=update.message.chat_id, text=response, reply_markup=pvp_keyboard(responses)) logger.info( 'PvP request by %s (MessageID: %s, ChatID: %s) with arguments %s', update._effective_user.username, bot_message.message_id, bot_message.chat_id, context.args) #Store the message and create a list for the competitors pvprequests[bot_message.message_id, bot_message.chat_id] = { 'user': update.effective_user.id, 'date': datetime.now(), 'text': response } competitors[bot_message.message_id, bot_message.chat_id] = []
def add_trainercode(update, context): #Load the language language = database.get_language(update.message.chat_id) responses = jsonresponse[language] #Trim spaces from the code code = ' '.join(context.args) code = code.replace(" ", "") #If the code is not 12 characters long it is malformed if len(code) != 12: context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['trainercode_length']) return #If we have a valid code store it in the db else: conn = database.connect() #Insert the code try: cursor = conn.cursor() query = "INSERT INTO `Names` (TelegramID, Trainercode) VALUES (?,?)" try: cursor.execute(query, ( update._effective_user.id, code, )) logger.info("Update entry %s (%s, %s)", query, update._effective_user.id, code) #If the user already has a trinercode or name we want to update this entry except: query = "UPDATE `Names` SET Trainercode=? WHERE TelegramID=?;" cursor.execute(query, ( code, update._effective_user.id, )) logger.info("Update entry %s (%s, %s)", query, code, update._effective_user.id) conn.commit() context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['trainercode_success']) #We couln't connect to the db except: logger.warn("Could not set Trainercode") context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['trainername_fail']) #Close the connection finally: conn.close()
def add_trainername(update, context): #Load the language of the user language = database.get_language(update.message.chat_id) responses = jsonresponse[language] #The trainername is not within the Pokemon Go boundaries - Inform the user and return if len(context.args) != 1 or len(context.args[0]) > 15 or len( context.args[0]) < 4: context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['trainername_length']) #Set the trainername else: logger.info("Setting Trainername") #Connect to the database try: conn = database.connect() cursor = conn.cursor() query = "INSERT INTO `Names` (TelegramID, Trainername) VALUES (?,?)" #Try to insert the trainername try: cursor.execute(query, ( update._effective_user.id, context.args[0], )) logger.info("Insert new entry %s (%s, %s)", query, update._effective_user.id, context.args[0]) #Update the trainername if the user already exists and wants to update their trainername except: query = "UPDATE `Names` SET Trainername=? WHERE TelegramID=?;" cursor.execute(query, ( context.args[0], update._effective_user.id, )) logger.info("Update entry %s (%s, %s)", query, context.args[0], update._effective_user.id) conn.commit() context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['trainername_success']) #We cannot connect to the database except: logger.warn("Could not set Trainername") context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['trainername_fail']) #Make sure the connection gets closed finally: if conn is not None: conn.close()
def add_competitor(update, context): #Get the info about the message that was clicked query = update.callback_query #Get the current language language = database.get_language(update._effective_chat.id) #remove user from competitor list if update.effective_user in competitors[query.message.message_id, update._effective_chat.id]: logger.info('%s revokes the PvP request from %s', update.effective_user.username, pvprequests[update.effective_message.message_id, update.effective_chat.id]['text'].split()[0]) competitors[query.message.message_id, update._effective_chat.id].remove(update.effective_user) #add user too competitor list else: logger.info('%s joins from the PvP request from %s', update.effective_user.username, pvprequests[update.effective_message.message_id, update.effective_chat.id]['text'].split()[0]) competitors[query.message.message_id, update._effective_chat.id].append(update.effective_user) #Retrieve the user object and his name, if he has one defined user = update.effective_user name = trainernames.get_trainername(user.id) #Format the users name if name is not None: direct_message = "[" + name + "](tg://user?id=" + str(user.id) + ")" else: direct_message = '@' + user.username #load the direct message to notifiy the creator direct_message += jsonresponse[language]['accepted'] #Try to send a private notification to the creator of the poll try: context.bot.send_message(parse_mode='Markdown', chat_id=pvprequests[update.effective_message.message_id, update.effective_chat.id]['user'], text=direct_message) logger.info("Sent a private notification to %s", pvprequests[update.effective_message.message_id, update.effective_chat.id]['text'].split()[0]) #If the creator doesn't have a private chat with the bot we cannot send him a private notification except: logger.info("Cannot initiate private conversation with %s", pvprequests[update.effective_message.message_id, update.effective_chat.id]['text'].split()[0]) """ Edit the pvp request and add the competitor""" #Get the initial request response = pvprequests[update.effective_message.message_id, update.effective_chat.id]['text'] #Add the name of each user to the request for user in competitors[query.message.message_id, update._effective_chat.id]: name = trainernames.get_trainername(user.id) if name is not None: response += "\n- [" + name + "](tg://user?id=" + str(update.effective_user.id) + ")" else: response += '\n- ' + user.name #Update the message context.bot.edit_message_text(parse_mode='Markdown', chat_id=query.message.chat_id, message_id=query.message.message_id, text=response, reply_markup=pvp_keyboard(jsonresponse[language]))
def language(update, context): logger.info('Language query by %s with query %s', update._effective_user.username, context.args) #Make sure that we only handle messages that we can speak language = database.get_language(update.message.chat_id) if update.message.chat_id < 0: admins = (admin.user.id for admin in context.bot.get_chat_administrators(update.message.chat.id)) if update._effective_user.id not in admins: response = responses[language]['only_for_admins'] bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response) return if len(context.args) == 1 and context.args[0].lower() in supported_languages: database.toggle_groups(update, context, 'Language') #If we reject the input we try to delete the users message and let him know which languages we speak else: #Get the language that we are speaking in this group and tell the user which languages we can speak response = responses[language]['language_not_supported'] response = response.format(supported_languages) bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response)
def language(update, context): #Make sure that we only handle messages that we can speak if len(context.args) == 1 and context.args[0].lower( ) in supported_languages: database.toggle_groups(update, context, 'Language') #If we reject the input we try to delete the users message and let him know which languages we speak else: try: context.bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id) except: logger.info("Cannot delete message Chat:%s MessageID:%s", update.message.chat_id, update.message.message_id) #Get the language that we are speaking in this group and tell the user which languages we can speak language = database.get_language(update.message.chat_id) response = responses[language]['language_not_supported'] response = response.format(supported_languages) bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response)
def update_form(update, context): try: language = database.get_language(update._effective_chat.id) responses = jsonresponse[language] iv_config = database.get_iv_config(update._effective_chat.id, "IV") #data = json.loads(update.callback_query.data) split_data = update.callback_query.data.split(',') data = {"IVs": split_data[0:3], "league": [split_data[3]], "xl": [split_data[4]], "Name": split_data[5]} en_name, initial_language, different_language = get_english_name(data['Name'], language) response, ivs = iv_given(en_name.lower(), initial_language, responses, iv_config, data['IVs'][0], data['IVs'][1], data['IVs'][2], data['league'][0], data['xl'][0]=='1') #context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=form_keyboard(data)) #data = {"IVs": data['IVs']} forms = find_forms(en_name) context.bot.edit_message_text(parse_mode='HTML', chat_id=update._effective_chat.id, message_id=update._effective_message.message_id, text=response, reply_markup=form_keyboard(en_name, forms, data)) except: logger.warn("Could not update iv form query: " + str(update.callback_query.bot.data))
def build_move_response(update, context, type): try: language = database.get_language(update.message.chat_id) responses = jsonresponse[language] logger.info('Move query %s with query %s', update._effective_user.username, context.args) #The user wants to edit the response customisation if (len(context.args) == 0): if update.message.chat_id < 0: admins = (admin.user.id for admin in context.bot.get_chat_administrators( update.message.chat.id)) if update._effective_user.id in admins: response = responses['moves_menu'] context.bot.send_message( parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard( update.message.chat_id, response_menu.types["moves"])) else: response = responses['only_for_admins'] bot_message = context.bot.send_message( parse_mode='Markdown', chat_id=update.message.chat_id, text=response) return else: logger.info("Invalid pokemon") response = responses['moves_menu'] context.bot.send_message( parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard( update.message.chat_id, response_menu.types["moves"])) return #Check, if move queries should be en-/disabled in this group if len(context.args) == 1 and (context.args[0] == 'enable' or context.args[0] == 'disable'): logger.info("/Move %s by %s", context.args[0], update._effective_user.username) #En-/disable IV queries for this group database.toggle_groups(update, context, 'Moves') return False #If we are in a group and dont want ivs queries are disabled we just delete the request and return if update.message.chat_id < 0 and not database.group_enabled( update.message.chat_id, 'Moves'): logger.info("Disabled /moves request attempted by (%s)", update._effective_user.username) return False #If the user did not specify a Pokemon if (len(context.args) != 1): response = responses['iv_no_argument'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) return False #Look for the moves of that pokemon else: #Load the IV_Config for the current chat id (i.e. which attributes should be returned) move_config = database.get_iv_config(update.message.chat_id, "Moves") en_name, initial_language, different_language = iv_check.get_english_name( context.args[0], language) response = get_moves(en_name.lower(), language, responses, type, move_config) #Send the response to the user char_limit = 4096 for i in range(0, int(len(response) / 4096) + 1): if len(response) > 0: splitted_response = response[i * char_limit:(i + 1) * char_limit] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=splitted_response) return True #We got some weird input which we cannot perform except: logger.info("Could not perform /move request") response = responses['iv_error'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) return False
def iv_rank(update, context): #Retrieve the current language language = database.get_language(update.message.chat_id) responses = jsonresponse[language] logger.info('IV request by %s with query %s', update._effective_user.username, context.args) #Check, if IV queries should be en-/disabled in this group if len(context.args) == 1 and (context.args[0] == 'enable' or context.args[0] == 'disable'): logger.info("/IV %s by %s", context.args[0], update._effective_user.username) #En-/disable IV queries for this group database.toggle_groups(update, context, 'IV') return #If we are in a group and dont want ivs queries are disabled we just delete the request and return if update.message.chat_id < 0 and not database.group_enabled( update.message.chat_id, 'IV'): logger.info("Disabled /iv request attempted by (%s)", update._effective_user.username) context.bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id) return #The user didn't specify a pokemon if (len(context.args) == 0): logger.info("Invalid pokemon") response = responses['iv_no_argument'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) else: try: if context.args[0][0] is '+': evolutions, initial_language, different_language = get_pokemon_family( context.args[0][1:], language) else: evolutions, initial_language, different_language = get_english_name( context.args[0], language) evolutions = [evolutions] for evo in evolutions: #If the user just specified a Pokemon - Return the optimal distribution if (len(context.args) == 1): response = iv_given(evo.lower(), initial_language, responses) #If the user gave IVs with the pokemon - Return where this one ranks elif (len(context.args) == 4): att = normalize_iv(context.args[1]) de = normalize_iv(context.args[2]) sta = normalize_iv(context.args[3]) response = iv_given(evo.lower(), initial_language, responses, att, de, sta) logger.info('Return %s', response.encode("utf-8")) if different_language: language_hint = responses['language_hint'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['language_hint']) #Send the response to the user context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) #We got some weird input which we cannot perform except: logger.info("Could not perform /iv request") response = responses['iv_error'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response)
def get_iv_rank(update, context, xl): #Retrieve the current language language = database.get_language(update.message.chat_id) responses = jsonresponse[language] logger.info('IV request by %s with query %s', update._effective_user.username, context.args) #Check, if IV queries should be en-/disabled in this group if len(context.args) == 1 and (context.args[0] == 'enable' or context.args[0] == 'disable'): logger.info("/IV %s by %s", context.args[0] , update._effective_user.username) #En-/disable IV queries for this group database.toggle_groups(update, context, 'IV') return #If we are in a group and dont want ivs queries are disabled we just delete the request and return if update.message.chat_id < 0 and not database.group_enabled(update.message.chat_id, 'IV'): logger.info("Disabled /iv request attempted by (%s)", update._effective_user.username) context.bot.delete_message(chat_id=update.message.chat_id,message_id=update.message.message_id) return league = '1500' if len(context.args) > 0 and context.args[0] == '2500': context.args.pop(0) league = '2500' if len(context.args) > 0 and context.args[0] == '500': context.args.pop(0) league = '500' #The user didn't specify a pokemon if(len(context.args) == 0): if update.message.chat_id < 0: admins = (admin.user.id for admin in context.bot.get_chat_administrators(update.message.chat.id)) if update._effective_user.id in admins: response = responses['iv_menu'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard(update.message.chat_id, response_menu.types["iv"])) #context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=iv_keyboard(update.message.chat_id)) else: response = responses['only_for_admins'] bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response) return else: logger.info("Invalid pokemon") response = responses['iv_menu'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard(update.message.chat_id, response_menu.types["iv"])) #context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=iv_keyboard(update.message.chat_id)) return else: try: #Load the IV_Config for the current chat id (i.e. which attributes should be returned) iv_config = database.get_iv_config(update.message.chat_id, "IV") if context.args[0][0] == '+': evolutions, initial_language, different_language = get_pokemon_family(context.args[0][1:], language) else: evolutions, initial_language, different_language = get_english_name(context.args[0], language) evolutions = [evolutions] for evo in evolutions: #If the user just specified a Pokemon - Return the optimal distribution if(len(context.args) == 1): response, ivs = iv_given(evo.lower(), initial_language, responses, iv_config, None, None, None, league, xl) #When a user requests a specific rank if(len(context.args) == 2): rank = context.args[1] response, ivs = iv_given_rank(evo.lower(), initial_language, responses, iv_config, rank, league, xl) #If the user gave IVs with the pokemon - Return where this one ranks elif(len(context.args) == 4): att = normalize_iv(context.args[1]) de = normalize_iv(context.args[2]) sta = normalize_iv(context.args[3]) response, ivs = iv_given(evo.lower(), initial_language, responses, iv_config, att, de, sta, league, xl) logger.info('Return %s', response.encode("utf-8")) if different_language: context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['language_hint']) #Send the response to the user data = {"IVs": ivs, 'league': [str(league)], 'xl': [str(int(xl))]} forms = find_forms(evo) #forms = ["", "+alolan", "+purified"] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=form_keyboard(evo.lower(), forms, data)) #We got some weird input which we cannot perform except: logger.info("Could not perform /iv request") response = responses['iv_error'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response)