This wrapper makes it simpler to user the Facebook Messenger platform wrapping the endpoints as functions.
For exemple, to send a text message to the user you can easily do:
from fbbotw import fbbotw
# ...
user_fbid = "<user fb id>"
my_message = "Hello World"
fbbotw.post_text_message(fbid=user_fbid, message=my_message)
# The user with the specified fbid will receive the text 'Hello World'
This is much less code than the traditional way to call the Send API
and send a text. Using requests
and json
you probably would do like this:
fbid = "<user psid>"
message = "Hello World"
url = 'https://graph.facebook.com/v2.6/me/messages?access_token={0}'
url = url.format(PAGE_ACCESS_TOKEN)
header = {"Content-Type": "application/json"}
payload = {}
payload['recipient'] = {'id': fbid}
payload['message'] = {'text': message}
data = json.dumps(payload)
response = requests.post(url=url, headers=header, data=data)
Learn more about the fbbotw methods by reading the DOCS
pip install fbbotw
The only configuration needed is to set the PAGE_ACCESS_TOKEN
with
the value you got from the facebook app dashboard. If you are using Django, create the variable in your settings.py
. If not, define the variable in your enviroment:
1 - In your settings.py
define the variable PAGE_ACCESS_TOKEN
that was
generated on the app configuration from facebook.
#settings.py
PAGE_ACCESS_TOKEN = "<your access token>"
Create an os environment variable called PAGE_ACCESS_TOKEN
:
export PAGE_ACCESS_TOKEN='<your access token>'
After setting the access token, just import and use fbbotw
methods:
from fbbotw import fbbotw
fbbotw.post_sender_action(fbid="<user psid>", sender_action="typing_on")
See the documentation to learn about the methods provided by the package.