Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Add the new Facebook Built-in NLP #943

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/readme-facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions examples/facebook_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
41 changes: 40 additions & 1 deletion lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};

Expand Down Expand Up @@ -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
Expand Down