Skip to main content

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

Project description

https://travis-ci.com/rehabstudio/fbmessenger.svg?token=GC74DPVqhupkfZm2TAsz&branch=master

https://travis-ci.com/rehabstudio/fbmessenger.svg?token=GC74DPVqhupkfZm2TAsz&branch=master

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

Installation

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,
                                            self.page_access_token)

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

    def messages_delivered(self, message):
        pass

    def messaging_postbacks(self, messages):
        pass

    def messagin_optins(self, messages):
        pass

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'))

@app.route('/webhook')
def webhook():
    if request.method == 'GET':
        return messenger.verify(request.args.get('hub.verify_token'), request.args.get('hub.challenge'))
    elif request.method == 'POST':
        messenger.handle(request.get_json(force=True))
    return ''

if __name__ == "__main__":
    app.run(host='0.0.0.0')

Elements

from fbmessenger import elements

Text

You can pass a simple dict or use the Class

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

elem = elements.Text('Your Message')
messenger.send(elem.to_dict())

Images

image = elements.Image(url='http://example.com/image.jpg')
messenger.send(image.to_dict())

Web button

btn = elements.Button(title='Web button', url='http://example.com')
messenger.send(btn.to_dict())

Payload button

To use these buttons you must have the message_deliveries subscription enabled

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

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.

Source Distributions

fbmessenger-0.1.0.tar.gz (5.3 kB view hashes)

Uploaded Source

fbmessenger-0.1.0.macosx-10.11-x86_64.tar.gz (8.6 kB view hashes)

Uploaded Source

Built Distribution

fbmessenger-0.1.0-py2.py3-none-any.whl (7.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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