From 7b4dcf956ad6136477fb36d855da6f5f774b977a Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Sun, 30 Jul 2017 19:32:06 +0200 Subject: [PATCH 1/4] Add build-in NLP to facebook --- lib/Facebook.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index db977a773..bfbd6a3a5 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -650,10 +650,45 @@ function Facebookbot(configuration) { }; + var build_in_nlp = { + enable: function() { + facebook_botkit.api.build_in_nlp.postAPI(true); + }, + disable: function() { + facebook_botkit.api.build_in_nlp.postAPI(false); + }, + postAPI: function(value) { + request.post('https://' + api_host + '/v2.8/me/nlp_configs?nlp_enabled=' + value + '&access_token=' + configuration.access_token, + {}, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not enable/disable build-in NLP'); + } else { + + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in build-in NLP API call: Could not parse JSON', err, body); + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in build-in API call: ', results.error.message); + } else { + facebook_botkit.debug('Successfully enable/disable build-in NLP', body); + } + } + } + }); + } + }; + facebook_botkit.api = { 'messenger_profile': messenger_profile_api, 'thread_settings': messenger_profile_api, - 'attachment_upload': attachment_upload_api + 'attachment_upload': attachment_upload_api, + 'build_in_nlp': build_in_nlp }; // Verifies the SHA1 signature of the raw request payload before bodyParser parses it From cd32af3f02d6ff7d14c4e5eff162400498a92a11 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Sun, 30 Jul 2017 19:57:02 +0200 Subject: [PATCH 2/4] Add NLP section to the catched facebook message --- lib/Facebook.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Facebook.js b/lib/Facebook.js index bfbd6a3a5..553947a05 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -278,6 +278,7 @@ function Facebookbot(configuration) { sticker_id: facebook_message.message.sticker_id, attachments: facebook_message.message.attachments, quick_reply: facebook_message.message.quick_reply, + nlp: facebook_message.message.nlp, type: 'user_message', }; From 462cc7d9d99cc3536a6dfb0b9f777c0974d32694 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Sun, 30 Jul 2017 20:06:16 +0200 Subject: [PATCH 3/4] Add custom_token an optional param to set a server access token from Wit --- lib/Facebook.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 553947a05..b97a2c92c 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -652,15 +652,18 @@ function Facebookbot(configuration) { }; var build_in_nlp = { - enable: function() { - facebook_botkit.api.build_in_nlp.postAPI(true); + enable: function(custom_token) { + facebook_botkit.api.build_in_nlp.postAPI(true, custom_token); }, disable: function() { facebook_botkit.api.build_in_nlp.postAPI(false); }, - postAPI: function(value) { - request.post('https://' + api_host + '/v2.8/me/nlp_configs?nlp_enabled=' + value + '&access_token=' + configuration.access_token, - {}, + postAPI: function(value, custom_token) { + var uri = 'https://' + api_host + '/v2.8/me/nlp_configs?nlp_enabled=' + value + '&access_token=' + configuration.access_token; + if(custom_token) { + uri+= '&custom_token=' + custom_token; + } + request.post(uri, {}, function(err, res, body) { if (err) { facebook_botkit.log('Could not enable/disable build-in NLP'); From 5745731f46872dbab590d8c6f522c8e3cc219d75 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Sun, 30 Jul 2017 20:11:41 +0200 Subject: [PATCH 4/4] Add some docs and exaples of how to activate/disactivate build-in NLP --- docs/readme-facebook.md | 11 +++++++++++ examples/facebook_bot.js | 5 +++++ lib/Facebook.js | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/readme-facebook.md b/docs/readme-facebook.md index 86587674c..5e3a924eb 100644 --- a/docs/readme-facebook.md +++ b/docs/readme-facebook.md @@ -539,6 +539,17 @@ var attachment = { ``` +## Built-in NLP + +Natural Language Processing (NLP) allows you to understand and extract meaningful information out of the messages people send : + + +```js + // Enable Build-in NLP + controller.api.build_in_nlp.build_in_nlp.enable(); + // disable Build-in NLP with + controller.api.build_in_nlp.build_in_nlp.disable(); +``` ## Use BotKit for Facebook Messenger with an Express web server Instead of the web server generated with setupWebserver(), it is possible to use a different web server to receive webhooks, as well as serving web pages. diff --git a/examples/facebook_bot.js b/examples/facebook_bot.js index c5ee94b1e..c24d2e4df 100755 --- a/examples/facebook_bot.js +++ b/examples/facebook_bot.js @@ -452,6 +452,11 @@ controller.hears(['shutdown'], 'message_received', function(bot, message) { }); }); +/** Build in NLP **/ +controller.hears(['build-in NLP'], 'message_received,facebook_postback', function(bot, message) { + controller.api.build_in_nlp.build_in_nlp.disable(); + controller.api.build_in_nlp.build_in_nlp.enable(); +}); controller.hears(['uptime', 'identify yourself', 'who are you', 'what is your name'], 'message_received', function(bot, message) { diff --git a/lib/Facebook.js b/lib/Facebook.js index b97a2c92c..bd857fa22 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -660,8 +660,8 @@ function Facebookbot(configuration) { }, postAPI: function(value, custom_token) { var uri = 'https://' + api_host + '/v2.8/me/nlp_configs?nlp_enabled=' + value + '&access_token=' + configuration.access_token; - if(custom_token) { - uri+= '&custom_token=' + custom_token; + if (custom_token) { + uri += '&custom_token=' + custom_token; } request.post(uri, {}, function(err, res, body) {