def match_team2_players(team2, team2_players): #TODO add prompt for errors #match.team2players match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] intent_name = request['queryResult']['intent']['displayName'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] team2_players = team2_players.strip() #checking dot and dollar, as mongo does not allow dot or dollar in key if "." in team2_players or "$" in team2_players: return json.dumps( Message.general_message( "ValidationError: < '.' or '$' > not allowed in usernames (player names) e.g pankaj.singh is invalid, pankajsingh is valid,\nPlease say 'exit' and re-start match" )) team2_players_list = team2_players.split() if len(team2_players_list) != len(set(team2_players_list)): return json.dumps( Message.general_message( "ValidationError: Duplicate usernames exists in players list, \nPlease say 'exit' and re-start match" )) team2_players_set = [] for x in team2_players_list: if x not in team2_players_set: team2_players_set.append(x) team2_players_set = [x.strip(' ') for x in team2_players_set] return ActionListener.add_players_action(team2, team2_players_set, match_params['match_id'], chat_id, intent_name)
def out_with_fielder_action(match_id, chat_id, request, out_type): bot = BotDatabase(match_id) bot.out_with_fielder(out_type) fielder_list = bot.get_available_bowlers() TelegramHelper.send_keyboard_message(chat_id, "Fielder name?", fielder_list) return json.dumps({})
def match_innings_change(): match_id = Helper.get_match_params(request)['match_id'] chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] bot = BotDatabase(match_id) batsman_list = bot.get_available_batsman() TelegramHelper.send_keyboard_message(chat_id, "strike-batsman name?", batsman_list)
def strike_change_action(chat_id, match_id): bot = BotDatabase(match_id) bot.strike_change() bot.match["undo_count"] = 1 bot.match.save() match_info = bot.get_live_match_info() TelegramHelper.send_scoring_keyboard(chat_id, match_info) return json.dumps({})
def runout_update(match_id, chat_id, request, out_type, run): #TODO bowler stats update, personnel bot = BotDatabase(match_id) bot.run_out_update(out_type, int(run)) fielder_list = bot.get_available_bowlers() TelegramHelper.send_keyboard_message(chat_id, "Fielder name?", fielder_list) return json.dumps({})
def test_out_fielder_update(fielder): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] return ActionListener.out_fielder_update_listner(match_params['match_id'], chat_id, request, fielder)
def test_noball_back(): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] bot = BotDatabase(match_id) match_info = bot.get_live_match_info() TelegramHelper.send_scoring_keyboard(chat_id, match_info) return json.dumps({})
def match_pause(): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] return ActionListener.pause_match_listner(match_params['match_id'], match_params['username'], request)
def test_ball(bowler): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] ActionListener.test_ball_listener(bowler, match_params['match_id'], chat_id) return json.dumps({})
def add_players_action(team_name, team_players_list, match_id, chat_id, intent_name): bot = BotDatabase(match_id) bot.add_players(team_name, team_players_list) if intent_name == 'match.team2players': batsman_list = bot.get_available_batsman() TelegramHelper.send_keyboard_message(chat_id, "strike-batsman name?", batsman_list) return json.dumps({})
def test_out_runout_striker_or_nonstriker(batsman_type): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] return ActionListener.runout_batsman_action(match_id, chat_id, batsman_type)
def undo_listener(chat_id, match_id): bot = BotDatabase(match_id) bot.undo_match() match_info = bot.get_live_match_info() if match_info["ball_number"] == 6: TelegramHelper.send_scoring_keyboard(chat_id, match_info, undo=True) else: TelegramHelper.send_scoring_keyboard(chat_id, match_info)
def match_opening_strike_batsmen(strike_batsman): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] res = ActionListener.update_on_strike_batsmen_listener( strike_batsman, match_params['match_id'], chat_id, "strike_batsman") return res
def toss_action_listener(team1, team2, decision, toss_team, overs, match_id, start_date): _id = BotDatabase.update_teams(team1, team2, decision, toss_team, overs, start_date, match_id) if group_notification_enabled: TelegramHelper.send_general_message( group_id, Message.match_start_group_payload(front_end_url + str(_id), team1, team2, match_id)) return json.dumps( Message.match_start_payload(front_end_url + str(_id), team1))
def test_batsman_change(batsman): match_params = Helper.get_match_params(request) match_id = match_params['match_id'] chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] match = BotDatabase.get_match_document(match_id) send_live_data(match) return ActionListener.batsman_change_action_listener( batsman, match_params['match_id'], chat_id)
def ball_action_listener(run, match_id, chat_id, request, SESSION_ID, action, intent_name, user_text, response): #TODO bowler stats update bot = BotDatabase(match_id) bot.players_stats_update(int(run)) res = bot.run_update(int(run)) #for resume match only #TODO below BotDatabase.push_history(match_id, SESSION_ID, action, intent_name, user_text, res["response"]) if res["type"] == "ask_next_bowler": bowler_list = bot.get_available_bowlers() TelegramHelper.send_keyboard_message( chat_id, res['response'] + "\n\nNext Bowler?", bowler_list) return json.dumps({}) elif res["type"] == "end": # end_message = Message.end_match_payload() # res = Helper.append_clear_context_payload(end_message,request) clear = Helper.clear_contexts(match_id, request) TelegramHelper.remove_keyboard(chat_id) return clear elif res["type"] == "change": TelegramHelper.send_keyboard_general(chat_id, "change innings?", [[{ "text": "change" }, { "text": "Undo" }]]) return json.dumps({}) match_info = bot.get_live_match_info() TelegramHelper.send_scoring_keyboard(chat_id, match_info) return json.dumps({})
def update_on_strike_batsmen_listener(batsman, match_id, chat_id, batsman_type): bot = BotDatabase(match_id) bot.on_strike_batsmen_update(batsman, batsman_type) player_list = [] if batsman_type == "strike_batsman": player_list = bot.get_available_batsman() TelegramHelper.send_keyboard_message(chat_id, "non-strike batsman?", player_list) else: player_list = bot.get_available_bowlers() TelegramHelper.send_keyboard_message(chat_id, "Opening Bowler?", player_list) return json.dumps({})
def test_bowler_change(bowler): print("==> Request in test_bowler_change:") print(request) match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] # if 'undo' in match_params: # ActionListener.undo_listener(chat_id,match_id) # return json.dumps(Message.general_message("Undo done.")) return ActionListener.bowler_change_action_listener( bowler, match_id, chat_id)
def batsman_change_action_listener(batsman, match_id, chat_id): bot = BotDatabase(match_id) response = bot.batsman_change(batsman) if response["type"] == 'ask_next_bowler': bowler_list = bot.get_available_bowlers() TelegramHelper.send_keyboard_message( chat_id, response['response'] + "\n\nNext Bowler?", bowler_list) return json.dumps({}) elif response["type"] == 'next': match_info = bot.get_live_match_info() TelegramHelper.send_scoring_keyboard(chat_id, match_info) # TelegramHelper.send_ball_keyboard_message(chat_id) return json.dumps({}) return json.dumps({})
def match_toss(team_name, decision, team1, team2, overs): #match.toss print('******Request message start*********') print(request) print('******Request message end*********') match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] response = ActionListener.toss_action_listener(team1, team2, decision, team_name, overs, match_params['match_id'], match_params['start_date']) return response
def noball_with_number(number): #test.noball_with_number match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] response = ActionListener.noball_with_number_number_action_listener( number, match_params['match_id'], chat_id) #websocket response start match = BotDatabase.get_match_document(match_id) send_live_data(match) #websocket response end return response
def out_fielder_update_listner(match_id, chat_id, request, fielder): bot = BotDatabase(match_id) response = bot.out_fielder_update(fielder) if response["type"] == 'ask_next_batsman': batsman_list = bot.get_available_batsman() TelegramHelper.send_keyboard_message(chat_id, "Next Batsman?", batsman_list) return json.dumps({}) elif response["type"] == "end": BotDatabase.set_match_status(match_id=match_id, from_status="live", to_status="end") clear = Helper.clear_contexts(match_id, request) TelegramHelper.remove_keyboard(chat_id) return clear elif response["type"] == "change": TelegramHelper.send_keyboard_general(chat_id, "change innings?", [[{ "text": "change" }, { "text": "Undo" }]]) return json.dumps({}) return json.dumps(response['response'])
def link_bot_user(): print('in ********* link_bot_user') username = '' if "from" in request['originalDetectIntentRequest']['payload'][ "data"] and "username" in request['originalDetectIntentRequest'][ 'payload']["data"]["from"]: username = request['originalDetectIntentRequest']['payload']['data'][ 'from']['username'] else: return json.dumps( Message.general_message( "ValidationError: Telegram username is required, please go to settings and create one" )) if username[:1] == '@': username = username[1:] bot_user = '******' + username platform_user = username print(bot_user) print(platform_user) if "." in bot_user or "$" in bot_user: return json.dumps( Message.general_message( "ValidationError: < '.' or '$' > not allowed in usernames e.g pankaj.singh is invalid, pankajsingh is valid,\nPlease change your telegram username" )) try: source = request['originalDetectIntentRequest']['source'] res = '' if source == 'telegram': if not TelegramHelper.validate_platform_user_request_message( request): res = Message.get_invalid_request_payload() return json.dumps(res) print(source) if not BotDatabase.user_already_exist(bot_user): res = BotDatabase.link_users(bot_user, platform_user, source) print("res=") print(res) if res == False: return json.dumps(Message.get_invalid_request_payload()) else: return json.dumps(Message.general_message("done")) else: res = Message.get_invalid_request_payload() return json.dumps(res) except Exception as e: return Message.get_invalid_request_payload()
def test_runs(number): #score = req['queryResult']['parameters']['number'] # flask_request_json = flask_request.get_json() number = int(number) match_params = Helper.get_match_params(request) match_id = '' chat_id = '' if match_params['username'] == '': match_id = request['queryResult']['parameters']['match_id'] else: match_id = match_params['match_id'] chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] if 'exit' in match_params: TelegramHelper.remove_keyboard(chat_id) return match_params['exit'] # if 'undo' in match_params: # ActionListener.undo_listener(chat_id,match_id) # return json.dumps(Message.general_message("Undo done.")) match_status = BotDatabase.get_match_status(match_id) print("match_status before processing:") print(match_status) if match_status == 'pause': BotDatabase.set_match_status(match_id=match_id, from_status="pause", to_status="resume") user_text = request['queryResult']['queryText'] response = '' session = request['session'] intent_name = request['queryResult']['intent']['displayName'] action = request['queryResult']['action'] SESSION_ID = session.rpartition('/')[2] match_status = BotDatabase.get_match_status(match_id) print("match_status after change:") print(match_status) if match_status == 'live': chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] response = ActionListener.ball_action_listener(number, match_id, chat_id, request, SESSION_ID, action, intent_name, user_text, response) match = BotDatabase.get_match_document(match_id) start_int = time.process_time() print("start of send_live_data==>") send_live_data(match) print("end of send_live_data==>") print(time.process_time() - start_int) elif match_status == 'resume': print('********** Resume *************') print("match_id:" + match_id) print("status in if block:") print(match_status) last_txn = ActionListener.get_last_txn_from_history( match_id, match_status) response = json.dumps(last_txn['response']) print(json.dumps(response)) return response
def test_ball_listener(bowler, match_id, chat_id): bot = BotDatabase(match_id) bot.current_bowler_update(bowler) match_info = bot.get_live_match_info() TelegramHelper.send_scoring_keyboard(chat_id, match_info)
def undo_next_over_action(chat_id, match_id): bot = BotDatabase(match_id) bowler_list = bot.get_available_bowlers() TelegramHelper.send_keyboard_message(chat_id, "Next Bowler?", bowler_list)
def noball_with_number_number_action_listener(run, match_id, chat_id): bot = BotDatabase(match_id) bot.noball_update(int(run)) match_info = bot.get_live_match_info() TelegramHelper.send_scoring_keyboard(chat_id, match_info) return json.dumps({})
def bowler_change_action_listener(bowler, match_id, chat_id): bot = BotDatabase(match_id) bot.current_bowler_update(bowler) match_info = bot.get_live_match_info() TelegramHelper.send_scoring_keyboard(chat_id, match_info) return json.dumps({})