Automate Facebook Messenger using Graph API and Python

In this tutorial we are going to cover how you can automate facebook messenger using Graph API and Python.

The points we will cover in this tutorial are:

  • Create our Application — Flask
  • Set-up Facebook Messenger webhook inside our application
  • Host our application on HTTPS
  • Write code for receiving messages from our webhook and send a response back to Facebook
  • Add Messenger permissions to our Facebook App, get access tokens
  • Test our application
Automate Facebook Messenger using Facebook Graph API

Create our Flask Application

First we create a python project directory and virtual environment, run the following commands on the command line:

mkdir messenger && cd messenger
virtualenv messengerenv
source messengerenv/bin/activate

Create the files we need to work with:


Paste this in to the file:

from flask import Flask, request
app = Flask(__name__)
app.config['SECRET_KEY'] = 'enter-your-secret-key-here'
@app.route('/', methods=["GET", "POST"])
def home():
    return 'HOME'
if __name__ == '__main__':'', port='8888', debug=True)

This is a minimal flask application that will just display the words ‘HOME’ on the home-page.

You can run the application and test that is working:


Then visit localhost:8888 or your-ip-address:8888 (Port 8888).

Set up Facebook Messenger Webhook inside our application

We will be following the start-up tutorial from Facebook Messenger API documentation available here:

Facebook Messenger API Documentation also provides sample code in Javascript, which we will be re-purposing for Python here on GitHub:fbsamples/messenger-platform-samples
Messenger Platform samples for sending and receiving messages. Walk through the Get Started with this code…

Edit your file to look like this to automate facebook messenger:

Host our application on HTTPS

To host the application on HTTPS — you need a hosting service, there is a complete video on youtube you can follow with all the details: Hosting a Python Flask Application on Digital Ocean.

Add Messenger App and Get Page Access Token

We need to get a Facebook application that has the correct messenger permissions and access token to complete the process.

Go to the facebook developer page and create an app. The go to settings > basic and select messenger from the grid of apps. Click set-up to continue.

Set up Messenger API access tokens

Select “add new page” and go through the authorisation by logging in to your account and selecting the page you want to use. 

Click on generate token — copy it and paste it inside the file. Call the token PAGE_ACCESS_TOKEN inside the file as a string. 

Generate page access token for Messenger API

Then click the button at the bottom to enter the callback url and verify string. Your webhook app should be up and running by now on a https server for this o work without a problem.

Enter the url — https://your-host-domain/webook and enter the exact same verify token you have inside your file.

Test your application for automating facebook messenger

You can now go to the Facebook Page you connected and test the application, note that the app will not work for any other users other than the owner of the Facebook page and registered developers — until you take your app through the Facebook App Verification Approval Process. 

Full Video to Automate Facebook Messenger

Leave a Reply

Your email address will not be published.

Subscribe to our mailing list

And get this 💃💃 Free😀 eBook !

Holler Box