PyMessager is a Python API for Facebook Messenger and a sample project to demonstrate how to develop a chatbot on Facebook Messenger.


Complete tutorials are on Develop a Facebook Bot Using Python and Chatbot: from 0 to 1 where you can find more detailed information to setup and develop.

Before Starting

  1. Prepare a Facebook Page. (to create one if you don’t have)
  2. Create a developer application on Facebook for Developers.
  3. Start a python project, and install the required packages and modules: Flask, Requests.
  4. Use Let’s Encrypt to apply SSL certification for your domain name.


To install PyMessager, simply run:

$ pip install pymessager

or install from the repository:

$ git clone
$ cd PyMessager
$ pip install -r requirements.txt

Get Started


from pymessager.message import Messager, ... # something else you need


You can initialize a messager client via a Facebook Access Token from the developer console:

from pymessager.message import Messager
client = Messager(config.facebook_access_token)

Receiver APIs

The following code is used to build a message receiver, there are three main steps to prepare for your bot:

  1. Setup the Webhook
@app.route(API_ROOT + FB_WEBHOOK, methods=["GET"])
def fb_webhook():
verification_code = 'I_AM_VERIFICIATION_CODE'
verify_token = request.args.get('hub.verify_token')
if verification_code == verify_token:
return request.args.get('hub.challenge')
view raw hosted with ❤ by GitHub
  1. Receive the message
@app.route(API_ROOT + FB_WEBHOOK, methods=['POST'])
def fb_receive_message():
message_entries = json.loads('utf8'))['entry']
for entry in message_entries:
for message in entry['messaging']:
if message.get('message'):
print("{sender[id]} says {message[text]}".format(**message))
return "Hi"
  1. Start the server with https
if __name__ == '__main__':
context = ('ssl/fullchain.pem', 'ssl/privkey.pem')'', debug=True, ssl_context=context)
view raw hosted with ❤ by GitHub

Sender APIs


There are several types of message: textimagequick repliesbutton template or generic template. API provides different classes to generate the message template.

Sending a text and image

Send a simple text or an image to a recipient, just make sure that image URL is a valid link.

client.send_text(recipient_id, "Hello, I'm enginebai.")
client.send_image(recipient_id, "https://image-url.jpg")

Quick Replies

The QuickReply(title, payload, image_url, content_type) class defines a present buttons to the user in response to a message.

Parameter Description Required
title The button title Y
payload The click payload string Y
The icon image URL N
content_type TEXT or LOCATION Y
client.send_quick_replies(recipient_id, "Help", [
         QuickReply("Projects", Intent.PROJECT),
         QuickReply("Blog", Intent.BLOG),
         QuickReply("Contact Me", Intent.CONTACT_ME)

Button Template

The ActionButton(button_type, title, url, payload) class defines button template which contains a text and buttons attachment to request input from the user.

Parameter Description Required
button_type WEB_URL or POSTBACK Y
title The button title Y
url The link Only if button_type is url
payload The click payload string Only if button_type is POSTBACK
client.send_buttons(recipient_id, "You can find me with below", [
    ActionButton(ButtonType.WEB_URL, "Blog", ""),
	ActionButton(ButtonType.POSTBACK, "Email", Intent.EMAIL)

Generic Template

The GenericElement(title, subtitle, image_url, buttons) class defines a horizontal scrollable carousel of items, each composed of an image attachment, short description and buttons to request input from the user.

Parameter Description Required
title_text The message main title Y
subtitle_text The message subtitle, leave it empty if you don’t need it N
button_list The list of ActionButton Y
project_list = []
for project_id, project in projects.items():
        config.api_root + project["image_url"], [
                         # Payload use Intent for the beginning
                + project_id)
client.send_generic(recipient_id, project_list)

Utility APIs

Subscribe the pages

Before your chatbot starts to receive messages, you have to subscribe the application to your chatbot page. To subscribe a page, just call it:


Set the welcome message and get-started button


The greeting text will show at the first time you open this chatbot on mobile only. The payload is the trigger when the users click “Get Started” button.

client.set_greeting_text("Hi, this is Engine Bai. Nice to meet you!")
client.set_get_started_button_payload("HELP")  # Specify a payload string.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑