• Stars
    star
    113
  • Rank 308,343 (Top 7 %)
  • Language
    Python
  • License
    Other
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Facebook Messenger

PyPI Build Status Coverage Status PyPI

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

Table of Contents

Installation

Install from pip

pip install fbmessenger

Facebook app setup

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 abstract class and implement methods for each of the following subscription fields.

  • message
  • delivery
  • read
  • optin
  • postback
  • account_linking
from fbmessenger import BaseMessenger


class Messenger(BaseMessenger):
    def __init__(self, page_access_token, app_secret=None):
        self.page_access_token = page_access_token
        self.app_secret = app_secret
        self.client = MessengerClient(self.page_access_token, app_secret=self.app_secret)

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

    def delivery(self, message):
        pass

    def read(self, message):
        pass

    def account_linking(self, message):
        pass

    def postback(self, message):
        pass

    def optin(self, message):
        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__)
app.debug = True

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

@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
    if request.method == 'GET':
        if (request.args.get('hub.verify_token') == os.environ.get('FB_VERIFY_TOKEN')):
            return request.args.get('hub.challenge')
        raise ValueError('FB_VERIFY_TOKEN does not match.')
    elif request.method == 'POST':
        messenger.handle(request.get_json(force=True))
    return ''


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

Timeouts

Any method on either the BaseMessenger or MessengerClient classes which perform network access accept an optional timeout parameter. This should be a number, and causes an exception to be raised if the server (ie. Facebook) has not started responding within timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out. Note that in particular, timeout is not a time limit on the entire response download - just the initial connection.

For example, this call will raise a socket timeout exception if the start of a response has not been received within 10 seconds:

messenger.send({'text': msg}, 'RESPONSE', timeout=10)

If no timeout is provided (the default) then connection attempts will not time out.

Elements

Import the elements (or just the ones you need)

from fbmessenger import elements

Messaging type

Starting from 7th May 2018, Facebook requires that all message sends must include the messaging_type property:

https://developers.facebook.com/docs/messenger-platform/reference/send-api

This is passed in the send() calls below - in each case, we'll just use RESPONSE. You should use whatever value is appropriate for your application. Supported values are:

  • RESPONSE (default)
  • UPDATE
  • MESSAGE_TAG

See Messaging Types for more information.

Notification Type

Any of the elements below may be sent in conjunction with a notification type (see the Send API documentation for more details). notification_type is an optional parameter to the .send() call. For example:

messenger.send({'text': msg}, 'RESPONSE', notification_type='REGULAR')

Supported values are are:

  • REGULAR (default)
  • SILENT_PUSH
  • NO_PUSH

Message Tags

Message tags give you the ability to send messages to a person outside of the normally allowed 24-hour window for a limited number of purposes that require continual notification or updates.

messenger.send({'text': msg}, 'MESSAGE_TAG', tag='NON_PROMOTIONAL_SUBSCRIPTION')

Supported values are are:

  • BUSINESS_PRODUCTIVITY
  • COMMUNITY_ALERT
  • CONFIRMED_EVENT_REMINDER
  • NON_PROMOTIONAL_SUBSCRIPTION
  • for more see Supported Tags

See Message Tags for more information.

Text

You can pass a simple dict or use the Class

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

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

Web button

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

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(), 'RESPONSE')

Attachments

You can upload attachments to Facebook for use in their other APIs:

attachment = attachments.Image(url='https://example.com/image.jpg')
client = MessengerClient(page_access_token=12345678)
res = client.upload_attachment(attachment)
print(res)
{"attachment_id": "12345"}

Images

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

Audio

audio = attachments.Image(url='http://example.com/audio.mp3')
messenger.send(audio.to_dict(), 'RESPONSE')

Video

video = attachments.Video(url='http://example.com/video.mp4')
messenger.send(video.to_dict(), 'RESPONSE')

Files

file = attachments.File(url='http://example.com/file.txt')
messenger.send(file.to_dict(), 'RESPONSE')

Templates

Import the templates (or just the ones you need)

from fbmessenger import templates

Generic template

btn = elements.Button(title='Web button', url='http://facebook.com')
elems = elements.Element(
    title='Element',
    item_url='http://facebook.com',
    image_url='http://facebook.com/image.jpg',
    subtitle='Subtitle',
    buttons=[
        btn
    ]
)
res = templates.GenericTemplate(elements=[elems])
messenger.send(res.to_dict(), 'RESPONSE')

Button template

btn = elements.Button(title='Web button', url='http://facebook.com')
btn2 = elements.Button(title='Postback button', payload='payload')
res = templates.ButtonTemplate(
    text='Button template',
    buttons=[btn, btn2]
)
messenger.send(res.to_dict(), 'RESPONSE')

Receipt template

element = elements.Element(
    title='Classic White T-Shirt',
    subtitle='100% Soft and Luxurious Cotton',
    quantity=2,
    price=50,
    currency='USD',
    image_url='http://petersapparel.parseapp.com/img/whiteshirt.png',
)
adjustment1 = elements.Adjustment(name='New Customer Discount', amount=20)
adjustment2 = elements.Adjustment(name='$10 Off Coupon', amount=10)
address = elements.Address(
    street_1='1 Hacker Way',
    city='Menlo Park',
    postal_code='94025',
    state='CA',
    country='US'
)
summary = elements.Summary(
    subtotal=75.00,
    shipping_cost=4.95,
    total_tax=6.19,
    total_cost=56.14
)
res = templates.ReceiptTemplate(
    recipient_name='Stephane Crozatier',
    order_number='12345678902',
    currency='USD',
    payment_method='Visa 2345',
    order_url='http://petersapparel.parseapp.com/order?order_id=123456',
    timestamp='1428444852',
    address=address,
    summary=summary,
    adjustments=[adjustment1, adjustment2],
    elements=[element]
)
messenger.send(res.to_dict(), 'RESPONSE')

Media template

btn = elements.Button(
    button_type='web_url',
    title='Web button',
    url='http://facebook.com'
)
attachment = attachments.Image(attachment_id='12345')
res = templates.MediaTemplate(attachment, buttons=[btn])
messenger.send(res.to_dict())

Sender Actions

Typing on

typing_on = SenderAction(sender_action='typing_on')
messenger.send_action(typing_on.to_dict())

Typing off

typing_ffn = SenderAction(sender_action='typing_off')
messenger.send_action(typing_off.to_dict())

Mark seen

mark_seen = SenderAction(sender_action='mark_seen')
messenger.send_action(mark_seen.to_dict())

Quick Replies

quick_reply_1 = QuickReply(title='Do something', payload='Send me this payload')
quick_reply_2 = QuickReply(title='Do something else', payload='Send me this other payload')
quick_replies = QuickReplies(quick_replies=[
	quick_reply_1,
	quick_reply_2
])
text = { text: 'A message' }
text['quick_replies'] = quick_replies.to_dict()
messenger.send(text, 'RESPONSE')

Messenger Profile

Greeting Text

from fbmessenger.thread_settings import GreetingText, MessengerProfile

greeting_text = GreetingText('Welcome to my bot')
messenger_profile = MessengerProfile(greetings=[greeting_text])
messenger.set_messenger_profile(messenger_profile.to_dict())

Get Started Button

from fbmessenger.thread_settings import GetStartedButton, MessengerProfile

get_started = GetStartedButton(payload='GET_STARTED')
messenger_profile = MessengerProfile(get_started=get_started)
messenger.set_messenger_profile(messenger_profile.to_dict())

You can then check for this payload in the postback method

Persistent Menu

from fbmessenger.thread_settings import PersistentMenu, PersistentMenuItem, MessengerProfile

menu_item_1 = PersistentMenuItem(item_type='web_url', title='Menu Item 1', url='https://facebook.com')
menu_item_2 = PersistentMenuItem(item_type='postback', title='Menu Item 2', payload='PAYLOAD')

menu = PersistentMenu(menu_items=[menu_item_1, menu_item_2])

messenger_profile = MessengerProfile(persistent_menus=[menu])
messenger.set_messenger_profile(messenger_profile.to_dict())

More Repositories

1

docker-gunicorn-nginx

An experimental docker setup for Python / Gunicorn / Nginx stack
Nginx
102
star
2

blink-detect

JavaScript
17
star
3

fe-skeleton

The official front-end skeleton used by rehabstudio.
JavaScript
16
star
4

vagrant-python-appengine

A vagrant box that sets up a Google Appengine/Python environment for local development purposes
Shell
10
star
5

animixer

Generate and return generated animal to google assistant using firebase
Jupyter Notebook
6
star
6

ludumdare-32

'Gradience' - our team entry to the Ludum Dare weekend game jam - April 2015
JavaScript
5
star
7

vagrant-puppet-cakephp

A vagrant box based on puphpet that has some customisations and supports setup of the FriendsOfCake/app-template project
Puppet
5
star
8

docker-autoproxy

A simple Go application that runs alongside nginx in a docker container and can automatically configure the server (nginx) to provide reverse proxy functionality for any containers running on the same host.
Go
5
star
9

docker-autodns

Simple inter-container DNS for containers on a single docker host.
ApacheConf
4
star
10

oneill

oneill is a small tool that manages a set of docker containers running on a single host.
Go
4
star
11

dockerfiles

Dockerfiles for automated building of public images on Docker Hub
Python
4
star
12

docker-alpine-node-npm-gulp

lightweight alpine, node, npm & gulp container
Shell
3
star
13

vagrant-php-nginx-mysql-cake

Ubuntu PHP/MySQL/Nginx vagrant box with cake dependencies preloaded
Puppet
3
star
14

oneill-server

Ansible roles that allow the user to bootstrap, provision and manage an oneill controlled docker host on a supported platform.
ApacheConf
3
star
15

app-deployer

Ruby gem for app deployment. Has tasks for various frameworks, databases and web servers including CakePHP, Lithium, MySQL, Nginx and Apache. Uses railsless deploy
Ruby
2
star
16

rehab-rip

Gathers up image elements in the DOM denoted with a particular class and hides them until their source has been determined and preloaded.
JavaScript
2
star
17

vagrant-php-nginx-mysql

Ubuntu PHP/MySQL/Nginx vagrant box with some other common libraries preloaded
Puppet
2
star
18

wit-js

A modern Node wrapper for the Wit.ai API using Promises and Events.
JavaScript
2
star
19

docker-appengine-djangae

Djangae on App Engine for Docker
Python
1
star
20

docker-registry-gce

Provision a new private docker registry on GCE
1
star
21

docker-multiprovisioning-template

Bare bones Docker setup, demonstrating the ability to have varying provisions from a single Dockerfile, depending on the ENV vars passed during `docker run`
Shell
1
star
22

jira-rehabstudioproject

A jira plugin that allows you to create a 'rehabstudio' project type
Java
1
star
23

fe-skeleton-testsuite

A wrapper for the test suite of the rehabstudio front-end skeleton.
JavaScript
1
star
24

devdocs

rehabstudio Developersโ€™ Documentation
Python
1
star
25

puppet-capistrano

Simple capistrano submodule for vagrantboxes repo
Puppet
1
star
26

tina-the-trex-website

React/Redux app that communicates with the Tina the T. rex chatbot
JavaScript
1
star
27

rehabstudio.github.com

1
star