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 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 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 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 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 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 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 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 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 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 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