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 db977a773..bd857fa22 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', }; @@ -650,10 +651,48 @@ function Facebookbot(configuration) { }; + var build_in_nlp = { + 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, 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'); + } 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