From f448e9a61f46afd94e66cb20d2423f3f069726f2 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Tue, 12 Jul 2016 00:11:59 -0400 Subject: [PATCH] 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;