Python framework for quick, simple, and dynamic generation of Facebook Messenger bots.
While developing a Messenger bot for a charity in the Greater Toronto Area, we realized how Facebook Messenger was a powerful outlet for small businesses and charities to converse with their customers/users. As well, we realized that there was a semi-standard approach to writing the logic for Messenger bots with a sufficient amount of functionality (i.e. postback and message list support).
Thinking that it would be useful and cost-efficient for small businesses and charities to use a black box which dynamically generated Messenger bots, we began working towards developing a tool/engine/API for bot creation through simple JSON specification.
The engine and associated process currently exists as a set of bash scripts and Python modules which can be configured (via parameters) to generate a Facebook Messenger bot. Due to cost limitations, there exists a single database (and collection) for data storage.
The next iteration of this project would include packaging the engine and template modules into a Flask application server, and deploying said server on a remote machine (e.g. thin EC2 instance, Amazon Lightsail). The deployed server would expose a simple API allowing JSON passing, while generating subprocesses for handling the generated files and deploying the bot to Heroku.
In order to use the bot-engine, the following machine dependencies are required.
-
Valid Python distribution with pip. It is recommended that a virtual environment is created to box the python dependencies.
-
Verified Facebook Developer account.
-
Heroku installed with the login credentials specified. You will need space for a new application to be deployed to Heroku.
# log into Heroku
heroku login
# check existing apps
heroku apps
# destroy a running application
heroku apps:destroy --app <application_name> --confirm <application_name>
# view the logs for a running application
heroku logs --app <application_name>
-
Clone this repository to your local or remote machine. Navigate to the root directory of the repository.
-
Install the dependencies through the following command.
pip install -r requirements.txt
-
Navigate to https://www.facebook.com/pages/create/
-
Choose an appropriate category corresponding to your page. For testing use Cause or Community.
-
Go through the prescribed setup process (if required).
-
Navigate to the Facebook Developer quickstart page: https://developers.facebook.com/quickstarts/?platform=web
-
Click Skip and Create Application ID.
-
Specify your 'Application Name', 'Contact Email', and 'Category' (e.g. Communication).
-
Enter the captcha.
-
Within Product Setup, click Get Started next to Messenger. You will be redirected to the Messenger Product page.
-
Under Token Generation, select your previously created page.
-
Enable all mandatory permissions, in case a prompt appears.
-
Add the outputted 'Page Access Token' to your clipboard.
-
Modify the bot-config.json file with your desired configuration. The existing content should be used as a template/example. Please note that the configuration file must follow valid JSON format.
-
Modify evars.sh to include the newly retrieved 'Page Access Token'.
-
Specify a custom 'Verification Token' in evars.sh.
-
In order for the engine to work, it must connect to a valid mongo instance. Please email [email protected] to retrieve the database url and appropriate credentials. Once the url is obtained, specify the 'Mongo Host' in evars.sh.
-
Run the run.sh bash script and retrieve the outputted web url. This bash script will wipe all collections in the database (dev + testing feature which will be removed in production).
cd src
bash run.sh
-
Navigate to the previously opened Messenger Product page.
-
Click Setup Webhooks within the Webhooks section.
-
Specify the previously outputted application url under 'Callback URL'.
-
Specify the custom verification token under 'Verify Token'.
-
Click the messages, message_deliveries, messaging_optins, messaging_postbacks, and message_reads options under 'Subscription Fields'.
-
Under Webhooks, choose your previously created page and click Subscribe such that the page subscribes to your newly created webhook.
- Navigate to your page on Facebook and click Message to begin communicating with your bot.
Note: At this point your deployed application will only communicate with you through your Facebook page, as it is in a development stage. In order to deploy this application to the public you must follow these instructions.
-
Navigate to App Review within your application on the Facebook Developers page.
-
Turn the switch on under 'Make <application_name> public?'.