Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Support fb send upload #870

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var request = require('request');
var express = require('express');
var bodyParser = require('body-parser');
var crypto = require('crypto');
var fs = require('fs');

function Facebookbot(configuration) {

Expand Down Expand Up @@ -77,6 +78,10 @@ function Facebookbot(configuration) {

if (message.attachment) {
facebook_message.message.attachment = message.attachment;

if (message.filedata) {
facebook_message.filedata = message.filedata;
}
}

if (message.sticker_id) {
Expand Down Expand Up @@ -129,31 +134,51 @@ function Facebookbot(configuration) {
//Add Access Token to outgoing request
facebook_message.access_token = configuration.access_token;

request({
method: 'POST',
json: true,
headers: {
'content-type': 'application/json',
},
body: facebook_message,
uri: 'https://' + api_host + '/v2.6/me/messages'
},
function(err, res, body) {
var requestObj = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about initializing requestObject with POST method and uri to avoid duplication ?

var requestObj = {
      method: 'POST',
      uri: 'https://' + api_host + '/v2.6/me/messages'
};

if ('filedata' in facebook_message) {
var prepareFormdata = {};
for (key in facebook_message) {
if (key !== 'filedata' && typeof facebook_message[key] === 'object') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code is really awesome, but what do you think about starting with checking for filedata to avoid a slightly duplication ?

if (key === 'filedata') {
   if (!fs.existsSync(facebook_message[key])) return cb(new Error('filedata not exist'));
       prepareFormdata[key] = fs.createReadStream(facebook_message[key]);
   } else if (typeof facebook_message[key] === 'object') {
       prepareFormdata[key] = JSON.stringify(facebook_message[key]);
   } else {
       prepareFormdata[key] = facebook_message[key];
   }

prepareFormdata[key] = JSON.stringify(facebook_message[key]);
} else if (key === 'filedata') {
if (!fs.existsSync(facebook_message[key])) return cb(new Error('filedata not exist'));

prepareFormdata[key] = fs.createReadStream(facebook_message[key]);
} else {
prepareFormdata[key] = facebook_message[key];
}
}
requestObj = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you initialised the requestObj with the http verbe POST and the uri, you just need to set here the formData :

requestObj.formData = prepareFormdata;

method: 'POST',
formData: prepareFormdata,
uri: 'https://' + api_host + '/v2.6/me/messages'
Copy link
Contributor

@jonchurch jonchurch Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't used the upload api myself, but the docs say the url for uploads is:
v2.6/me/message_attachments

Ahh, misunderstood the purpose of this PR! Carry on 😉

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonchurch you made me think to bring the functionality that you misunderstood to Botkit 😈

If you don't already start to work on 😬

};
} else {
requestObj = {
method: 'POST',
json: true,
headers: {
'content-type': 'application/json',
},
body: facebook_message,
uri: 'https://' + api_host + '/v2.6/me/messages'
};
}

if (err) {
botkit.debug('WEBHOOK ERROR', err);
return cb && cb(err);
}
request(requestObj, function(err, res, body) {
if (err) {
botkit.debug('WEBHOOK ERROR', err);
return cb && cb(err);
}

if (body.error) {
botkit.debug('API ERROR', body.error);
return cb && cb(body.error.message);
}
if (body.error) {
botkit.debug('API ERROR', body.error);
return cb && cb(body.error.message);
}

botkit.debug('WEBHOOK SUCCESS', body);
cb && cb(null, body);
});
botkit.debug('WEBHOOK SUCCESS', body);
cb && cb(null, body);
});
};

bot.startTyping = function(src, cb) {
Expand Down