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

Add Facebook Personas API #1497

Merged
merged 2 commits into from
Dec 14, 2018
Merged
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
64 changes: 64 additions & 0 deletions examples/facebook_bot.js
Original file line number Diff line number Diff line change
@@ -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 = {
137 changes: 135 additions & 2 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
@@ -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,13 +131,15 @@ function Facebookbot(configuration) {
var msg = {};
msg.channel = src.channel;
msg.sender_action = 'typing_on';
msg.persona_id = src.persona_id;
bot.say(msg, cb);
};

bot.stopTyping = function(src, cb) {
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,
};