Last active
August 8, 2017 13:08
-
-
Save akmalxxx/d7c090068a68b8b7238bd85374b6e34c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from requests_toolbelt import MultipartEncoder | |
def send_attachment(page_token, recipient_id, filetype, path): # filetype = image, audio, video, file | |
payload = { | |
'recipient':{ | |
{ 'id':recipient_id } | |
}, | |
'message':{ | |
{ | |
'attachment':{ 'type':filetype, 'payload':{} } | |
} | |
}, | |
'filedata':(os.path.basename(path), open(path, 'rb')) | |
} | |
multipart_data = MultipartEncoder(payload) | |
return requests.post('https://graph.facebook.com/v2.6/me/messages', | |
params={ 'access_token':page_token }, | |
headers={ 'Content-Type': multipart_data.content_type }, | |
data=multipart_data).json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Thanks for your gist.
I have a question though. When you write:
'recipient':{ { 'id':recipient_id } },
How can it be read by python? This object looks invalid.
I've tried to reuse it myself, but it's failing (Python 3x). (And I'm very interested in how to send an image to a Facebook bot)