1

我在 api.ai 中为我的项目使用 python(使用此示例)创建了一个 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 代理是:

图片 1 图片2

我不明白我错在哪里。

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

你能帮我吗 ?

4

2 回答 2

1

以下对我有用。request.data将从TradingViewwebhook 消息接收消息警报。

@app.route('/webhook', methods=['POST'])
def webhook():
    print("\nrequest.data={}".format(request.data))
    return request.data

TradingView警报消息中,我正在使用以下...

exchange = {{exchange}}, ticker = {{ticker}}, price = {{close}}, volume = {{volume}}

...并且我在日志中收到以下消息:

$ heroku logs --tail
2022-01-12T20:38:00.510006+00:00 app[web.1]: request.data=b'exchange = BINANCE, ticker = BTCUSDT, price = 43671.85, volume = 10.70612'
2022-01-12T20:38:00.510512+00:00 app[web.1]: 10.1.58.75 - - [12/Jan/2022:20:38:00 +0000] "POST /webhook HTTP/1.1" 200 73 "-" "Go-http-client/1.1"
于 2022-01-12T20:40:05.760 回答
0

您在履行中为 webhook 使用了错误的 url。

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

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

"此外,我在您的语音变量中看到了一个额外的str(parcelinfo[parcelnr]).

这是我用你的代码测试的代理,在右边你可以看到答案:

在此处输入图像描述

于 2017-03-24T21:04:21.523 回答