From 25288b39fd7d0d9f002e08996a3aa28b4d5bae7a Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Thu, 22 Mar 2018 00:41:09 +0100 Subject: [PATCH 1/4] Return a promise when making a post action to messenger profile API --- lib/Facebook.js | 66 +++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index e52ea2e3e..0310deca4 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -559,7 +559,7 @@ function Facebookbot(configuration) { 'text': payload }); } - facebook_botkit.api.messenger_profile.postAPI(message); + return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_greeting: function() { facebook_botkit.api.messenger_profile.deleteAPI('greeting'); @@ -573,7 +573,7 @@ function Facebookbot(configuration) { 'payload': payload } }; - facebook_botkit.api.messenger_profile.postAPI(message); + return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_get_started: function() { facebook_botkit.api.messenger_profile.deleteAPI('get_started'); @@ -585,7 +585,7 @@ function Facebookbot(configuration) { var messege = { persistent_menu: payload }; - facebook_botkit.api.messenger_profile.postAPI(messege); + return facebook_botkit.api.messenger_profile.postAPI(messege); }, delete_menu: function() { facebook_botkit.api.messenger_profile.deleteAPI('persistent_menu'); @@ -597,7 +597,7 @@ function Facebookbot(configuration) { var message = { 'account_linking_url': payload }; - facebook_botkit.api.messenger_profile.postAPI(message); + return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_account_linking: function() { facebook_botkit.api.messenger_profile.deleteAPI('account_linking_url'); @@ -609,7 +609,7 @@ function Facebookbot(configuration) { var message = { 'whitelisted_domains': Array.isArray(payload) ? payload : [payload] }; - facebook_botkit.api.messenger_profile.postAPI(message); + return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_domain_whitelist: function() { facebook_botkit.api.messenger_profile.deleteAPI('whitelisted_domains'); @@ -621,7 +621,7 @@ function Facebookbot(configuration) { var message = { 'target_audience': payload }; - facebook_botkit.api.messenger_profile.postAPI(message); + return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_target_audience: function() { facebook_botkit.api.messenger_profile.deleteAPI('target_audience'); @@ -633,7 +633,7 @@ function Facebookbot(configuration) { var message = { home_url: payload }; - facebook_botkit.api.messenger_profile.postAPI(message); + return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_home_url: function() { facebook_botkit.api.messenger_profile.deleteAPI('home_url'); @@ -642,35 +642,41 @@ function Facebookbot(configuration) { facebook_botkit.api.messenger_profile.getAPI('home_url', cb); }, postAPI: function(message) { - var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token; + return new Promise(function(resolve, reject) { + var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token; - if (facebook_botkit.config.require_appsecret_proof) { - uri += '&appsecret_proof=' + appsecret_proof; - } + if (facebook_botkit.config.require_appsecret_proof) { + uri += '&appsecret_proof=' + appsecret_proof; + } - request.post(uri, - {form: message}, - function(err, res, body) { - if (err) { - facebook_botkit.log('Could not configure messenger profile'); - } else { + request.post(uri, + {form: message}, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not configure messenger profile'); + reject(err); + } else { - var results = null; - try { - results = JSON.parse(body); - } catch (err) { - facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body); - } + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body); + reject(err); + } - if (results) { - if (results.error) { - facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message); - } else { - facebook_botkit.debug('Successfully configured messenger profile', body); + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message); + reject(results.error); + } else { + facebook_botkit.debug('Successfully configured messenger profile', body); + resolve(body); + } } } - } - }); + }); + }); }, deleteAPI: function(type) { var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token; From b6a3d0c0ea3af042b38a9740c73ceabb441f4175 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Thu, 22 Mar 2018 00:43:40 +0100 Subject: [PATCH 2/4] Return a promise when making a delete action into messenger profile API --- lib/Facebook.js | 50 ++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 0310deca4..62e0c7cf3 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -562,7 +562,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_greeting: function() { - facebook_botkit.api.messenger_profile.deleteAPI('greeting'); + return facebook_botkit.api.messenger_profile.deleteAPI('greeting'); }, get_greeting: function(cb) { facebook_botkit.api.messenger_profile.getAPI('greeting', cb); @@ -576,7 +576,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_get_started: function() { - facebook_botkit.api.messenger_profile.deleteAPI('get_started'); + return facebook_botkit.api.messenger_profile.deleteAPI('get_started'); }, get_get_started: function(cb) { facebook_botkit.api.messenger_profile.getAPI('get_started', cb); @@ -588,7 +588,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.postAPI(messege); }, delete_menu: function() { - facebook_botkit.api.messenger_profile.deleteAPI('persistent_menu'); + return facebook_botkit.api.messenger_profile.deleteAPI('persistent_menu'); }, get_menu: function(cb) { facebook_botkit.api.messenger_profile.getAPI('persistent_menu', cb); @@ -600,7 +600,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_account_linking: function() { - facebook_botkit.api.messenger_profile.deleteAPI('account_linking_url'); + return facebook_botkit.api.messenger_profile.deleteAPI('account_linking_url'); }, get_account_linking: function(cb) { facebook_botkit.api.messenger_profile.getAPI('account_linking_url', cb); @@ -612,7 +612,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_domain_whitelist: function() { - facebook_botkit.api.messenger_profile.deleteAPI('whitelisted_domains'); + return facebook_botkit.api.messenger_profile.deleteAPI('whitelisted_domains'); }, get_domain_whitelist: function(cb) { facebook_botkit.api.messenger_profile.getAPI('whitelisted_domains', cb); @@ -624,7 +624,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_target_audience: function() { - facebook_botkit.api.messenger_profile.deleteAPI('target_audience'); + return facebook_botkit.api.messenger_profile.deleteAPI('target_audience'); }, get_target_audience: function(cb) { facebook_botkit.api.messenger_profile.getAPI('target_audience', cb); @@ -636,7 +636,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.postAPI(message); }, delete_home_url: function() { - facebook_botkit.api.messenger_profile.deleteAPI('home_url'); + return facebook_botkit.api.messenger_profile.deleteAPI('home_url'); }, get_home_url: function(cb) { facebook_botkit.api.messenger_profile.getAPI('home_url', cb); @@ -679,25 +679,29 @@ function Facebookbot(configuration) { }); }, deleteAPI: function(type) { - var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token; + return new Promise(function(resolve, reject) { + var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token; - var message = { - 'fields': [type] - }; + var message = { + 'fields': [type] + }; - if (facebook_botkit.config.require_appsecret_proof) { - uri += '&appsecret_proof=' + appsecret_proof; - } + if (facebook_botkit.config.require_appsecret_proof) { + uri += '&appsecret_proof=' + appsecret_proof; + } - request.delete(uri, - {form: message}, - function(err, res, body) { - if (err) { - facebook_botkit.log('Could not configure messenger profile'); - } else { - facebook_botkit.debug('Successfully configured messenger profile', message); - } - }); + request.delete(uri, + {form: message}, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not configure messenger profile'); + reject(err); + } else { + facebook_botkit.debug('Successfully configured messenger profile', message); + resolve(message); + } + }); + }); }, getAPI: function(fields, cb) { var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?fields=' + fields + '&access_token=' + configuration.access_token; From 2a96d75e4ff648329d064b4c1d5d0bd142a86a08 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Thu, 22 Mar 2018 00:49:08 +0100 Subject: [PATCH 3/4] Return a promise when getting messenger profile API --- lib/Facebook.js | 52 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 62e0c7cf3..049d86a57 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -565,7 +565,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.deleteAPI('greeting'); }, get_greeting: function(cb) { - facebook_botkit.api.messenger_profile.getAPI('greeting', cb); + return facebook_botkit.api.messenger_profile.getAPI('greeting', cb); }, get_started: function(payload) { var message = { @@ -579,7 +579,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.deleteAPI('get_started'); }, get_get_started: function(cb) { - facebook_botkit.api.messenger_profile.getAPI('get_started', cb); + return facebook_botkit.api.messenger_profile.getAPI('get_started', cb); }, menu: function(payload) { var messege = { @@ -591,7 +591,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.deleteAPI('persistent_menu'); }, get_menu: function(cb) { - facebook_botkit.api.messenger_profile.getAPI('persistent_menu', cb); + return facebook_botkit.api.messenger_profile.getAPI('persistent_menu', cb); }, account_linking: function(payload) { var message = { @@ -603,7 +603,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.deleteAPI('account_linking_url'); }, get_account_linking: function(cb) { - facebook_botkit.api.messenger_profile.getAPI('account_linking_url', cb); + return facebook_botkit.api.messenger_profile.getAPI('account_linking_url', cb); }, domain_whitelist: function(payload) { var message = { @@ -615,7 +615,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.deleteAPI('whitelisted_domains'); }, get_domain_whitelist: function(cb) { - facebook_botkit.api.messenger_profile.getAPI('whitelisted_domains', cb); + return facebook_botkit.api.messenger_profile.getAPI('whitelisted_domains', cb); }, target_audience: function(payload) { var message = { @@ -627,7 +627,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.deleteAPI('target_audience'); }, get_target_audience: function(cb) { - facebook_botkit.api.messenger_profile.getAPI('target_audience', cb); + return facebook_botkit.api.messenger_profile.getAPI('target_audience', cb); }, home_url: function(payload) { var message = { @@ -639,7 +639,7 @@ function Facebookbot(configuration) { return facebook_botkit.api.messenger_profile.deleteAPI('home_url'); }, get_home_url: function(cb) { - facebook_botkit.api.messenger_profile.getAPI('home_url', cb); + return facebook_botkit.api.messenger_profile.getAPI('home_url', cb); }, postAPI: function(message) { return new Promise(function(resolve, reject) { @@ -704,26 +704,30 @@ function Facebookbot(configuration) { }); }, getAPI: function(fields, cb) { - var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?fields=' + fields + '&access_token=' + configuration.access_token; + return new Promise(function(resolve, reject) { + var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?fields=' + fields + '&access_token=' + configuration.access_token; - if (facebook_botkit.config.require_appsecret_proof) { - uri += '&appsecret_proof=' + appsecret_proof; - } + if (facebook_botkit.config.require_appsecret_proof) { + uri += '&appsecret_proof=' + appsecret_proof; + } - request.get(uri, - function(err, res, body) { - if (err) { - facebook_botkit.log('Could not get messenger profile'); - if (cb) { - cb(err); - } - } else { - facebook_botkit.debug('Successfully got messenger profile ', body); - if (cb) { - cb(null, body); + request.get(uri, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not get messenger profile'); + if (cb) { + cb(err); + } + reject(err); + } else { + facebook_botkit.debug('Successfully got messenger profile ', body); + if (cb) { + cb(null, body); + } + resolve(body); } - } - }); + }); + }); }, get_messenger_code: function(image_size, cb, ref) { var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_codes?access_token=' + configuration.access_token; From 97b3c1c7522711cab09349d0b010668bef5ebf2d Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Thu, 22 Mar 2018 01:42:42 +0100 Subject: [PATCH 4/4] Return an error if FB returns error in the result --- lib/Facebook.js | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 049d86a57..cda18e4f9 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -697,8 +697,23 @@ function Facebookbot(configuration) { facebook_botkit.log('Could not configure messenger profile'); reject(err); } else { - facebook_botkit.debug('Successfully configured messenger profile', message); - resolve(message); + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body); + reject(err); + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message); + reject(results.error); + } else { + facebook_botkit.debug('Successfully configured messenger profile ', body); + resolve(message); + } + } } }); }); @@ -715,16 +730,29 @@ function Facebookbot(configuration) { function(err, res, body) { if (err) { facebook_botkit.log('Could not get messenger profile'); - if (cb) { - cb(err); - } - reject(err); + if (cb) cb(err) + else reject(err) } else { - facebook_botkit.debug('Successfully got messenger profile ', body); - if (cb) { - cb(null, body); + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body); + if (cb) cb(err) + else reject(err) + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message); + if (cb) cb(results.error) + else reject(results.error) + } else { + facebook_botkit.debug('Successfully got messenger profile ', body); + if (cb) cb(null, body) + else resolve(body) + } } - resolve(body); } }); });