From bd329621f45e60917a0e0182e056945c9c3b3a3d Mon Sep 17 00:00:00 2001 From: Prathm Juneja Date: Mon, 8 Apr 2019 19:43:53 -0400 Subject: [PATCH 1/3] Fixed Facebook API v3.2 Messenger Bot (I can add more information to a README PR for set up purposes). The main change here is that Facebook's v3.2 Graph API has changed the URL of the POST request you need in order to setup a FB messenger chat bot. You will recall that when setting up a Facebook app and creating the webhook, you had to subscribe to certain fields. All you need for the chatbot to function is the "messages" field. In v3.2 of the Facebook Graph API, you now had to explicitly send FB, in the POST request, what fields you were planning on subscribing to. That is why 'messages' is now hard-coded into line 95. This POST request also required that you put the "pageID" of the Facebook page your chatbot was going to message from (the same page you got your Page Access Token from). This is either available in the settings of your Facebook page, or by viewing the source of the Facebook page and looking for the variable called "pageID". Another weird addition is that chatbots now require a permission called "manage_pages". You can't actually give your FB app and chatbot the manage_pages permission from the main developer console. Instead, you need to go to this link: https://developers.facebook.com/tools/explorer/?classic=0 and from there choose your page, and add a new permission of "manage_pages". --- lib/facebook/setup.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/facebook/setup.js b/lib/facebook/setup.js index 1df238a..bad7fc9 100644 --- a/lib/facebook/setup.js +++ b/lib/facebook/setup.js @@ -47,7 +47,7 @@ module.exports = function fbSetup(api, bot, logError, optionalParser, optionalRe stageName: lambdaDetails.alias }).then(data => { if (options['configure-fb-bot']) { - let token, pageAccessToken; + let token, pageAccessToken, pageID; return Promise.resolve().then(() => { if (data.variables && data.variables.facebookVerifyToken) @@ -71,11 +71,12 @@ module.exports = function fbSetup(api, bot, logError, optionalParser, optionalRe console.log(`\nYour webhook URL is: ${color.cyan}${lambdaDetails.apiUrl}/facebook${color.reset}\n`); console.log(`Your verify token is: ${color.cyan}${token}${color.reset}\n`); - return prompt(['Facebook page access token', 'Facebook App Secret']); + return prompt(['Facebook page access token', 'Facebook App Secret', 'Facebook Page ID']); }) .then(results => { console.log('\n'); pageAccessToken = results['Facebook page access token']; + pageID = results['Facebook Page ID']; const deployment = { restApiId: lambdaDetails.apiId, stageName: lambdaDetails.alias, @@ -84,13 +85,14 @@ module.exports = function fbSetup(api, bot, logError, optionalParser, optionalRe facebookAppSecret: results['Facebook App Secret'] } }; + console.log('https://graph.facebook.com/v3.2/${pageID}/subscribed_apps?subscribed_fields=["messages"]&access_token=${pageAccessToken}') if (!data.variables || (!data.variables.facebookAppSecret && !results['Facebook App Secret'])) console.log(`\n${color.yellow}Deprecation warning:${color.reset} your bot is not using facebook validation. Please re-run with --configure-fb-bot to set it. This will become mandatory in the next major version. See https://github.com/claudiajs/claudia-bot-builder/blob/master/docs/API.md#message-verification for more information.\n`); return utils.apiGatewayPromise.createDeploymentPromise(deployment); }) - .then(() => rp.post(`https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=${pageAccessToken}`)); + .then(() => rp.post(`https://graph.facebook.com/v3.2/${pageID}/subscribed_apps?subscribed_fields=['messages']&access_token=${pageAccessToken}`)); } }); }) From 14bb797b998b558b83d263b8245062952b42a40f Mon Sep 17 00:00:00 2001 From: Prathm Juneja Date: Mon, 8 Apr 2019 20:07:48 -0400 Subject: [PATCH 2/3] Facebook Messenger v3.2 Fix Reposting this commit because syntax differences caused Travis to fail on the last one. (I can add more information to a README PR for set up purposes). The main change here is that Facebook has changed the URL of the POST request you need in order to setup a FB messenger chat bot. You will recall that when setting up a Facebook app and creating the webhook, you had to subscribe to certain fields. All you need for the chatbot to function is the "messages" field. In v3.2 of the Facebook Graph API, you now had to explicitly send FB, in the POST request, what fields you were planning on subscribing to. That is why 'messages' is now hard-coded into line 95. This POST request also required that you put the "pageID" of the Facebook page your chatbot was going to message from (the same page you got your Page Access Token from). This is either available in the settings of your Facebook page, or by viewing the source of the Facebook page and looking for the variable called "pageID". Another weird addition is that chatbots now require a permission called "manage_pages". You can't actually give your FB app and chatbot the manage_pages permission from the main developer console. Instead, you need to go to this link: https://developers.facebook.com/tools/explorer/?classic=0 and from there choose your page, and add a new permission of "manage_pages". --- lib/facebook/setup.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/facebook/setup.js b/lib/facebook/setup.js index bad7fc9..1e10075 100644 --- a/lib/facebook/setup.js +++ b/lib/facebook/setup.js @@ -69,14 +69,14 @@ module.exports = function fbSetup(api, bot, logError, optionalParser, optionalRe console.log(`\n\n${color.green}Facebook Messenger setup${color.reset}\n`); console.log(`\nFollowing info is required for the setup, for more info check the documentation.\n`); console.log(`\nYour webhook URL is: ${color.cyan}${lambdaDetails.apiUrl}/facebook${color.reset}\n`); - console.log(`Your verify token is: ${color.cyan}${token}${color.reset}\n`); + console.log(` fda Your verify token is: ${color.cyan}${token}${color.reset}\n`); return prompt(['Facebook page access token', 'Facebook App Secret', 'Facebook Page ID']); }) .then(results => { console.log('\n'); pageAccessToken = results['Facebook page access token']; - pageID = results['Facebook Page ID']; + pageID = results['Facebook Page ID']; const deployment = { restApiId: lambdaDetails.apiId, stageName: lambdaDetails.alias, @@ -85,8 +85,6 @@ module.exports = function fbSetup(api, bot, logError, optionalParser, optionalRe facebookAppSecret: results['Facebook App Secret'] } }; - console.log('https://graph.facebook.com/v3.2/${pageID}/subscribed_apps?subscribed_fields=["messages"]&access_token=${pageAccessToken}') - if (!data.variables || (!data.variables.facebookAppSecret && !results['Facebook App Secret'])) console.log(`\n${color.yellow}Deprecation warning:${color.reset} your bot is not using facebook validation. Please re-run with --configure-fb-bot to set it. This will become mandatory in the next major version. See https://github.com/claudiajs/claudia-bot-builder/blob/master/docs/API.md#message-verification for more information.\n`); From 8c8bf967d15dcdd1c2e91984aec10a08569745c9 Mon Sep 17 00:00:00 2001 From: Prathm Juneja Date: Sun, 28 Apr 2019 18:48:27 -0400 Subject: [PATCH 3/3] Fixed a typo in setup.js --- lib/facebook/setup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/facebook/setup.js b/lib/facebook/setup.js index 1e10075..5210495 100644 --- a/lib/facebook/setup.js +++ b/lib/facebook/setup.js @@ -69,7 +69,7 @@ module.exports = function fbSetup(api, bot, logError, optionalParser, optionalRe console.log(`\n\n${color.green}Facebook Messenger setup${color.reset}\n`); console.log(`\nFollowing info is required for the setup, for more info check the documentation.\n`); console.log(`\nYour webhook URL is: ${color.cyan}${lambdaDetails.apiUrl}/facebook${color.reset}\n`); - console.log(` fda Your verify token is: ${color.cyan}${token}${color.reset}\n`); + console.log('\nYour verify token is: ${color.cyan}${token}${color.reset}\n`); return prompt(['Facebook page access token', 'Facebook App Secret', 'Facebook Page ID']); })