How to Create a Facebook Messenger Bot with Python Flask

The bot-revolution has taken over, and everyone is building a bot. In this post we take you through creating your first bot using Python/Flask. The codebase can be found here on Github.

A lightweight web application needs to be created to compliment our bot.

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
  • Python wrapper of OpenWeatherMap web API for weather reports

We are going to demonstrate a very simple feature :

Enquired about the current weather of a location, chatbot should return the weather information.


Question: What is the current weather of New Delhi?

Reply: Current weather report of New Delhi : max temp: 42.0 min temp: 40.0 current temp: 40.95 wind speed :2.6 humidity 20%

1. Setup 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 add an intent with user expression “What’s the weather in Singapore?”

where Singapore is tagged as an entity of type @sys.geo-city. This parameter can take any value in the conversation and used to determine the response in our application.

We add following response for the user expression: “Current weather report of $geo-city :”

API AI Python client

We can use API AI python SDK for natural language processing.

2. Setup Open Weather Map

Open Weather Map provides API for accessing current weather data of any location on earth. There is nice python wrapper for using these APIs. Register to open weather map and fetch the API key for getting weather data.

If you have a pro subscription for open weather map, then use following:

3. Setting up the Flask Server

Add client access tokens, page access tokens and verify token

Facebook webhook send the request to the application and if the verification is complete if the token returned by application matches the token entered in facebook application

Application will parse all messages sent by the facebook messenger

Call facebook Graph API using python requests library to send message to the facebook user

Use API AI to parse the message sent by the user and recieve the bot response from API AI. Fetch weather data from open weather map client and append the report with bot reponse.

Activate a virtual environment, install all packages

pip install -r requirements.txt

and run the script


Set up tunnelling to localhost

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

and run the ngrok client

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.

Fetch the app ID and update it in the script.

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

Enable webhook integration with callback URL and verify token

and 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.

This post will help you building a chatbot of your own, using API.AI. Feel free to get in touch with us, for any queries/requests.

Leave a Reply

Your email address will not be published. Required fields are marked *