无法从webhook

时间:2017-03-24 16:17:25

标签: python bots webhooks dialogflow facebook-webhooks

我使用python(使用此示例)为api.ai中的项目创建了一个webhook

我的代码在这里:

  #!/usr/bin/env python

import urllib
import json
import os

from flask import Flask
from flask import request
from flask import make_response

# Flask app should start in global layout
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 = makeWebhookResult(req)

res = json.dumps(res, indent=4)
print(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r

def makeWebhookResult(req):
if req.get("result").get("action") != "delivery.info":
    return {}
result = req.get("result")
parameters = result.get("parameters")
parcelnr = parameters.get("parcelnumber")

parcelinfo = {'5000':'your parcel has been shipped', '5001':'your parcel has 
not been shipped', '5002':'should be delivered on three days', '5003':'your 
parcel has not been shipped', '5004':'your parcel has been shipped'}

speech = "Parcel with numner" + parcelnr + " is " + 
str(parcelinfo[parcelnr]) "

print("Response:")
print(speech)

return {
    "speech": speech,
    "displayText": speech,
    #"data": {},
    # "contextOut": [],
    "source": "apiai-debot"
}


if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))

print "Starting app on port %d" % port

app.run(debug=True, port=port, host='0.0.0.0')

Api代理是:

image1 image2

我不明白我哪里错了。

我的代理人工作正常,但是当我要求机器人从webhook获取信息时,我从api.ai获得文本回复而不是来自webhook。

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您在履行中使用了错误的网址。

您的代码将请求映射到/webhook,而您的网址为https://your-domain.com/debotwebhook

@app.route('/webhook', methods=['POST'])更改为@app.route('/debotwebhook', methods=['POST'])或将api.ai中的网址更改为https://your-domain.com/webhook

此外,我在"之后的语音变量中看到了额外的str(parcelinfo[parcelnr])

以下是我使用您的代码测试的代理,右侧您可以看到答案:

enter image description here