chen-ye / hubot-fb

A Hubot adapter for the Facebook Messenger Platform
https://www.npmjs.com/package/hubot-fb
87 stars 21 forks source link

support for sending image(file) #9

Open kengz opened 8 years ago

kengz commented 8 years ago

Currently hubot-fb supports sending images via urls. Here's the guideline of sending image (file).

Will this be supported?

chen-ye commented 8 years ago

already supported! see https://github.com/chen-ye/hubot-fb#sending-rich-messages-templates-images

kengz commented 8 years ago

sorry was a bit vague bout that. I meant to ask about sending image via filedata, as in

curl  \
  -F recipient='{"id":"USER_ID"}' \
  -F message='{"attachment":{"type":"image", "payload":{}}}' \
  -F filedata=@/tmp/testpng.png \
  "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
chen-ye commented 8 years ago

Thats supported via the attachment payload as well - check the data recieved when an image is sent from the user to the bot

chen-ye commented 8 years ago

I tested using

robot.on "fb_richMsg", (envelope) -> 
    res = new Response robot, envelope, undefined
    res.envelope.fb = {
      richMsg: {
        attachment: envelope.attachments[0]
      }
    }
    res.send()

Which echos back rich mesages

kengz commented 8 years ago

ohh thx, will try later, let's close for now.

kengz commented 8 years ago

update: the method you showed above still works only for sending url of image, not the buffer stream of your local file. For example, run image transformation from facebook: take image from user, transform to output, send that file back to people.

Luckily this can be done by plain HTTP request now:

var formData = {
  recipient: `{"id":"${envelope.user.id}"}`,
  message: '{"attachment":{"type":"image", "payload":{}}}',
  filedata: fs.createReadStream(outFilepath)
}
request.post({url:'https://graph.facebook.com/v2.6/me/messages?access_token='+process.env.FB_PAGE_TOKEN, formData: formData});
chen-ye commented 8 years ago

Ok! I'm currently on a weekend vacation, but I can take a look at adding this functionality Tuesday. Do you know how other adapters which support file upload do this at the api level? On May 15, 2016 18:47, "Wah Loon Keng" notifications@github.com wrote:

update: the method you showed above still works only for sending url of image, not the buffer stream of your local file. For example, run image transformation from facebook: take image from user, transform to output, send that file back to people.

Luckily this can be done by plain HTTP request now:

var formData = { recipient: {"id":"${envelope.user.id}"}, message: '{"attachment":{"type":"image", "payload":{}}}', filedata: fs.createReadStream(outFilepath) }request.post({url:'https://graph.facebook.com/v2.6/me/messages?access_token='+process.env.FB_PAGE_TOKEN, formData: formData});

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/chen-ye/hubot-fb/issues/9#issuecomment-219293418

kengz commented 8 years ago

no worries, I'm using that script above now. yep. hubot-telegram