Messenger Bot using Flask and API.AI

Rahul Nayak
YML Innovation Lab
Published in
4 min readDec 15, 2017
ChatBot Service

The bot-revolution has taken over, and everyone is building a bot.

In this post, I take you through creating a messenger bot using python/flask.

The different technologies that we are going to use are as follows :

Server backend: Flask

Secure tunneling to localhost: Ngrok

Natural Language Processing platform: API.AI

Deployment on Facebook Messenger

Setting up API.AI

API.AI is a conversational experiences platform. Register and create an account at API.AI. Create an agent to access test console. Navigate to settings of the agent and get the Client Access Token.

Creating an intent

An intent is mapping between user expression and the desired response. We can train which user input fits to which intent. We can also use built-in parameters within the user input and set context. This helps us categorise the user inputs.

Setting up the Flask Server

Add client access token, page access token and verify token. A page has to be created for the bot. That page access token is needed. Verify token can be anything as per developer choice.

CLIENT_ACCESS_TOKEN = 'your_client_access_token_from_api_ai'
PAGE_ACCESS_TOKEN = 'your_facebook_page_access_token'
VERIFY_TOKEN = 'verification_token_for_facebook_chatbot'

Webhooks

Facebook webhook sends the request to the application and if the verification is complete i.e. the token returned by application matches the token entered in facebook application, request comes to the application server.

All messages sent by the facebook messenger are received on the application server and are handled.

Parse message

Application server receives the message but the intent is yet to be recognised. So, we use python library apiai to get the intent. To use apiai, client access token is needed.

API.AI parses the message sent by the user and receives the bot response from API.AI which contains the intent along with other information.

If everything is alright, response[‘status’][‘code’] should be 200.

response[‘result’][‘metadata’][‘intentName’] contains the intent.

After knowing the intent, we can do actions on it according to our requirements.

We can not only parse texts but can also parse events. We can also set context and assign session id to the request.

Sending back message to Facebook user

To send message to the facebook user, call Facebook Graph API using python requests library.

sender_id is present in the request body.

Make a flask app which contains the above routes (webhooks) and methods.

Activate a virtual environment, install all depedencies and run the flask app.

$ virtualenv .env
$ pip install -r requirments.txt
$ python flask_app.py

Set up tunnelling to localhost

We can use ngrok to expose local webserver to the internet so that it can be used for callback verification needed to be done for using a webhook with facebook app.

$ wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip (for Linux)
$ wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-darwin-amd64.zip (for Mac OS)
$ unzip /path/to/ngrok.zip
$ ./ngrok http <port>

Ngrok port should be same as the port to which the flask app is listening.

Please note that a secure callback URL (https) is needed for verification.

Set up facebook messenger

Create a facebook page and facebook app. Add Webhook to the app.

Add Messenger to the app and generate token for the page which has to used for chat.

This page access token is used in the flask application to send messages back to the facebook user.

Enable webhook integration with callback URL and verify token

The callback url should be the ngrok url and the verify token should be same as that defined in the flask app.

Select events for page subscription

Restart the server and now the chatbot is ready. Facebook further provides UI elements, you can play around with them here.

--

--