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 get_last_txn_from_history(match_id, status=None): print("status from get_last_txn_from_history:") print(status) if status == 'resume': BotDatabase.set_match_status(match_id, from_status="resume", to_status="live") last_txn = BotDatabase.get_last_txn(match_id) return last_txn
def exit_conversation(match_id, request): # session = request['session'] # SESSION_ID = session.rpartition('/')[2] BotDatabase.set_match_status(match_id, from_status="live", to_status="end") exit_payload = Message.exit_payload() if not 'outputContexts' in request['queryResult']: return exit_payload return Helper.append_clear_context_payload(exit_payload, request)
def pause_match_listner(match_id, username, request): success = BotDatabase.set_match_status(match_id=match_id, from_status="live", to_status="pause") if not success: return json.dumps(Message.get_invalid_request_payload()) else: #exit_payload = ActionListener.exit_listner(match_id,request) pause_payload = Message.get_match_pause_payload(username) pause_payload = Helper.append_clear_context_payload( pause_payload, request) return json.dumps(pause_payload)
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