为 dialogflow 构建 webhook 的问题

Issue with building a webhook for dialogflow

我正在尝试使用 dialogflow 开发一个 google 辅助操作。我 运行 在开发 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)

这对我有用。希望这可以帮助。我也使用 raspberry pi 中的 ngrok。

更改这些行:

port=int(os.getenv('PORT',8080)
app.run(port=port,host='localhost',ssl_context)

app.run(port=8080,host='localhost')

为我解决了这个问题。