Facebook bot for a local movie theater
This bot utilizes Facebook Messanger API in order to provide a representation of services of a local movie theater, namely, it provides customers with a way to navigate through menus representing different movies that the movie theater is currently showing, as well as announcements of the upcoming movies. The bot can be accessed via messaging: https://www.facebook.com/portalcinemabot/
Requirements:
-
Django is used as a main-framework for the bot.
-
Heroku in a combination with gunicorn is used as a remote hosting for the bot.
-
Django-heroku is used in order to easily configure the settings of our Django project for Heroku.
-
Psycopg is a Heroku requirement for Postgres.
-
APScheduler is used for timed jobs.
-
Django-extensions is used for its "shell_plus", because it provides us with an easy way to execute our timed job script (APScheduler) through our manage.py.
-
The Requests library is used to query the Facebook API and to parse the front-page of the movie theater website.
Setting it all up
In order to set this bot up and running we'll need a Facebook application and a page that represents it. After that, we'll need to fill in the required values in our "portal_const.py" that is located in the parser folder of our "fb_bot" Django application.
- access_token stands for the token generated by Facebook API in order to access a specific page you are managing.
- verify_token is a token you'll need to provide in order to verify the webhook connection between Facebook API and our application.
- webhook_url is a url of our webhook that is combined with our website.domain/bot/.
- allowed_host must be a url your bot is hosted on, use this value only if you are going to make use of ngrok or a similar service. If you are using heroku, 'allowed_host' can remain an empty value.
- *test_id is an id of a Facebook user that will be used when running tests locally
After setting up your heroku application (there are numerous guides on doing this all over the Internet) with our Procfile. We'll need to send a POST request to Facebook API in order to enable the "get started" button. That way, when the user if messaging our bot for the first time, they do not even have to type in the dummy message and instead can just click on "get started". The way to do it using cURL:
curl -X POST -H "Content-Type: application/json" -d '{
"get_started":{
"payload":"GET_STARTED_PAYLOAD"
}
}' "https://graph.facebook.com/v2.6/me/messenger_profile?access_token=PAGE_ACCESS_TOKEN"
Unfortunately, Facebook generates this button only for the first time a certain person is trying to message the bot, so if the user wants to access the starting menu again after that, they will have to either click on a previous message's "get back" button or type in a dummy message.
The last thing to do is to tell Heroku to run an instance of our clock application (which refreshes the datebase once every 12 hours) and in order to do so we must enter a command to the terminal (while the terminal is currently cd'ed in the root folder of the application): heroku ps:scale clock=1
Running tests locally
Since running tests on Heroku is very troublesome, before deploying new functionality you may want to run tests locally. In order to do that, you need to expose your local server via a public API. Commonly used free tool for such purposes is ngrok.
First, we need to get our test user id, it is easily achievable by running your bot and sending it a message. In your log, you should see: recipient_id: <your_facebook_id>
- Install ngrok and run it. Example:
./ngrok http 8000
- Fill in all the required values in portal_const.py, including
allowed_host
andtest_id
, which should be your ngrok url. Example:allowed_host = <randomly_generated_sequence>.ngrok.io test_id = <your_facebook_id>
. - Connect your Facebook app to your webhook.
- Run
python manage.py test
and you should see 5 OK tests as well as bunch of test messages in your Facebook inbox.
All tools used and their versions
These are the tools that were used for initially creating the bot, as well as their versions:
- Python - 3.6
- APScheduler - 3.5.1
- Django - 2.0.6
- Django Extensions - 2.0.7
- django-heroku - 0.3.1
- gunicorn - 19.8.1
- psycopg2 - 2.7.4
- psycopg2-binary - 2.7.4
- requests - 2.18.4
- Facebok API - 3.0