From 1dfb9624cc2408d6ee9aa2fbd51cf4ab7d5d20b4 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Sun, 28 Oct 2018 16:54:09 +0100 Subject: [PATCH 1/2] Add Facebook Persona API to Botkit --- lib/Facebook.js | 137 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 2 deletions(-) diff --git a/lib/Facebook.js b/lib/Facebook.js index df155fa86..f9324b77d 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -37,6 +37,10 @@ function Facebookbot(configuration) { platform_message.recipient.id = message.channel; } + if (message.persona_id) { + platform_message.persona_id = message.persona_id; + } + if (!message.sender_action) { if (message.text) { platform_message.message.text = message.text; @@ -127,6 +131,7 @@ function Facebookbot(configuration) { var msg = {}; msg.channel = src.channel; msg.sender_action = 'typing_on'; + msg.persona_id = src.persona_id; bot.say(msg, cb); }; @@ -134,6 +139,7 @@ function Facebookbot(configuration) { var msg = {}; msg.channel = src.channel; msg.sender_action = 'typing_off'; + msg.persona_id = src.persona_id; bot.say(msg, cb); }; @@ -1042,7 +1048,6 @@ function Facebookbot(configuration) { } }; - var handover = { get_secondary_receivers_list: function(fields, cb) { @@ -1241,6 +1246,7 @@ function Facebookbot(configuration) { }); } }; + var broadcast_api = { create_message_creative: function(message, cb) { var uri = 'https://' + api_host + '/' + api_version + '/me/message_creatives?access_token=' + configuration.access_token; @@ -1688,6 +1694,132 @@ function Facebookbot(configuration) { } }; + var personas_api = { + create: function(persona) { + return facebook_botkit.api.personas.postAPI(persona); + }, + get_persona: function(persona_id) { + return facebook_botkit.api.personas.getAPI(persona_id); + }, + get_all_personas: function() { + return facebook_botkit.api.personas.getAPI(); + }, + delete_persona: function(persona_id) { + return facebook_botkit.api.personas.deleteAPI(persona_id); + }, + postAPI: function(persona) { + return new Promise(function(resolve, reject) { + var uri = 'https://' + api_host + '/me/personas?access_token=' + configuration.access_token; + + if (facebook_botkit.config.require_appsecret_proof) { + uri += '&appsecret_proof=' + appsecret_proof; + } + + request.post(uri, + { + form: persona + }, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not create a persona'); + reject(err); + } else { + var results = null; + + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in creating persona API call: Could not parse JSON', err, body); + reject(err); + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in creating a persona API call: ', results.error.message); + reject(results.error); + } else { + facebook_botkit.debug('Successfully created a persona ', body); + resolve(results); + } + } + } + }); + }); + }, + getAPI: function(persona_id) { + return new Promise(function(resolve, reject) { + + var endpoint = persona_id ? persona_id : 'me/personas'; + + var uri = 'https://' + api_host + '/' + endpoint + '?access_token=' + configuration.access_token; + + if (facebook_botkit.config.require_appsecret_proof) { + uri += '&appsecret_proof=' + appsecret_proof; + } + + request.get(uri, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not get the persona ' + persona_id); + reject(err); + } else { + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in getting persona API call: Could not parse JSON', err, body); + reject(err); + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in getting persona API call: ', results.error.message); + reject(results.error); + } else { + facebook_botkit.debug('Successfully got persona ', body); + resolve(results); + } + } + } + }); + }); + }, + deleteAPI: function(persona_id) { + return new Promise(function(resolve, reject) { + var uri = 'https://' + api_host + '/' + persona_id + '?access_token=' + configuration.access_token; + + if (facebook_botkit.config.require_appsecret_proof) { + uri += '&appsecret_proof=' + appsecret_proof; + } + + request.delete(uri, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not delete persona'); + reject(err); + } else { + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in deleting persona API call: Could not parse JSON', err, body); + reject(err); + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in deleting persona API call: ', results.error.message); + reject(results.error); + } else { + facebook_botkit.debug('Successfully deleted persona ', body); + resolve(results); + } + } + } + }); + }); + } + }; facebook_botkit.api = { 'user_profile': user_profile, @@ -1698,7 +1830,8 @@ function Facebookbot(configuration) { 'tags': tags, 'handover': handover, 'broadcast': broadcast_api, - 'insights': insights_api + 'insights': insights_api, + 'personas': personas_api, }; From 705063ee6d7e3e42c2e415576b5bfbfa1f295112 Mon Sep 17 00:00:00 2001 From: Ouadie Lahdioui Date: Sun, 28 Oct 2018 18:01:21 +0100 Subject: [PATCH 2/2] Add some examples on how to use FB Personas API --- examples/facebook_bot.js | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/examples/facebook_bot.js b/examples/facebook_bot.js index bb7017c0d..1682da12d 100755 --- a/examples/facebook_bot.js +++ b/examples/facebook_bot.js @@ -131,6 +131,70 @@ controller.setupWebserver(process.env.port || 3000, function(err, webserver) { }); }); +/** + * FaceBook Personas API + */ +controller.hears('get all personas', 'message_received', function(bot, message) { + + controller.api.personas.get_all_personas() + .then(result => { + bot.startTyping({ + channel : message.channel, + persona_id : result.id + }); + + result.data.forEach((persona) => { + bot.reply(message, { + 'text' : 'Message send from ' + persona.name + ' at ' + new Date().toISOString(), + 'persona_id' : persona.id + }); + }); + + }) + .catch(reason => { + bot.reply(message, "Oops something was wrong"); + }); +}); + +controller.hears('delete latest persona', 'message_received', function(bot, message) { + + controller.api.personas.get_all_personas() + .then(result => { + if(result.data.length > 0) { + var persona = result.data[result.data.length - 1]; + controller.api.personas.delete_persona(persona.id) + .then(result => { + bot.reply(message, 'Done, the persona ' + persona.name + ' is deleted'); + }); + } else { + bot.reply(message, 'No persona found to delete'); + } + }) + .catch(reason => { + bot.reply(message, "Oops something was wrong"); + }); +}); + +controller.hears(['create persona'], 'message_received', function(bot, message) { + + controller.api.personas.create({ + 'name': 'Botkit', + 'profile_picture_url': 'https://pbs.twimg.com/profile_images/803642201653858305/IAW1DBPw_400x400.png' + }) + .then(result => { + bot.reply(message, { + 'text' : 'Hello from the new created persona :)', + 'persona_id' : result.id + }); + }) + .catch(reason => { + bot.reply(message, "Oops something was wrong"); + }); +}); + +/** + * End of FaceBook Personas API + */ controller.hears(['attachment_upload'], 'message_received', function(bot, message) { var attachment = {