2017-07-20 38 views
0

我正试图让一个谷歌家庭助理,只是鹦鹉回来无论用户对它说。基本上我需要捕捉用户在说什么,然后将它反馈回响应中。如何访问JSON以鹦鹉返回用户响应?

我有一些拼图想通了。

One初始化的API来完成查询:

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

另一种是意在刚刚捕获任何用户说,重复回后备意图:

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

另一个是API.AI站点上的JSON响应如下所示:

{ 
    "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" 
} 

m odule我是从长相intializing这样的:https://github.com/treethought/flask-assistant/blob/master/api_ai/api.py

全部程序是这样的:

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) 

我如何去获得了“resolvedQuery”出来的JSON待fedback为“讲话”为响应?

谢谢。

回答

0

flask_assistant库在将请求解析为dict对象方面做得很好。

你可以得到resolvedQuery通过写

speech = request['result']['resolvedQuery'] 
+0

这正是我需要的。非常感谢。 –

0

只需创建一个新的意图(无关紧要的名称),并与sys.any的模板;之后,在你的服务器上使用类似于以下代码的东西

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

然后发送userInput作为语音响应。

像这样的事情是你如何获得初始的JSON数据:

@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