0

I'm trying to make a google home assistant that just parrots back whatever a user says to it. Basically I need to capture what the user is saying, and then feed it back into a response.

I have some puzzle pieces figured out.

One is initializing the API to do queries:

api = ApiAi(os.environ['DEV_ACCESS_TOKEN'], os.environ['CLIENT_ACCESS_TOKEN'])

The other is a fallback intent that is intended to just capture whatever the user says and repeat it back:

@assist.action('fallback', is_fallback=True)
def say_response():
    """ Setting the fallback to act as a looper """
    speech = "test this" # <-- this should be whatever the user just said
    return ask(speech)

Another is the JSON response on the API.AI site looks like this:

{
  "id": "XXXX",
  "timestamp": "2017-07-20T14:10:06.149Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "ok then",
    "action": "say_response",
    "actionIncomplete": false,
    "parameters": {},
    "contexts": [],
    "metadata": {
      "intentId": "a452b371-f583-46c6-8efd-16ad9cde24e4",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "true",
      "webhookResponseTime": 112,
      "intentName": "fallback"
    },
    "fulfillment": {
      "speech": "test this",
      "source": "webhook",
      "messages": [
        {
          "speech": "test this",
          "type": 0
        }
      ],
      "data": {
        "google": {
          "expect_user_response": true,
          "is_ssml": true
        }
      }
    },
    "score": 1
  },
  "status": {
    "code": 200,
    "errorType": "success"
  },
  "sessionId": "XXXX"
}

The module I'm intializing from looks like this: https://github.com/treethought/flask-assistant/blob/master/api_ai/api.py

Full program looks like this:

import os
from flask import Flask, current_app, jsonify
from flask_assistant import Assistant, ask, tell, event, context_manager, request
from flask_assistant import ApiAi

app = Flask(__name__)
assist = Assistant(app, '/')

api = ApiAi(os.environ['DEV_ACCESS_TOKEN'], os.environ['CLIENT_ACCESS_TOKEN'])
# api.post_query(query, None)

@assist.action('fallback', is_fallback=True)
def say_response():
    """ Setting the fallback to act as a looper """
    speech = "test this" # <-- this should be whatever the user just said
    return ask(speech)

@assist.action('help')
def help():
    speech = "I just parrot things back!"
    ## a timeout and event trigger would be nice here? 
    return ask(speech)

@assist.action('quit')
def quit():
    speech = "Leaving program"
    return tell(speech)

if __name__ == '__main__':
    app.run(debug=False, use_reloader=False)

How do I go about getting the "resolvedQuery" out of the JSON to be fedback as "speech" for the response?

Thanks.

davidism
  • 121,510
  • 29
  • 395
  • 339
mishap_n
  • 578
  • 2
  • 10
  • 23

2 Answers2

0

The flask_assistant library does a good job of parsing the request into a dict object.

You can get the resolvedQuery by writing:

speech = request['result']['resolvedQuery']
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
0

Just create a new intent (doesn’t matter the name) and a template with sys.any; after that go on your server and use something similar to the following code

userInput = req.get(‘result’).get(‘parameters’).get(‘YOUR_SYS_ANY_PARAMETER_NAME’)

Then send userInput back as the speech response.

Something like this is how you get the initial JSON data:

@app.route(’/google_webhook’, methods=[‘POST’])
def google_webhook():
# Get JSON request
jsonRequest = request.get_json(silent=True, force=True, cache=False)

print("Google Request:")
print(json.dumps(jsonRequest, indent=4))

# Get result 
appResult = google_process_request(jsonRequest)
appResult = json.dumps(appResult, indent=4)

print("Google Request finished")

# Make a JSON response 
jsonResponse = make_response(appResult)
jsonResponse.headers['Content-Type'] = 'application/json'
return jsonResponse