1

I created a webhook using python ( using this example) for my project in api.ai

My code is here :

  #!/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 agent is :

image1 image2

I don't understand where i'm wrong.

My agent work ok, but when i ask the bot to get info from the webhook i get the text response from api.ai not from webhook.

Can you help me please ?

user2454923
  • 49
  • 11

2 Answers2

1

The following is working for me. request.data will be receiving message alerts from the TradingView webhook message.

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

In TradingView Alert Message, I am using following...

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

...and I am getting following message in logs:

$ 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"
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

You are using the wrong url for the webhook in the fulfillment.

Your code is mapping the request to /webhook, while your url is https://your-domain.com/debotwebhook.

Either change @app.route('/webhook', methods=['POST']) to @app.route('/debotwebhook', methods=['POST']) or change the url in api.ai to https://your-domain.com/webhook

Also, I see an extra " in your speech variable after str(parcelinfo[parcelnr]).

Here is the agent I tested with your code and on the right you can see the answer:

enter image description here

  • Hello Tais, thnx for your answer...i try to do the changes and still a can't receive message from the webhook. Do you have another idea ? – user2454923 Mar 27 '17 at 08:20
  • Are you testing in the API.ai? There you can see the JSON of the request that is generating and the answer, can you paste it here? Also, can I see the Entity @parcelnumber? I used your code and was able to make it work, but the only difference was that I used the @sys.number-integer as parcel number instead of the custom one. – Taís Bellini Mar 27 '17 at 13:36
  • I edited the answer to put a printscreen of the agent working and another issue I found in the code. It might be the extra `"` , or some problem in parsing the Entity @parcelnumber. The reason you are getting the API.ai text instead of the one in your code is probably that your webhook is failing at some point. Check the logs of the webhook too. – Taís Bellini Mar 27 '17 at 14:39
  • hello , i made the changes as you said...but :( still i cant get the response . https://bot.api.ai/dd4ffd3b-8cef-41ad-a925-ec40fec385aa – user2454923 Mar 28 '17 at 10:40
  • Are you hosting the webhook on heroku? Can you show me the logs you are getting there? Your webhook is failing for some reason, maybe the logs will give the answer. – Taís Bellini Mar 28 '17 at 11:39
  • 2017-03-28T10:35:06.707380+00:00 heroku[router]: at=info code=H81 desc="Blank app" method=POST path="/webhook" host=botdelivery.herokuapp.com request_id=548791a9-b0a6-47c2-8e0c-19fa75635982 fwd="54.159.203.187" dyno= connect= service= status=502 bytes= protocol=https 2017-03-28T10:38:30.882617+00:00 heroku[router]: at=info code=H81 desc="Blank app" method=POST path="/webhook" host=botdelivery.herokuapp.com request_id=04815fd9-1f70-4553-a42f-00bb72ad6b50 fwd="54.159.203.187" dyno= connect= service= status=502 bytes= protocol=https – user2454923 Mar 28 '17 at 14:32
  • Have you deployed your code to heroku? When requesting to your endpoint I get "Heroku | Welcome to your new app! Refer to the documentation if you need help deploying.". – Taís Bellini Mar 28 '17 at 15:50