【问题标题】:Issue with building a webhook for dialogflow为对话流构建 webhook 的问题
【发布时间】:2017-12-21 16:52:35
【问题描述】:

我正在尝试使用对话流开发谷歌助手操作。我在开发 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": []
    }

我不知道为什么。任何帮助将不胜感激。

【问题讨论】:

  • 您不应该使用已解决来更改帖子的标题,您应该做的是:如果答案可以帮助您解决问题,那么您应该将其标记为正确,如果您有自己的答案然后创建一个并将其标记为正确。

标签: python json webhooks dialogflow-es google-assistant-sdk


【解决方案1】:
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。

【讨论】:

    【解决方案2】:

    更改这些行:

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

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

    为我解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2019-05-16
      • 2020-07-04
      • 2018-11-27
      • 2020-08-15
      • 1970-01-01
      • 2018-11-06
      相关资源
      最近更新 更多