From 5453f30f4a894cbfc3fea9283ea089342c609b01 Mon Sep 17 00:00:00 2001 From: Ferrari Lee Date: Sun, 4 Jun 2017 02:00:36 +0800 Subject: [PATCH 1/3] support using filedata parameter to upload file attachment --- lib/Facebook.js | 62 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 78cf8f208..6bb5ac28c 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -77,6 +77,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) { @@ -129,31 +133,47 @@ function Facebookbot(configuration) { //Add Access Token to outgoing request facebook_message.access_token = configuration.access_token; - request({ - method: 'POST', - json: true, - headers: { + var requestObj = {}; + if ('filedata' in facebook_message) { + var prepareFormdata = {}; + for (key in facebook_message && typeof facebook_message[key] === 'object') { + if (key !== 'filedata') { + prepareFormdata[key] = JSON.stringify(facebook_message[key]); + } else { + prepareFormdata[key] = facebook_message[key]; + } + } + requestObj = { + method: 'POST', + formData: prepareFormdata, + uri: 'https://' + api_host + '/v2.6/me/messages' + }; + } else { + requestObj = { + method: 'POST', + json: true, + headers: { 'content-type': 'application/json', - }, - body: facebook_message, - uri: 'https://' + api_host + '/v2.6/me/messages' - }, - function(err, res, body) { - + }, + 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) { From 79df4e6178b361620029364c2468e8ec27384b23 Mon Sep 17 00:00:00 2001 From: Ferrari Lee Date: Sun, 4 Jun 2017 02:15:11 +0800 Subject: [PATCH 2/3] bugfix and add basic filedata validator --- lib/Facebook.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 6bb5ac28c..5024df46c 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -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) { @@ -136,9 +137,13 @@ function Facebookbot(configuration) { var requestObj = {}; if ('filedata' in facebook_message) { var prepareFormdata = {}; - for (key in facebook_message && typeof facebook_message[key] === 'object') { - if (key !== 'filedata') { + for (key in facebook_message) { + if (key !== 'filedata' && typeof facebook_message[key] === 'object') { prepareFormdata[key] = JSON.stringify(facebook_message[key]); + } else if (key === 'filedata') { + if (!fs.existSync(facebook_message[key])) return cb(new Error('filedata not exist')); + + prepareFormdata[key] = fs.createReadStream(facebook_message[key]); } else { prepareFormdata[key] = facebook_message[key]; } From b9bbcfc97854226f7b8b8cbcf702691514e84f6c Mon Sep 17 00:00:00 2001 From: Ferrari Lee Date: Sun, 4 Jun 2017 02:17:07 +0800 Subject: [PATCH 3/3] typo --- lib/Facebook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 5024df46c..9a4591abe 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -141,7 +141,7 @@ function Facebookbot(configuration) { if (key !== 'filedata' && typeof facebook_message[key] === 'object') { prepareFormdata[key] = JSON.stringify(facebook_message[key]); } else if (key === 'filedata') { - if (!fs.existSync(facebook_message[key])) return cb(new Error('filedata not exist')); + if (!fs.existsSync(facebook_message[key])) return cb(new Error('filedata not exist')); prepareFormdata[key] = fs.createReadStream(facebook_message[key]); } else {