Skip to content

Instantly share code, notes, and snippets.

@akmalxxx
Last active August 8, 2017 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akmalxxx/d7c090068a68b8b7238bd85374b6e34c to your computer and use it in GitHub Desktop.
Save akmalxxx/d7c090068a68b8b7238bd85374b6e34c to your computer and use it in GitHub Desktop.
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()
@antisrdy
Copy link

antisrdy commented Aug 8, 2017

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment