Skip to main content

A Python Library For Using The Facebook Messenger Platform API

Project description

# FBMQ (Facebook Messenger Platform Python Library)
[![PyPI](https://img.shields.io/pypi/v/fbmq.svg?v=1&maxAge=3600)](https://pypi.python.org/pypi/fbmq)
[![PyPI](https://img.shields.io/pypi/l/fbmq.svg?v=1&maxAge=2592000)](https://pypi.python.org/pypi/fbmq)

A Python Library For Using The Facebook Messenger Platform API (Python Facebook Chat & Chatbot Library)
Facebook messenger platform api full features are supported

# Install
```
pip install fbmq
```

# Handle Webhook

## Usage (with flask)
```
from flask import Flask, request
import fbmq

@app.route('/webhook', methods=['POST'])
def webhook():
payload = request.get_data(as_text=True)
fbmq.handle_webhook(payload,
optin=optin_handler,
message=message_handler,
echo=echo_handler,
delivery=delivery_handler,
postback=postback_handler,
read=read_handler,
account_linking=account_linking_handler)
return "ok"

def message_handler(event):
sender_id = event['sender']['id']
message = event['message']

page = fbmq.Page(PAGE_ACCESS_TOKEN)
page.send(sender_id, "thank you! your message is '%s'" % message)
```


# Request

## Setting

```
from fbmq import Attachment, Template, QuickReply, Page

page = Page(PAGE_ACCESS_TOKEN)
```

## Basic

#### Text
```
page.send(recipient_id, "hello world!")
```


#### Image (jpg, png, gif support)
```
page.send(recipient_id, Attachment.Image(image_url))
```


#### Audio
```
page.send(recipient_id, Attachment.Audio(audio_url))
```

#### Video
```
page.send(recipient_id, Attachment.Video(video_url))
```


#### File
```
page.send(recipient_id, Attachment.File(file_url))
```



#### quick reply
```
quick_replies = [
QuickReply(title="Action", payload="PICK_ACTION"),
QuickReply(title="Comedy", payload="PICK_COMEDY")
]

page.send(recipient_id,
"What's your favorite movie genre?",
quick_replies=quick_replies,
metadata="DEVELOPER_DEFINED_METADATA")
```

you can use dict instead of QuickReply class
```
quick_replies = [{'title': 'Action', 'payload': 'PICK_ACTION'},
{'title': 'Comedy', 'payload': 'PICK_COMEDY'}}
```



#### typing on/off
```
page.typing_on(recipient_id)
page.typing_off(recipient_id)
```



## Templates

#### Template : Button
```
buttons = [
Attachment.ButtonWeb("Open Web URL", "https://www.oculus.com/en-us/rift/"),
Attachment.ButtonPostBack("trigger Postback", "DEVELOPED_DEFINED_PAYLOAD"),
Attachment.ButtonPhoneNumber("Call Phone Number", "+16505551234")
]
page.send(recipient_id, Template.Buttons("hello", buttons))
```
you can use dict instead of Button class
```
buttons = [{'type': 'web_url', 'title': 'Open Web URL', 'value': 'https://www.oculus.com/en-us/rift/'},
{'type': 'postback', 'title': 'trigger Postback', 'value': 'DEVELOPED_DEFINED_PAYLOAD'},
{'type': 'phone_number', 'title': 'Call Phone Number', 'value': '+16505551234'}]
```


#### Template : Generic
```
page.send(recipient_id, Template.Generic([
Template.GenericElement("rift",
subtitle="Next-generation virtual reality",
item_url="https://www.oculus.com/en-us/rift/",
image_url=CONFIG['SERVER_URL'] + "/assets/rift.png",
buttons=[
Template.ButtonWeb("Open Web URL", "https://www.oculus.com/en-us/rift/"),
Template.ButtonPostBack("tigger Postback", "DEVELOPED_DEFINED_PAYLOAD"),
Template.ButtonPhoneNumber("Call Phone Number", "+16505551234")
]),
Template.GenericElement("touch",
subtitle="Your Hands, Now in VR",
item_url="https://www.oculus.com/en-us/touch/",
image_url=CONFIG['SERVER_URL'] + "/assets/touch.png",
buttons=[
Template.ButtonWeb("Open Web URL", "https://www.oculus.com/en-us/rift/"),
Template.ButtonPostBack("tigger Postback", "DEVELOPED_DEFINED_PAYLOAD"),
Template.ButtonPhoneNumber("Call Phone Number", "+16505551234")
])
]))
```


#### Template : Receipt
```
element = Template.ReceiptElement(title="Oculus Rift",
subtitle="Includes: headset, sensor, remote",
quantity=1,
price=599.00,
currency="USD",
image_url=CONFIG['SERVER_URL'] + "/assets/riftsq.png"
)

address = Template.ReceiptAddress(street_1="1 Hacker Way",
street_2="",
city="Menlo Park",
postal_code="94025",
state="CA",
country="US")

summary = Template.ReceiptSummary(subtotal=698.99,
shipping_cost=20.00,
total_tax=57.67,
total_cost=626.66)

adjustment = Template.ReceiptAdjustment(name="New Customer Discount", amount=-50)

fbpage.send(recipient_id, Template.Receipt(recipient_name='Peter Chang',
order_number='1234',
currency='USD',
payment_method='Visa 1234',
timestamp="1428444852",
elements=[element],
address=address,
summary=summary,
adjustments=[adjustment]))
```



# Example

1. fill example/config.py
2. run server
```
cd example
virtualenv env
source env/bin/activate
pip install -r requirements.txt
python server.py
```

![](./example/assets/screen2.jpg)
![](./example/assets/screen3.jpg)
![](./example/assets/screen4.jpg)
![](./example/assets/screen5.jpg)
![](./example/assets/screen6.jpg)
![](./example/assets/screen7.jpg)
![](./example/assets/screen8.jpg)
![](./example/assets/screen9.jpg)
![](./example/assets/screen10.jpg)

Project details


Download files

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

Source Distribution

fbmq-1.5.0.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file fbmq-1.5.0.tar.gz.

File metadata

  • Download URL: fbmq-1.5.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fbmq-1.5.0.tar.gz
Algorithm Hash digest
SHA256 4666b29488d2589f29b448d544ba9a2398a199ff2e05f425ace7842f7b701921
MD5 48d1c6eedb66b1bf76cbfcdfd9a966fa
BLAKE2b-256 1482086ec41539d958bf305f18c5290d878edaed5daa300e52c3dc6761edaa3d

See more details on using hashes here.

Supported by

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