0

I am developing FB bot on GAE and one of the function of my bot is sending images. I want to attach my image and send it to user (not via link).

The problem is I get error: recipient must be specified, but actually i have specified it:

from requests_toolbelt import MultipartEncoder
from google.appengine.api import urlfetch

def send_image(recipient_id):
payload = {
    'recipient': json.dumps(
        {
            'id': recipient_id
        }
    ),
    'message': json.dumps(
        {
            'attachment': {
                'type': 'image',
                'payload': {}
            }
        }
    ),
    'filedata': (path, open(path, 'rb'))
}
multipart_data = MultipartEncoder(payload)
multipart_header = {
    u'Content-Type': multipart_data.content_type
}

result = urlfetch.fetch(
    url=request_endpoint,
    payload=multipart_data,
    method=urlfetch.POST,

    headers=multipart_header,
    deadline=10)

return result.content

result.response:

'{"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"CIGds8u22au"}}'
michaelborisov
  • 113
  • 1
  • 1
  • 7
  • Can you print out the result.content? Perhaps the recipient_id is empty or the message is malformed. Can you send the same message with the link model? – Matthew Fisher Sep 09 '16 at 13:53
  • @MatthewFisher, thanks for your response. I have edited the question. And I have checked that multipart_data has non-empty field recepient. recepient = '{"id": "value"}'. I do not want to use links, because i generate pictures programmatically, so in this case I will need some hosting. – michaelborisov Sep 09 '16 at 14:46
  • Well that doesn't tell us much. How about printing the multipart_data? Can send other messages? – Matthew Fisher Sep 09 '16 at 14:49
  • @MatthewFisher, https://snag.gy/NTA1hv.jpg I suppose that problem is inappropriate header, but it looks OK, – michaelborisov Sep 09 '16 at 15:07
  • It would be nice to get the plain text of what the program is actually sending on the wire. Perhaps there is a debug output for 'urlfetch'. I'd like to compare it against the example herehttps://developers.facebook.com/docs/messenger-platform/send-api-reference/image-attachmen The filedata field looks strange to me. I expected a MIME encoded block of text. Can you try it with the link style? It would be good to understand if the recipient is the actual issue or if the error message is misleading. – Matthew Fisher Sep 09 '16 at 15:17

1 Answers1

0

To send images, you could use Attachment Upload Api. The process is simple. You upload the image as attachment by providing the image link and get the attachment ID. Then send user the attachment by using that attachment ID. Reference: Attachment Upload Api

Azizul Hakim
  • 154
  • 5