A basic scaffold for high performance Facebook messenger bot in Sanic : Async web server that's written to go fast
Sanic is very high performance JSON serialization, so it is very suitable for facebook webhook. Please refer the benchmark and related test source codes Techempower
- uvloop : which is a fast, drop-in replacement of the built-in asyncio event loop. uvloop is implemented in Cython and uses libuv under the hood.
- aiohttp : which is an HTTP client/server for asyncio.
- webhook
Steps to get up and running:
-
Create a new facebook App .https://developers.facebook.com/quickstarts/?platform=web
-
Create a new facebook Page https://www.facebook.com/pages/create
-
Go to your app and to the messengers tab on the sidebar
-
Generate a token for your page.
-
Edit your webhook configuration
Verify Token is any string you like to help your webhook app confirm or authenticate the source.
I adopt environment variables to inject the PAGE_TOKEN and VERIFY_TOKEN for security, and easy to port on the docker environment.
$ pip install -r requirment.txt
$ VERIFY_TOKEN=<your verfiy token> PAGE_ACCESS_TOKEN=<your page token> python -m app.py
the module support 5 parameters for non web server like gunicorn or nginx. Since the webhook in Facebook needs SSL, you must run with CERT.
--host
-p, --port the app running port.
-d, --debug enable debug mode.
--cert, put your cert,
--key, your cert passphrase.
https://blog.signifai.io/not-your-fathers-python-amazing-powerful-frameworks/
While you try to test a webhook on your own circumstance, you might encounter callback issues. Callback function always needs two requirements:
- Domain name
- SSL (HTTPS)
The testing procedure would like this.
- coding on the local site, and debuging with experience.
- upload toward server
- On your chatbot platform, check if the req-ack work well
- Check log message on server
- If there's an error, go back to step 1
Ngrok is an antidote for this situation. It provides localhost toward https with domain name such as localhost:5000 to https://bbcc3.ngrok.com
you can go visiting https://ngrok.com/, and check how you can exploit it well.