我试图使用dialogflow开发谷歌助理动作。我在开发webhook时遇到了一些麻烦。我正在使用python。这是代码:
import json
import os
import urllib
from flask import Flask
from flask import request
from flask import make_response
app=Flask(__name__)
@app.route('/webhook',methods=['POST'])
def webhook():
req = request.get_json(silent=True,force=True)
print("Request:")
print(json.dumps(req,indent=4))
res={
"speech": "Complete",
"displayText": "Complete",
"source": "Myself"
}
res=json.dumps(res,indent=4)
r=make_response(res)
r.headers['Content-Type']='application/json'
return r
if __name__ == '__main__':
port=int(os.getenv('PORT',8080))
app.run(port=port,host='localhost',ssl_context='adhoc')
问题是脚本返回的JSON对象始终为空。使用ngrok,我在对象的履行键中得到了类似的东西:
"fulfillment": {
"speech": "",
"messages": []
}
我无法弄清楚原因。任何帮助,将不胜感激。
from flask import Flask, request, jsonify
app = Flask(__name__)
base_response = {
'speech':"sample response",
'source' : 'Manual'}
@app.route('/',methods=['GET','POST'])
def index():
if request.method == 'GET':
text = """WELCOME to RBG<br>
/testing -> red testing<br>"""
return text
else:
req_body = request.get_json()
print(req_body)
response = base_response.copy()
return jsonify(response)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True)
这对我有用。希望这可以帮助。我也从我的树莓派中使用了ngrok。
Changline这些线:
port=int(os.getenv('PORT',8080)
app.run(port=port,host='localhost',ssl_context)
至
app.run(port=8080,host='localhost')
为我解决了这个问题。