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

Adding Facebook message tags #960

Merged
merged 6 commits into from
Aug 29, 2017
Merged
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
22 changes: 22 additions & 0 deletions docs/readme-facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,28 @@ var attachment = {

```

## Message Tags

Adding a tag to a message allows you to send it outside the 24+1 window.

View the facebook [documentation](https://developers.facebook.com/docs/messenger-platform/messenger-profile/home-url) for more details.

- Get all tags :
```javascript
controller.api.tags.get_all(function (tags) {
// use tags.data
});
```

- Send a tagged message :
```javascript
var taggedMessage = {
"text": "Hello Botkit !",
"tag": "RESERVATION_UPDATE"
};
bot.reply(message, taggedMessage);
```


## 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
16 changes: 16 additions & 0 deletions examples/facebook_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,22 @@ controller.on('message_received', function(bot, message) {
return false;
});

controller.hears(['tags'], 'message_received', function (bot, message) {
controller.api.tags.get_all(function (tags) {
for (var i = 0; i < tags.data.length; i++) {
bot.reply(message, tags.data[i].tag + ': ' + tags.data[i].description);
}
});
});

controller.hears(['send tagged message'], 'message_received', function (bot, message) {
var taggedMessage = {
"text": "Hello Botkit !",
"tag": "RESERVATION_UPDATE"
};
bot.reply(message, taggedMessage);
});


function formatUptime(uptime) {
var unit = 'second';
Expand Down
34 changes: 32 additions & 2 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function Facebookbot(configuration) {

var facebook_message = {
recipient: {},
message: message.sender_action ? undefined : {}
message: message.sender_action ? undefined : {},
tag: message.tag
};

if (typeof(message.channel) == 'string' && message.channel.match(/\+\d+\(\d\d\d\)\d\d\d\-\d\d\d\d/)) {
Expand Down Expand Up @@ -650,10 +651,39 @@ function Facebookbot(configuration) {

};

var tags = {
get_all: function(cb) {
request.get('https://' + api_host + '/v2.6/page_message_tags?access_token=' + configuration.access_token,
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not get tags list');
} else {

var results = null;
try {
results = JSON.parse(body);
} catch (err) {
facebook_botkit.log('ERROR in page message tags call: Could not parse JSON', err, body);
}

if (results) {
if (results.error) {
facebook_botkit.log('ERROR in page message tags: ', results.error.message);
} else {
facebook_botkit.debug('Successfully call page message tags', body);
cb(results);
}
}
}
});
}
};

facebook_botkit.api = {
'messenger_profile': messenger_profile_api,
'thread_settings': messenger_profile_api,
'attachment_upload': attachment_upload_api
'attachment_upload': attachment_upload_api,
'tags': tags
};

// Verifies the SHA1 signature of the raw request payload before bodyParser parses it
Expand Down