A python library to communicate with the Facebook Messenger API's
Project description
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
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())
Development Notes
Pydoc should be installed locally to convert the README to reStructuredText format for uploading to PyPi
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for fbmessenger-0.1.0.macosx-10.11-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36f241e568b66f4b175ecc1ae39f33a424556277dfbe185be523bc7e6ab5b126 |
|
MD5 | 923ba8999db3ba7cb46bf8b5957acb05 |
|
BLAKE2b-256 | 74f357d6dc4b3334a00d8932f525f672ca5a1413c4f63a94befc634428323b2a |
Hashes for fbmessenger-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f26908243be6dee9f93f6b6271f0804da9895b46a1114045d1fe5ae9e5c6484d |
|
MD5 | 3b7dda85c07d06a06ebc246fe30c3844 |
|
BLAKE2b-256 | a1c12145797744ecbed0d6d926151e35b6b1ddf070e18d9989f0c42694c9e099 |