diff --git a/docs/readme-facebook.md b/docs/readme-facebook.md index 86587674c..98bc37201 100644 --- a/docs/readme-facebook.md +++ b/docs/readme-facebook.md @@ -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. diff --git a/examples/facebook_bot.js b/examples/facebook_bot.js index c5ee94b1e..c9e0c244d 100755 --- a/examples/facebook_bot.js +++ b/examples/facebook_bot.js @@ -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'; diff --git a/lib/Facebook.js b/lib/Facebook.js index db977a773..97719e334 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -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/)) { @@ -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