From fac122b6e9ab7fef1b0eaf4e7b1855a16523e059 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Tue, 29 Aug 2017 09:18:12 -0500 Subject: [PATCH] Add support for facebook's built-in nlp --- changelog.md | 9 +++++++++ docs/readme-facebook.md | 12 ++++++++++++ examples/facebook_bot.js | 2 ++ lib/Botkit.d.ts | 2 ++ lib/Facebook.js | 41 +++++++++++++++++++++++++++++++++++++++- lib/TwilioSMSBot.js | 2 +- 6 files changed, 66 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index c097b209c..159176672 100644 --- a/changelog.md +++ b/changelog.md @@ -8,8 +8,17 @@ Slack: Support for sending ephemeral messages with `bot.whisper()` and `bot.sendEphemeral()`. In addition, any message with `message.ephemeral` set to true will be sent with `bot.sendEphemeral()` automatically. [Read documentation here.](docs/readme-slack.md#ephemeral-messages) Thanks to [@jonchurch](https://github.com/howdyai/botkit/pull/958) +Slack: Add support for `bot.api.files.sharedPublicURL()` method. Thanks to [@GitTristan](https://github.com/howdyai/botkit/pull/912) + Facebook: Support for using [message tags](https://developers.facebook.com/docs/messenger-platform/message-tags). [Read documentation here.](docs/readme-facebook.md#message-tags) Thanks to [@ouadie-lahdioui](https://github.com/howdyai/botkit/pull/960) +Facebook: Support for using Facebook's new built-in NLP tools. [Read documentation here.](docs/readme-facebook.md#built-in-nlp) Thanks to [@ouadie-lahdioui](https://github.com/howdyai/botkit/pull/943) for this one too!! + + +Twilio SMS: Add support for sending MMS messages (file attachments) via Twilio. [Read documentation here.](docs/readme-twiliosms.md#sending-media-attachments-mms) Thanks to [@krismuniz](https://github.com/howdyai/botkit/pull/951)! + +Cisco Spark: Emit a console warning when a bot receives messages from outside the allowed domain list. Thanks to [@MathRobin](https://github.com/howdyai/botkit/pull/918)! + New: Typescript declarations! Thanks to [@uny and @naktibalda](https://github.com/howdyai/botkit/pull/953) for their work on this. diff --git a/docs/readme-facebook.md b/docs/readme-facebook.md index 98bc37201..b6d7044f2 100644 --- a/docs/readme-facebook.md +++ b/docs/readme-facebook.md @@ -539,6 +539,18 @@ var attachment = { ``` +## Built-in NLP + +Facebook offers some built-in natural language processing tools. Once enabled, messages may contain a `message.nlp.` object with the results of the Facebook NLP. +More information can be found [in Facebook's official documentation of this feature](https://developers.facebook.com/docs/messenger-platform/built-in-nlp). + +If specified, `message.nlp.entities` will include a list of entities and intents extracted by Facebook. + +Facebook's NLP option can be enabled by calling `controller.api.nlp.enable()` in your Botkit app. + +Facebook's NLP option can be disabled by calling `controller.api.nlp.enable()` in your Botkit app. + + ## Message Tags Adding a tag to a message allows you to send it outside the 24+1 window. diff --git a/examples/facebook_bot.js b/examples/facebook_bot.js index c9e0c244d..8dc81db1b 100755 --- a/examples/facebook_bot.js +++ b/examples/facebook_bot.js @@ -158,6 +158,8 @@ controller.hears(['attachment_upload'], 'message_received', function(bot, messag }); }); + +controller.api.nlp.enable(); controller.api.messenger_profile.greeting('Hello! I\'m a Botkit bot!'); controller.api.messenger_profile.get_started('sample_get_started_payload'); controller.api.messenger_profile.menu([{ diff --git a/lib/Botkit.d.ts b/lib/Botkit.d.ts index bac590c70..1271475c9 100644 --- a/lib/Botkit.d.ts +++ b/lib/Botkit.d.ts @@ -169,6 +169,8 @@ declare namespace botkit { messenger_profile: any; thread_settings: any; tags: any; + nlp: any; + }; createWebhookEndpoints(webserver: any, bot: FacebookBot, cb?: () => void): this; } diff --git a/lib/Facebook.js b/lib/Facebook.js index 97719e334..599697b30 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -279,6 +279,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', }; @@ -679,11 +680,49 @@ function Facebookbot(configuration) { } }; + var nlp = { + enable: function(custom_token) { + facebook_botkit.api.nlp.postAPI(true, custom_token); + }, + disable: function() { + facebook_botkit.api.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, - 'tags': tags + 'nlp': nlp, + 'tags': tags, }; // Verifies the SHA1 signature of the raw request payload before bodyParser parses it diff --git a/lib/TwilioSMSBot.js b/lib/TwilioSMSBot.js index 59155ac4a..0fb32396b 100644 --- a/lib/TwilioSMSBot.js +++ b/lib/TwilioSMSBot.js @@ -58,7 +58,7 @@ function TwilioSMS(configuration) { }; if (message.hasOwnProperty('medaUrl')) { - sms.mediaUrl = message.mediaUrl + sms.mediaUrl = message.mediaUrl; } client.messages.create(sms, function(err, message) {