Skip to main content

Python API client for FB Messenger

Project description


Minimum Requirements

  • Python 2.7+ or Python 3.3+

Install from pip

pip install fb_messenger

Build from source

git clone
cd fb_messenger
python install

How to develop

make install # create virtualenv folder and install requirements
make lint # run pylint
make test # run tests
make clean # remove virtualenv folder

How to Use

Send simple text

from fb_messenger.client import FBMessenger
from fb_messenger import attachments
from fb.messenger.exceptions import MessengerAPIError

client = FBMessenger(ACCESS_TOKEN, logger_level=logging.DEBUG)
text = attachments.Text('hello!')

    response = client.send_attachment(RECIPIENT_ID, text)
except MessengerAPIError as e:

Send attachment

More examples look into examples folder.

image = attachments.Image('')

    response = client.send_attachment(RECIPIENT_ID, text)
except MessengerAPIError as e:

Send action

from fb_messenger.types import action_types

    response = client.send_action(RECIPIENT_ID, action_types.MARK_SEEN)
except MessengerAPIError as e:

How to process messages in Flask

from flask import Flask, request
import logging
from fb_messenger.client import FBMessenger
from fb_messenger.types import webhook_types

app = Flask(__name__)

LOGGER = logging.getLogger(__name__)

client = FBMessenger(ACCESS_TOKEN, logger_level=logging.DEBUG)

@app.route('/webhook', methods=['GET'])
def get_webhook():
    if request.args.get('hub.verify_token', '') == VERIFY_TOKEN:
        return request.args.get('hub.challenge', '')

    return 'Error', 400

@app.route('/webhook', methods=['POST'])
def post_webhook():
    except Exception as e:

    return 'ok'

def message_received(webhook):
    :type webhook: fb_messenger.webhooks.MessageReceived
    LOGGER.debug((webhook_types.MESSAGE_RECEIVED, webhook))

def postback_received(webhook):
    :type webhook: fb_messenger.webhooks.Postback
    LOGGER.debug((webhook_types.POSTBACK_RECEIVED, webhook))

def authentication(webhook):
    :type webhook: fb_messenger.webhooks.Authentication
    LOGGER.debug((webhook_types.AUTHENTICATION, webhook))

def account_linking(webhook):
    :type webhook: fb_messenger.webhooks.AccountLinking
    LOGGER.debug((webhook_types.ACCOUNT_LINKING, webhook))

def message_delivered(webhook):
    :type webhook: fb_messenger.webhooks.MessageDelivered
    LOGGER.debug((webhook_types.MESSAGE_DELIVERED, webhook))

def message_read(webhook):
    :type webhook: fb_messenger.webhooks.MessageRead
    LOGGER.debug((webhook_types.MESSAGE_READ, webhook))

if __name__ == '__main__':, host='')

Download files

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

Source Distribution

fb_messenger-0.3.0.tar.gz (8.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page