Exemplo n.º 1
0
 def user_stats_listener(username, source):
     user_id = BotDatabase.userid_from_username(username, source)
     if user_id == None:
         return None
     user_detail = BotDatabase.user_stats(user_id)
     print("user_stats********")
     print(str(user_detail))
     return Message.get_user_stats_payload(user_detail)
Exemplo n.º 2
0
 def get_match_params(request):
     match_params = {}
     source = ''
     match_id = ''
     username = ''
     start_date = ''
     if 'source' in request['originalDetectIntentRequest']:
         source = request['originalDetectIntentRequest']['source']
     # else:
     #     return json.dumps(Message.get_invalid_request_payload())
     #have to update for other platforms
     if source == 'telegram':
         if "from" in request['originalDetectIntentRequest']['payload'][
                 'data'] and "username" in request[
                     'originalDetectIntentRequest']['payload']['data'][
                         "from"]:
             username = request['originalDetectIntentRequest']['payload'][
                 'data']['from']['username']
         start_date = request['originalDetectIntentRequest']['payload'][
             'data']['date']
         if username != '' and username[:1] == '@':
             username = username[1:]
         if username != '':
             match_id = BotDatabase.userid_from_username(username, source)
     else:
         match_id = "test"
         start_date = ''
     match_params["username"] = username
     match_params["match_id"] = match_id
     match_params["start_date"] = start_date
     match_params["source"] = source
     if request['queryResult']['queryText'] in exit_set:
         match_params['exit'] = Helper.clear_contexts(match_id, request)
     if request['queryResult']['queryText'].lower() == 'undo':
         match_params['undo'] = True
     return match_params
Exemplo n.º 3
0
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)