From 4f8fcc170a0f035ed165dc2ad4877fd74cf97837 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Sun, 10 Jul 2016 16:01:43 -0400 Subject: [PATCH 1/5] Post using JSON and newer endpoint to support Emojis. --- lib/Facebook.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index feabbf7ee..d2d39c025 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -47,32 +47,35 @@ function Facebookbot(configuration) { facebook_message.message.quick_replies = message.quick_replies; } - request.post('https://graph.facebook.com/me/messages?access_token=' + configuration.access_token, + //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://graph.facebook.com/v2.6/me/messages' + }, function(err, res, body) { - if (err) { - botkit.debug('WEBHOOK ERROR', err); - return cb && cb(err); - } - - try { - var json = JSON.parse(body); - } catch (err) { - - botkit.debug('JSON Parse error: ', err); + if (err) { + botkit.debug('WEBHOOK ERROR', err); return cb && cb(err); - } - if (json.error) { + if (body.error) { botkit.debug('API ERROR', json.error); return cb && cb(json.error.message); } botkit.debug('WEBHOOK SUCCESS', body); cb && cb(null, body); - }).form(facebook_message); + }) }; bot.reply = function(src, resp, cb) { From 1b603e67fdf745bede5b73b176a2c3d30c43d90e Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Sun, 10 Jul 2016 17:38:01 -0400 Subject: [PATCH 2/5] Needed to update old references. --- lib/Facebook.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index d2d39c025..1d4ecdac6 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -69,8 +69,8 @@ function Facebookbot(configuration) { } if (body.error) { - botkit.debug('API ERROR', json.error); - return cb && cb(json.error.message); + botkit.debug('API ERROR', body.error); + return cb && cb(body.error.message); } botkit.debug('WEBHOOK SUCCESS', body); From f448e9a61f46afd94e66cb20d2423f3f069726f2 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Tue, 12 Jul 2016 00:11:59 -0400 Subject: [PATCH 3/5] Added Facebook config API methods. --- lib/Facebook.js | 181 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 162 insertions(+), 19 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 1d4ecdac6..f16c07616 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -52,14 +52,14 @@ function Facebookbot(configuration) { facebook_message.access_token = configuration.access_token; request({ - method: "POST", - json: true, - headers: { - "content-type": "application/json", + method: "POST", + json: true, + headers: { + "content-type": "application/json", + }, + body: facebook_message, + uri: 'https://graph.facebook.com/v2.6/me/messages' }, - body: facebook_message, - uri: 'https://graph.facebook.com/v2.6/me/messages' - }, function(err, res, body) { @@ -215,6 +215,149 @@ function Facebookbot(configuration) { return facebook_botkit; }; + facebook_botkit.persistentMenu = function(cb) { + + var buttons = configuration.persistent_menu && configuration.persistent_menu.buttons; + + if (buttons) { + request({ + uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token, + json: true, + method: "POST", + headers: { + "content-type": "application/json", + }, + body: { + "setting_type": "call_to_actions", + "thread_state": "existing_thread", + "call_to_actions": .map(function(button) { + return { + "type": "postback", + "title": button.title, + "payload": button.payload + } + }): [] + } + }, function(err, res, body) { + + if (err) { + facebook_botkit.error(err); + cb(err); + } else { + facebook_botkit.log(body.result); + cb(); + } + + }); + } else { + return cb(); + } + + }; + + facebook_botkit.greetingText = function(cb) { + + var greetingText = configuration.greeting && configuration.greeting.text; + + if (greetingText) { + + request({ + uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token, + json: true, + method: "POST", + headers: { + "content-type": "application/json", + }, + body: { + "setting_type": "greeting", + "greeting": { + "text": configuration.greeting.text + } + } + }, function(err, res, body) { + + if (err) { + facebook_botkit.error(err); + return cb(err); + } else { + facebook_botkit.log(body.result); + return cb(); + } + + }); + + } else { + return cb(); + } + + }; + + facebook_botkit.greetingPayload = function(cb) { + + var payload = configuration.greeting && configuration.greeting.payload; + + if (payload) { + + request({ + uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token, + json: true, + method: "POST", + headers: { + "content-type": "application/json", + }, + body: { + "setting_type": "call_to_actions", + "thread_state": "new_thread", + "call_to_actions": [{ + "payload": payload + }] + } + }, function(err, res, body) { + + if (err) { + facebook_botkit.error(err); + return cb(err); + } else { + facebook_botkit.log(body.result); + return cb && cb(); + } + + }); + } else { + return cb() + } + + }; + + facebook_botkit.reactivateFacebook = function(cb) { + + request.post("https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=" + configuration.access_token, + function(err, res, body) { + + if (err) { + facebook_botkit.error(err); + return cb && cb(err); + } else { + + var isSuccess = JSON.parse(body).success; + + if (isSuccess) { + + facebook_botkit.log("Successfully subscribed to Facebook events"); + + return cb && cb(); + + } else { + facebook_botkit.error("Could not subscribe to page messages."); + return cb("Could not subscribe to page messages."); + } + + } + + }); + + }; + facebook_botkit.setupWebserver = function(port, cb) { if (!port) { @@ -224,7 +367,7 @@ function Facebookbot(configuration) { throw new Error('Specified port is not a valid number'); } - var static_dir = __dirname + '/public'; + var static_dir = __dirname + '/public'; if (facebook_botkit.config && facebook_botkit.config.webserver && facebook_botkit.config.webserver.static_dir) static_dir = facebook_botkit.config.webserver.static_dir; @@ -233,7 +376,9 @@ function Facebookbot(configuration) { facebook_botkit.webserver = express(); facebook_botkit.webserver.use(bodyParser.json()); - facebook_botkit.webserver.use(bodyParser.urlencoded({ extended: true })); + facebook_botkit.webserver.use(bodyParser.urlencoded({ + extended: true + })); facebook_botkit.webserver.use(express.static(static_dir)); var server = facebook_botkit.webserver.listen( @@ -241,19 +386,17 @@ function Facebookbot(configuration) { function() { facebook_botkit.log('** Starting webserver on port ' + facebook_botkit.config.port); - if (cb) { cb(null, facebook_botkit.webserver); } + if (cb) { + cb(null, facebook_botkit.webserver); + } }); - request.post('https://graph.facebook.com/me/subscribed_apps?access_token=' + configuration.access_token, - function(err, res, body) { - if (err) { - facebook_botkit.log('Could not subscribe to page messages'); - } else { - facebook_botkit.debug('Successfully subscribed to Facebook events:', body); - facebook_botkit.startTicking(); - } - }); + facebook_botkit.reactivateFacebook(); + facebook_botkit.greetingText() + facebook_botkit.greetingPayload(); + facebook_botkit.persistentMenu(); + facebook_botkit.startTicking(); return facebook_botkit; From b826db5ec48b3e33dd875bb111248260e5637990 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Tue, 12 Jul 2016 00:16:19 -0400 Subject: [PATCH 4/5] Revert "Added Facebook config API methods." This reverts commit f448e9a61f46afd94e66cb20d2423f3f069726f2. --- lib/Facebook.js | 181 +++++------------------------------------------- 1 file changed, 19 insertions(+), 162 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index f16c07616..1d4ecdac6 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -52,14 +52,14 @@ function Facebookbot(configuration) { facebook_message.access_token = configuration.access_token; request({ - method: "POST", - json: true, - headers: { - "content-type": "application/json", - }, - body: facebook_message, - uri: 'https://graph.facebook.com/v2.6/me/messages' + method: "POST", + json: true, + headers: { + "content-type": "application/json", }, + body: facebook_message, + uri: 'https://graph.facebook.com/v2.6/me/messages' + }, function(err, res, body) { @@ -215,149 +215,6 @@ function Facebookbot(configuration) { return facebook_botkit; }; - facebook_botkit.persistentMenu = function(cb) { - - var buttons = configuration.persistent_menu && configuration.persistent_menu.buttons; - - if (buttons) { - request({ - uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token, - json: true, - method: "POST", - headers: { - "content-type": "application/json", - }, - body: { - "setting_type": "call_to_actions", - "thread_state": "existing_thread", - "call_to_actions": .map(function(button) { - return { - "type": "postback", - "title": button.title, - "payload": button.payload - } - }): [] - } - }, function(err, res, body) { - - if (err) { - facebook_botkit.error(err); - cb(err); - } else { - facebook_botkit.log(body.result); - cb(); - } - - }); - } else { - return cb(); - } - - }; - - facebook_botkit.greetingText = function(cb) { - - var greetingText = configuration.greeting && configuration.greeting.text; - - if (greetingText) { - - request({ - uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token, - json: true, - method: "POST", - headers: { - "content-type": "application/json", - }, - body: { - "setting_type": "greeting", - "greeting": { - "text": configuration.greeting.text - } - } - }, function(err, res, body) { - - if (err) { - facebook_botkit.error(err); - return cb(err); - } else { - facebook_botkit.log(body.result); - return cb(); - } - - }); - - } else { - return cb(); - } - - }; - - facebook_botkit.greetingPayload = function(cb) { - - var payload = configuration.greeting && configuration.greeting.payload; - - if (payload) { - - request({ - uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token, - json: true, - method: "POST", - headers: { - "content-type": "application/json", - }, - body: { - "setting_type": "call_to_actions", - "thread_state": "new_thread", - "call_to_actions": [{ - "payload": payload - }] - } - }, function(err, res, body) { - - if (err) { - facebook_botkit.error(err); - return cb(err); - } else { - facebook_botkit.log(body.result); - return cb && cb(); - } - - }); - } else { - return cb() - } - - }; - - facebook_botkit.reactivateFacebook = function(cb) { - - request.post("https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=" + configuration.access_token, - function(err, res, body) { - - if (err) { - facebook_botkit.error(err); - return cb && cb(err); - } else { - - var isSuccess = JSON.parse(body).success; - - if (isSuccess) { - - facebook_botkit.log("Successfully subscribed to Facebook events"); - - return cb && cb(); - - } else { - facebook_botkit.error("Could not subscribe to page messages."); - return cb("Could not subscribe to page messages."); - } - - } - - }); - - }; - facebook_botkit.setupWebserver = function(port, cb) { if (!port) { @@ -367,7 +224,7 @@ function Facebookbot(configuration) { throw new Error('Specified port is not a valid number'); } - var static_dir = __dirname + '/public'; + var static_dir = __dirname + '/public'; if (facebook_botkit.config && facebook_botkit.config.webserver && facebook_botkit.config.webserver.static_dir) static_dir = facebook_botkit.config.webserver.static_dir; @@ -376,9 +233,7 @@ function Facebookbot(configuration) { facebook_botkit.webserver = express(); facebook_botkit.webserver.use(bodyParser.json()); - facebook_botkit.webserver.use(bodyParser.urlencoded({ - extended: true - })); + facebook_botkit.webserver.use(bodyParser.urlencoded({ extended: true })); facebook_botkit.webserver.use(express.static(static_dir)); var server = facebook_botkit.webserver.listen( @@ -386,17 +241,19 @@ function Facebookbot(configuration) { function() { facebook_botkit.log('** Starting webserver on port ' + facebook_botkit.config.port); - if (cb) { - cb(null, facebook_botkit.webserver); - } + if (cb) { cb(null, facebook_botkit.webserver); } }); - facebook_botkit.reactivateFacebook(); - facebook_botkit.greetingText() - facebook_botkit.greetingPayload(); - facebook_botkit.persistentMenu(); - facebook_botkit.startTicking(); + request.post('https://graph.facebook.com/me/subscribed_apps?access_token=' + configuration.access_token, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not subscribe to page messages'); + } else { + facebook_botkit.debug('Successfully subscribed to Facebook events:', body); + facebook_botkit.startTicking(); + } + }); return facebook_botkit; From 809cf5d21ca7ca7ba5f4168cda8fb58ec0eb723c Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Thu, 14 Jul 2016 16:38:09 -0400 Subject: [PATCH 5/5] This should be updated to 1500 seconds because Facebooks servers sometimes process messages out of order when using images due to the file size processing time. 1500 seconds seems to fix it. --- lib/CoreBot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/CoreBot.js b/lib/CoreBot.js index fe61062ba..842692917 100755 --- a/lib/CoreBot.js +++ b/lib/CoreBot.js @@ -823,7 +823,7 @@ function Botkit(configuration) { // set up a once a second tick to process messages botkit.tickInterval = setInterval(function() { botkit.tick(); - }, 1000); + }, 1500); } };