Facebook Messenger Bot, send Image Attachment

Carpetfizz picture Carpetfizz · Apr 17, 2016 · Viewed 14.5k times · Source

I'm creating a Facebook bot that has images uploaded to it, and it responds with an image. Can I send the image in an attachment and delete it off my server or do I have to send a URL to the image and keep the image on my server?

Answer

Alvin Reyes picture Alvin Reyes · Jun 25, 2017

You can use their Upload API to upload your attachments to their servers.

curl -X POST -H "Content-Type: application/json" -d '{
  "message":{
    "attachment":{
      "type":"image", 
      "payload":{
        "url":"http://www.messenger-rocks.com/image.jpg", 
        "is_reusable":true,
      }
    }
  }
}' "https://graph.facebook.com/v2.6/me/message_attachments?access_token=PAGE_ACCESS_TOKEN"

The upload call will respond back an attachment_id which can be used to send the attachment to the user without uploading it again.

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient": {
    "id": "USER_ID"
  },
  "message": {
    "attachment": {
      "type": "image",
      "payload": {
        "attachment_id": "1745504518999123"
      }
    }
  }
}' "https://graph.facebook.com/me/messages?access_token=PAGE_ACCESS_TOKEN"