How to receive an image in a Facebook Messenger bot

Vivek Yadav picture Vivek Yadav · May 9, 2016 · Viewed 9.6k times · Source

How can I receive an attachment in form of an image through the Facebook Messenger API? Their documentation only provides instructions on how to receive text-based messages.

Answer

Sritam picture Sritam · May 10, 2016

I am not sure what language you are using to code your bot but since you are referring to the facebook documents where most of the messenger code snippets are in node.js Here's something for you to try, let me know if this helps.

app.post('/webhook/', function (req, res) {
 //Getting the mesagess
 var messaging_events = req.body.entry[0].messaging;
  //Looping through all the messaging events
  for (var i = 0; i < messaging_events.length; i++) {
   var event = req.body.entry[0].messaging[i];
   //Checking for attachments
   if (event.message.attachments) {
    //Checking if there are any image attachments 
    if(atts[0].type === "image"){
     var imageURL = atts[0].payload.url;
     console.log(imageURL);
    }
   }
  }      
 }