Python webhook template
Please read more about Aimylogic webhooks in our Help Center.
How to run on Heroku
heroku.com provides a free plan for Python applications. Please click on the button below to run your copy of this webhook on Heroku
Heroku will build and deploy your webhook automatically. After that you have to provide a public URL of your webhook in your bot's settings.
How to upload code changes
Please make the next steps to upload your changes on Heroku.
heroku login heroku git:clone -a <your Heroku application name> cd <your Heroku application name> git remote add origin https://github.com/aimylogic/python-webhook git pull origin master
You have to do these steps only once.
Once you are ready to upload your changes to Heroku, please type
git add . git commit -am "some comments" git push
Heroku will build and deploy your changes automatically.
How to run this template locally in development mode
Run a terminal (or console) and jump into the folder with your copy of this template.
python3 setup.py develop, then
Webhook will be running at localhost
0.0.0.0:5000. To let Aimylogic server send requests to your webhook, you have to share localhost url
0.0.0.0:5000 with, e.g., ngrok. Download ngrok and run it with:
- ngrok.exe http 5000 (windows)
- ./ngrok http 5000
Copy it and paste into the field named "Webhook for tests" in your bot's settings. All requests to your webhook will go to your local machine while you test your bot scenario via a test widget on Aimylogic. This is very useful for rapid development and debugging purposes.
Note that you don't have to restart the local server each time you change any source file. Flask will handle it for you serving the same public URL.
How to use this template
webhook.py file contains all source code you need to change.
Here you can add/remove action handlers for your webhook (please read more about webhook actions on Help Center).
How to add action handler
You can change action behaviour in webhook function inside
def webhook(session): action = session['action'] if action == 'event1': print('Received request from event1 action') session['variable'] = 'some value' return json.dumps(session)
Once the bot steps into the screen with enabled action action1, it calls your webhook and receives variable named variable with value "some value".
How to return multiple variables
Just add variables in session object
def webhook(session): action = session['action'] if action == 'event1': print('Received request from event1 action') session['variable1'] = 'value1' session['variable2'] = 'value2' return json.dumps(session)
This will provide variable1 and variable2 in Aimylogic scenario.
How to handle multiple actions
You can add as many handlers as you need:
def webhook(session): action = session['action'] if action == 'event1': print('Received request from event1 action') session['variable1'] = "value1" elif action == 'event2': print('Received request from event2 action') session['variable2'] = "value2" return json.dumps(session), 200