构建用于对话流的webhook的问题

时间:2017-12-21 16:52:35

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

我正在尝试使用对话框流程开发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": []
    }

我无法弄清楚原因。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

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。

答案 1 :(得分:0)

长线这些线:

import Ember from 'ember';

export default Ember.Route.extend({
  async model() {
    let record;
    record = await this.store.queryRecord('record', { queryParams });

    if (!record) {
        record = this.store.createRecord('record', { properties });
    }

    return record.save();
  },

});

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

为我解决了这个问题。