Use Python to send Messages to Facebook Messenger API

Skolo Online Learning
Nerd For Tech
Published in
3 min readApr 21, 2021

We are going to conclude our automation series of tutorials with a Messenger Graph API tutorial. In this tutorial, we will look in to how one can send messages to Facebook Graph Messenger API from a Python App.

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
Use Python to send Messages to Facebook Messenger 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:

touch app.py config.py
nano app.py

Paste this in to the app.py file:

from flask import Flask, requestapp = Flask(__name__)app.config['SECRET_KEY'] = 'enter-your-secret-key-here'@app.route('/', methods=["GET", "POST"])
def home():
return 'HOME'if __name__ == '__main__':
app.run(host='0.0.0.0', 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:

python app.py

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: https://developers.facebook.com/docs/messenger-platform/getting-started/quick-start

Facebook Messenger API Documentation also provides sample code in Javascript, which we will be re-purposing for Python here on GitHub:

Edit your app.py file to look like this:

Use Python to send Messages to Facebook Messenger API

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 config.py file. Call the token PAGE_ACCESS_TOKEN inside the config.py 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 app.py file.

Test your application

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 Tutorial

--

--

Skolo Online Learning
Nerd For Tech

Founder of Tati Digital, passionate about digital transformation and ushering businesses in to the digital age and 4th industrial revolution.