Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive an image in a Facebook Messenger bot

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.

like image 819
Vivek Yadav Avatar asked May 09 '16 13:05

Vivek Yadav


1 Answers

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);
    }
   }
  }      
 }
like image 197
Sritam Avatar answered Oct 04 '22 19:10

Sritam