2017-07-20 70 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