def test_ball_strikechange(): match_params = Helper.get_match_params(request) match_id = match_params['match_id'] chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] ActionListener.strike_change_action(chat_id, match_id) return json.dumps(Message.general_message("strike changed"))
def test_undo_nextover(): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] ActionListener.undo_next_over_action(chat_id, match_id) return json.dumps({})
def match_delete(): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] ActionListener.delete_live_matches_action(match_id) return json.dumps( Message.general_message("Done. Now you can start match again."))
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_noball_runs(run): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] return ActionListener.runout_update(match_id, chat_id, request, "runout_noball", int(run))
def test_out_bowled(): match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] return ActionListener.out_without_fielder_action(match_id, chat_id, request, "bowled")
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 test_out_caught(): # return out_common("caught",request) match_params = Helper.get_match_params(request) chat_id = request['originalDetectIntentRequest']['payload']['data'][ 'chat']['id'] match_id = match_params['match_id'] return ActionListener.out_with_fielder_action(match_id, chat_id, request, "caught")
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_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 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 user_stats(): match_params = Helper.get_match_params(request) print("username=" + match_params['username']) if not match_params['username'] == '': response = ActionListener.user_stats_listener(match_params['username'], match_params['source']) if response == None: return json.dumps(Message.get_invalid_request_payload()) return json.dumps(response) else: return json.dumps(Message.get_invalid_request_payload())
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 most_runs(): return json.dumps(ActionListener.most_runs_listener())
def match_resume(scorer_id): match_id = Helper.get_match_params(request)['match_id'] scorer_username = scorer_id if scorer_username[:1] == '@': scorer_username = scorer_username[1:] print("userid from intent:") print(scorer_username) scorer_id = BotDatabase.userid_from_username(scorer_username, 'telegram') print("userid after conversion:") print(scorer_id) #-------------------------------------------------------------- print("processing......") last_txn = ActionListener.get_last_txn_from_history(scorer_id) match_status = BotDatabase.get_match_status(scorer_id) if match_status == 'live': BotDatabase.set_match_status(match_id=scorer_id, from_status="live", to_status="pause") SESSION_ID = last_txn['SESSION_ID'] intent_name = last_txn['intent_name'] user_text = last_txn['user_text'] print("last_txn") print(last_txn) input_context = MatchDatabase.get_input_context_from_intent_name( intent_name) print(input_context) session_client = dialogflow_v2.SessionsClient() parameters = dialogflow_v2.types.struct_pb2.Struct() parameters["match_id"] = match_id context_1 = dialogflow_v2.types.context_pb2.Context( name="projects/cricbot-qegqqr/agent/sessions/" + SESSION_ID + "/contexts/" + input_context, lifespan_count=2, parameters=parameters) query_params = {"contexts": [context_1]} session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID) text_input = dialogflow_v2.types.TextInput(text=user_text, language_code="en-us") query_input = dialogflow_v2.types.QueryInput(text=text_input) try: response = session_client.detect_intent(session=session, query_input=query_input, query_params=query_params) except InvalidArgument: raise print("respose from resume:") print(response) print("Query text:", response.query_result.query_text) print("Detected intent:", response.query_result.intent.display_name) print("Detected intent confidence:", response.query_result.intent_detection_confidence) print("Fulfillment text:", response.query_result.fulfillment_text) output = { "fullfillmentText": 'valid', "fulfillmentMessages": [{ "text": { "text": ["Match is On!"] } }] } output['outputContexts'] = [{ "name": "projects/cricbot-qegqqr/agent/sessions/b630631f-19ae-396b-9477-6ba29737d8e8/contexts/ball", "lifespan_count": 5 }] logger.info("output=") logger.info(output) return json.dumps(output)
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