From 24983651f4c0ced25ac7c51960f5e00cfba44e43 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Sat, 9 Dec 2017 10:19:47 +0100 Subject: [PATCH 01/14] Upgrade FB Graphe API version --- lib/Facebook.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index bff34d432..3079f1e20 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -5,6 +5,7 @@ var bodyParser = require('body-parser'); function Facebookbot(configuration) { + var api_version = 'v2.11'; var api_host = configuration.api_host || 'graph.facebook.com'; // Create a core botkit bot @@ -145,7 +146,7 @@ function Facebookbot(configuration) { 'content-type': 'application/json', }, body: message, - uri: 'https://' + api_host + '/v2.6/me/messages' + uri: 'https://' + api_host + '/' + api_version + '/me/messages' }, function(err, res, body) { @@ -291,7 +292,7 @@ function Facebookbot(configuration) { team: {}, }; - request.get('https://' + api_host + '/v2.6/me?access_token=' + configuration.access_token, + request.get('https://' + api_host + '/' + api_version+ '/me?access_token=' + configuration.access_token, {}, function(err, res, body) { if (err) { @@ -566,7 +567,7 @@ function Facebookbot(configuration) { facebook_botkit.api.messenger_profile.getAPI('home_url', cb); }, postAPI: function(message) { - request.post('https://' + api_host + '/v2.6/me/messenger_profile?access_token=' + configuration.access_token, + request.post('https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token, {form: message}, function(err, res, body) { if (err) { @@ -594,7 +595,7 @@ function Facebookbot(configuration) { var message = { 'fields': [type] }; - request.delete('https://' + api_host + '/v2.6/me/messenger_profile?access_token=' + configuration.access_token, + request.delete('https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token, {form: message}, function(err, res, body) { if (err) { @@ -605,7 +606,7 @@ function Facebookbot(configuration) { }); }, getAPI: function(fields, cb) { - request.get('https://' + api_host + '/v2.6/me/messenger_profile?fields=' + fields + '&access_token=' + configuration.access_token, + request.get('https://' + api_host + '/' + api_version + '/me/messenger_profile?fields=' + fields + '&access_token=' + configuration.access_token, function(err, res, body) { if (err) { facebook_botkit.log('Could not get messenger profile'); @@ -626,7 +627,7 @@ function Facebookbot(configuration) { message.data = {'ref': ref}; } - request.post('https://' + api_host + '/v2.6/me/messenger_codes?access_token=' + configuration.access_token, + request.post('https://' + api_host + '/' + api_version + '/me/messenger_codes?access_token=' + configuration.access_token, {form: message}, function(err, res, body) { @@ -666,7 +667,7 @@ function Facebookbot(configuration) { } }; - request.post('https://' + api_host + '/v2.6/me/message_attachments?access_token=' + configuration.access_token, + request.post('https://' + api_host + '/' + api_version + '/me/message_attachments?access_token=' + configuration.access_token, { form: message }, function(err, res, body) { if (err) { @@ -700,7 +701,7 @@ function Facebookbot(configuration) { var tags = { get_all: function(cb) { - request.get('https://' + api_host + '/v2.6/page_message_tags?access_token=' + configuration.access_token, + request.get('https://' + api_host + '/' + api_version + '/page_message_tags?access_token=' + configuration.access_token, function(err, res, body) { if (err) { facebook_botkit.log('Could not get tags list'); @@ -734,7 +735,7 @@ function Facebookbot(configuration) { facebook_botkit.api.nlp.postAPI(false, custom_token); }, postAPI: function(value, custom_token) { - var uri = 'https://' + api_host + '/v2.8/me/nlp_configs?nlp_enabled=' + value + '&access_token=' + configuration.access_token; + var uri = 'https://' + api_host + '/' + api_version + '/me/nlp_configs?nlp_enabled=' + value + '&access_token=' + configuration.access_token; if (custom_token) { uri += '&custom_token=' + custom_token; } @@ -768,7 +769,7 @@ function Facebookbot(configuration) { fields = 'first_name,last_name,timezone,gender,locale'; } return new Promise(function(resolve, reject) { - var uri = 'https://' + api_host + '/v2.6/' + uid + '?fields=' + fields + '&access_token=' + configuration.access_token; + var uri = 'https://' + api_host + '/' + api_version + '/' + uid + '?fields=' + fields + '&access_token=' + configuration.access_token; request.get(uri, {}, function(err, res, body) { if (err) { From ce8bf6b5e8d7c65f43aac18bf7144316bb7f34d7 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 11:18:20 +0100 Subject: [PATCH 02/14] Add Broadcast API --- lib/Facebook.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 3079f1e20..2d14486a4 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -816,10 +816,7 @@ function Facebookbot(configuration) { }); }; - - - - + var broadcast_api = {}; facebook_botkit.api = { 'user_profile': user_profile, @@ -828,12 +825,10 @@ function Facebookbot(configuration) { 'attachment_upload': attachment_upload_api, 'nlp': nlp, 'tags': tags, + 'broadcast': broadcast_api }; - - - // Verifies the SHA1 signature of the raw request payload before bodyParser parses it // Will abort parsing if signature is invalid, and pass a generic error to response function verifyRequest(req, res, buf, encoding) { From 2682688c93160ad7b95a1da30d03f2417f5a178c Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 11:18:46 +0100 Subject: [PATCH 03/14] Add create message creative --- lib/Facebook.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 2d14486a4..f5d77475f 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -816,7 +816,43 @@ function Facebookbot(configuration) { }); }; - var broadcast_api = {}; + var broadcast_api = { + create_message_creative: function(message, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/me/message_creatives?access_token=' + configuration.access_token; + + var messageToBroadcast = {}; + + if (typeof(message) == 'string') { + messageToBroadcast.text = message; + } else { + messageToBroadcast = message; + } + + var body = { + 'messages' : [messageToBroadcast] + }; + + request({ + method: 'POST', + json: true, + body: body, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not Create a broadcast message'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in creating a broadcast message call: ', body.error); + cb(body.error); + } else { + facebook_botkit.debug('Successfully create a broadcast message', body); + cb(null, body); + } + } + }); + } + }; facebook_botkit.api = { 'user_profile': user_profile, From a630b8d168c5e237ad77852cdc9ef71821e705c4 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 11:19:07 +0100 Subject: [PATCH 04/14] Add send broadcast message --- lib/Facebook.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Facebook.js b/lib/Facebook.js index f5d77475f..81e1fa72e 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -851,6 +851,33 @@ function Facebookbot(configuration) { } } }); + }, + send: function(message_creative_id, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/me/broadcast_messages?access_token=' + configuration.access_token; + + var body = { + 'message_creative_id' : message_creative_id + }; + + request({ + method: 'POST', + json: true, + body: body, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not send a broadcast message'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in sending a broadcast message call : ', body.error); + cb(body.error); + } else { + facebook_botkit.debug('Successfully send a broadcast message', body); + cb(null, body); + } + } + }); } }; From bea5c1f899b752b3e73b4524d67900b26fddf42a Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 11:31:49 +0100 Subject: [PATCH 05/14] Add getting broadcast metrics --- lib/Facebook.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/Facebook.js b/lib/Facebook.js index 81e1fa72e..7afb43346 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -878,6 +878,34 @@ function Facebookbot(configuration) { } } }); + }, + get_broadcast_metrics: function(broadcast_id, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/' + broadcast_id + '/insights/messages_sent?access_token=' + configuration.access_token; + + request.get(uri, {}, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not send a broadcast message'); + cb(err); + } else { + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in getting broadcast metrics call: Could not parse JSON', err, body); + cb(err); + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in getting broadcast metrics call : ', results.error.message); + cb(results.error); + } else { + facebook_botkit.log('Successfully get broadcast metrics '); + cb(null, results); + } + } + } + }); } }; From f444e046e48d45f5855cb0c0d3950bb18dc42690 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 11:41:46 +0100 Subject: [PATCH 06/14] Add creat label --- lib/Facebook.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 7afb43346..d6047c7ab 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -906,7 +906,34 @@ function Facebookbot(configuration) { } } }); - } + }, + create_label: function(name, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/me/custom_labels?access_token=' + configuration.access_token; + + var body = { + 'name' : name + }; + + request({ + method: 'POST', + json: true, + body: body, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not create a label to target broadcast messages'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in creating a label to target broadcast messages call : ', body.error); + cb(body.error); + } else { + facebook_botkit.debug('Successfully creat a label to target broadcast messages', body); + cb(null, body); + } + } + }); + }, }; facebook_botkit.api = { From 958017a1c8798c22f7d4fd0d5006e55a3fe803de Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 11:57:49 +0100 Subject: [PATCH 07/14] Add user to a custom label --- lib/Facebook.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index d6047c7ab..7428527a6 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -928,12 +928,39 @@ function Facebookbot(configuration) { facebook_botkit.log('ERROR in creating a label to target broadcast messages call : ', body.error); cb(body.error); } else { - facebook_botkit.debug('Successfully creat a label to target broadcast messages', body); + facebook_botkit.debug('Successfully create a label to target broadcast messages', body); cb(null, body); } } }); }, + add_user_to_label: function(user, label_id, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/' + label_id + '/label?access_token=' + configuration.access_token; + + var body = { + 'user' : user + }; + + request({ + method: 'POST', + json: true, + body: body, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not add a user to a label'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in adding a user to a label API call : ', body.error); + cb(body.error); + } else { + facebook_botkit.debug('Successfully add a user to a label', body); + cb(null, body); + } + } + }); + } }; facebook_botkit.api = { From 12d0f16e1b0875f71f5c379a7629460aa752d8b9 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 12:11:51 +0100 Subject: [PATCH 08/14] Add remove a user from a label --- lib/Facebook.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Facebook.js b/lib/Facebook.js index 7428527a6..28ab9df8d 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -960,6 +960,33 @@ function Facebookbot(configuration) { } } }); + }, + remove_user_from_label: function(user, label_id, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/' + label_id + '/label?access_token=' + configuration.access_token; + + var body = { + 'user' : user + }; + + request({ + method: 'DELETE', + json: true, + body: body, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not remove a user from a label'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in removing a user from a label API call : ', body.error); + cb(body.error); + } else { + facebook_botkit.debug('Successfully remove a user from a label', body); + cb(null, body); + } + } + }); } }; From 0f81131b3a35486c4f4585bff3693cad0b76db05 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 12:18:55 +0100 Subject: [PATCH 09/14] Add Retrieving Labels Associated with a PSID --- lib/Facebook.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 28ab9df8d..c34478adf 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -987,7 +987,29 @@ function Facebookbot(configuration) { } } }); - } + }, + get_labels_by_user: function(user, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/' + user + '/custom_labels?access_token=' + configuration.access_token; + + request({ + method: 'GET', + json: true, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not get labels associated with a user'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in getting labels associated with a user call : ', body.error.message); + cb(body.error); + } else { + facebook_botkit.log('Successfully get labels associated with a user'); + cb(null, body); + } + } + }); + }, }; facebook_botkit.api = { From 23b16627b0ebbb2c27d445cc805003ac4adbbb42 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 14:33:18 +0100 Subject: [PATCH 10/14] Add get label details --- lib/Facebook.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/Facebook.js b/lib/Facebook.js index c34478adf..600efbaa1 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -1010,6 +1010,32 @@ function Facebookbot(configuration) { } }); }, + get_label_details: function(label_id, fields, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/' + label_id + '?access_token=' + configuration.access_token; + + if (fields && fields.length > 0) { + uri += '&fields=' + fields.join(","); + } + + request({ + method: 'GET', + json: true, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not get label details'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in getting label details call : ', body.error.message); + cb(body.error); + } else { + facebook_botkit.log('Successfully get label details'); + cb(null, body); + } + } + }); + }, }; facebook_botkit.api = { From fe295773e9721afea0f76efe8f9868601b09be48 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 14:36:24 +0100 Subject: [PATCH 11/14] Add get all labels --- lib/Facebook.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/Facebook.js b/lib/Facebook.js index 600efbaa1..4982da8cc 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -1036,6 +1036,32 @@ function Facebookbot(configuration) { } }); }, + get_all_label: function(fields, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/me/custom_labels?access_token=' + configuration.access_token; + + if (fields && fields.length > 0) { + uri += '&fields=' + fields.join(","); + } + + request({ + method: 'GET', + json: true, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not get labels'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in getting labels call : ', body.error.message); + cb(body.error); + } else { + facebook_botkit.log('Successfully get labels'); + cb(null, body); + } + } + }); + } }; facebook_botkit.api = { From efe3ca4de44fe61d28fa2e16c2e89f800aa457df Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 14:53:22 +0100 Subject: [PATCH 12/14] Add remove label --- lib/Facebook.js | 69 ++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index 4982da8cc..fcf79a6ae 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -866,14 +866,14 @@ function Facebookbot(configuration) { uri: uri }, function(err, res, body) { if (err) { - facebook_botkit.log('Could not send a broadcast message'); + facebook_botkit.log('Could not send the broadcast message'); cb(err); } else { if (body.error) { - facebook_botkit.log('ERROR in sending a broadcast message call : ', body.error); + facebook_botkit.log('ERROR in sending the broadcast message call : ', body.error); cb(body.error); } else { - facebook_botkit.debug('Successfully send a broadcast message', body); + facebook_botkit.debug('Successfully send the broadcast message', body); cb(null, body); } } @@ -882,27 +882,21 @@ function Facebookbot(configuration) { get_broadcast_metrics: function(broadcast_id, cb) { var uri = 'https://' + api_host + '/' + api_version + '/' + broadcast_id + '/insights/messages_sent?access_token=' + configuration.access_token; - request.get(uri, {}, function(err, res, body) { + request({ + method: 'GET', + json: true, + uri: uri + }, function(err, res, body) { if (err) { facebook_botkit.log('Could not send a broadcast message'); cb(err); } else { - var results = null; - try { - results = JSON.parse(body); - } catch (err) { - facebook_botkit.log('ERROR in getting broadcast metrics call: Could not parse JSON', err, body); - cb(err); - } - - if (results) { - if (results.error) { - facebook_botkit.log('ERROR in getting broadcast metrics call : ', results.error.message); - cb(results.error); - } else { - facebook_botkit.log('Successfully get broadcast metrics '); - cb(null, results); - } + if (body.error) { + facebook_botkit.log('ERROR in getting broadcast metrics call : ', body.error.message); + cb(body.error); + } else { + facebook_botkit.log('Successfully get broadcast metrics '); + cb(null, body); } } }); @@ -921,14 +915,14 @@ function Facebookbot(configuration) { uri: uri }, function(err, res, body) { if (err) { - facebook_botkit.log('Could not create a label to target broadcast messages'); + facebook_botkit.log('Could not create label to target broadcast messages'); cb(err); } else { if (body.error) { - facebook_botkit.log('ERROR in creating a label to target broadcast messages call : ', body.error); + facebook_botkit.log('ERROR in creating label to target broadcast messages call : ', body.error); cb(body.error); } else { - facebook_botkit.debug('Successfully create a label to target broadcast messages', body); + facebook_botkit.debug('Successfully create label to target broadcast messages', body); cb(null, body); } } @@ -997,14 +991,14 @@ function Facebookbot(configuration) { uri: uri }, function(err, res, body) { if (err) { - facebook_botkit.log('Could not get labels associated with a user'); + facebook_botkit.log('Could not get user associated labels'); cb(err); } else { if (body.error) { - facebook_botkit.log('ERROR in getting labels associated with a user call : ', body.error.message); + facebook_botkit.log('ERROR in getting user associated labels API call : ', body.error.message); cb(body.error); } else { - facebook_botkit.log('Successfully get labels associated with a user'); + facebook_botkit.log('Successfully get user associated labels'); cb(null, body); } } @@ -1061,6 +1055,29 @@ function Facebookbot(configuration) { } } }); + }, + remove_label: function(label_id, cb) { + var uri = 'https://' + api_host + '/' + api_version + '/' + label_id + '?access_token=' + configuration.access_token; + + request({ + method: 'DELETE', + json: true, + body: {}, + uri: uri + }, function(err, res, body) { + if (err) { + facebook_botkit.log('Could not remove label'); + cb(err); + } else { + if (body.error) { + facebook_botkit.log('ERROR in removing label API call : ', body.error); + cb(body.error); + } else { + facebook_botkit.debug('Successfully remove label', body); + cb(null, body); + } + } + }); } }; From f199a1babf50d1c7a9d6c01f23e4593e45e89955 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 16:58:57 +0100 Subject: [PATCH 13/14] Add custom label_id --- lib/Facebook.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index fcf79a6ae..727df5b55 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -852,13 +852,17 @@ function Facebookbot(configuration) { } }); }, - send: function(message_creative_id, cb) { + send: function(message_creative_id, custom_label_id, cb) { var uri = 'https://' + api_host + '/' + api_version + '/me/broadcast_messages?access_token=' + configuration.access_token; var body = { 'message_creative_id' : message_creative_id }; + if(custom_label_id) { + body['custom_label_id'] = custom_label_id; + } + request({ method: 'POST', json: true, From 669cec375d9db269d2fe9e2078c4106c12fc54ca Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Mon, 11 Dec 2017 16:59:39 +0100 Subject: [PATCH 14/14] Improve docs : How to use broadcast API --- docs/readme-facebook.md | 143 ++++++++++++++++++++++++++++++++++++++++ lib/Facebook.js | 2 +- 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/docs/readme-facebook.md b/docs/readme-facebook.md index 2a65358ec..e8494b5ef 100644 --- a/docs/readme-facebook.md +++ b/docs/readme-facebook.md @@ -21,6 +21,7 @@ Table of Contents * [Silent and No Notifications](#silent-and-no-notifications) * [Messenger code API](#messenger-code-api) * [Attachment upload API](#attachment-upload-api) +* [Broadcast Messages API](#broadcast-messages-api) * [Running Botkit with an Express server](#use-botkit-for-facebook-messenger-with-an-express-web-server) ## Getting Started @@ -570,6 +571,148 @@ var taggedMessage = { bot.reply(message, taggedMessage); ``` +## Broadcast Messages API + +Broadcast Messages API allows you to send a message to many recipients, this means sending messages in batches to avoid hitting the Messenger Platform's rate limit, which can take a very long time. + +More information about the Broadcast Messages API can be found [here](https://developers.facebook.com/docs/messenger-platform/send-messages/broadcast-messages) + +### Creating a Broadcast Message + +Messages sent with the Broadcast API must be defined in advance. + +To create a broadcast message : + +```javascript +/* Simple example */ +controller.api.broadcast.create_message_creative("Hello friend !", function (err, body) { + // Your awesome code here + console.log(body['message_creative_id']); + // And here +}); + +/* Personalizing Messages example */ +controller.api.broadcast.create_message_creative({ + 'dynamic_text': { + 'text': 'Hi, {{first_name}}!', + 'fallback_text': 'Hello friend !' + } +}, function (err, body) { + // Your awesome code here + console.log(body['message_creative_id']); + // And here +}); +``` + +### Sending a Broadcast Message + +To send a broadcast message, you call ```controller.api.broadcast.send(...)``` with the broadcast message ID. + +On success, Botkit will return a numeric broadcast_id that can be used to identify the broadcast for analytics purposes : + +```javascript +controller.api.broadcast.send('', null, function (err, body) { + // Your awesome code here + console.log(body['broadcast_id']); + // And here +}); +``` + +### Broadcast Metrics + +Once a broadcast has been delivered, you can find out the total number of people it reached by calling ```controller.api.broadcast.get_broadcast_metrics(...)```. + +```javascript +controller.api.broadcast.get_broadcast_metrics("", function (err, body) { + // Your awesome code here +}); +``` + +### Targeting a Broadcast Message + +By default, the Broadcast API sends your message to all open conversations with your Messenger bot. To allow you broadcast to a subset of conversations, the Broadcast API supports 'custom labels', which can be associated with individual PSIDs. + +#### Creating a Label + +To create a label : + +```javascript +controller.api.broadcast.create_label("", function (err, body) { + // Your awesome code here +}); +``` + +#### Associating a Label to a user + +To associate a label to a specific user : + +```javascript +controller.api.broadcast.add_user_to_label(message.user, "", function (err, body) { + // Your awesome code here +}); +``` + +#### Sending a Message with a Label + +To send a broadcast message to the set of users associated with a label : + +```javascript +controller.api.broadcast.send('', '', function (err, body) { + // Your awesome code here + console.log(body['broadcast_id']); + // And here +}); +``` + +#### Removing a Label From a user + +To remove a label currently associated with a user : + +```javascript +controller.api.broadcast.remove_user_from_label(message.user, '', function (err, body) { + // Your awesome code here +}); +``` + +#### Retrieving Labels Associated with a USER + +To retrieve the labels currently associated with a USER : + +```javascript +controller.api.broadcast.get_labels_by_user(message.user, function (err, body) { + // Your awesome code here +}); +``` + +### Retrieving Label Details + +To retrieve a single label : + +```javascript +controller.api.broadcast.get_label_details('', ['name'], function (err, body) { + // Your awesome code here +}); +``` + +#### Retrieving a List of All Labels + +To retrieve the list of all the labels for the page : + +```javascript +controller.api.broadcast.get_all_labels(['name'], function (err, body) { + // Your awesome code here +}); +``` + +#### Deleting a Label + +To delete a label : + +```javascript +controller.api.broadcast.remove_label('', function (err, body) { + // Your awesome code here +}); +``` ## 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/lib/Facebook.js b/lib/Facebook.js index 727df5b55..0c6392a78 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -1034,7 +1034,7 @@ function Facebookbot(configuration) { } }); }, - get_all_label: function(fields, cb) { + get_all_labels: function(fields, cb) { var uri = 'https://' + api_host + '/' + api_version + '/me/custom_labels?access_token=' + configuration.access_token; if (fields && fields.length > 0) {