Skip to main content

A python library to communicate with the Facebook Messenger API's

Project description

A python library to communicate with the Facebook Messenger API’s


Install from pip

pip install fbmessenger

Facebook app setup

  • Create a page for your app, if you don’t already have one
  • Create an app
  • Add the Messenger product
  • Select the Page to generate a page token

Example usage with Flask

First you need to create a verify token, this can be any string e.g. 'my_verify_token'.

Messenger class

We need to extend the BaseMessenger class and implement methods for each of the following subscription fields.

  • messages
  • message_deliveries
  • messaging_optins
  • messaging_postbacks
from fbmessenger import BaseMessenger

class Messenger(BaseMessenger):
    def __init__(self, verify_token, page_access_token):
        self.verify_token = verify_token
        self.page_access_token = page_access_token
        super(BaseMessenger, self).__init__(self.verify_token,

    def messages(self, message):
        self.send({'text': 'Received: {0}'.format(message['message']['text'])})

    def messages_delivered(self, message):

    def messaging_postbacks(self, messages):

    def messagin_optins(self, messages):

Create a route for the callback url

This can be used to process any messages received and also to verify your app

import os
from flask import Flask, request

app = Flask(__name__)

messenger = Messenger(os.environ.get('FB_VERIFY_TOKEN'), os.environ.get('FB_PAGE_TOKEN'))

def webhook():
    if request.method == 'GET':
        return messenger.verify(request.args.get('hub.verify_token'), request.args.get('hub.challenge'))
    elif request.method == 'POST':
    return ''

if __name__ == "__main__":'')


from fbmessenger import elements


You can pass a simple dict or use the Class

messenger.send({'text': msg})

elem = elements.Text('Your Message')


image = elements.Image(url='')

Web button

btn = elements.Button(title='Web button', url='')

Payload button

To use these buttons you must have the message_deliveries subscription enabled

btn = elements.Button(title='Postback button', payload='payload')

Development Notes

Pydoc should be installed locally to convert the README to reStructuredText format for uploading to PyPi

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for fbmessenger, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size fbmessenger-0.1.0.macosx-10.11-x86_64.tar.gz (8.6 kB) File type Source Python version None Upload date Hashes View
Filename, size fbmessenger-0.1.0-py2.py3-none-any.whl (7.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size fbmessenger-0.1.0.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page