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

Add Facebook insights API #1183

Merged

Conversation

ouadie-lahdioui
Copy link
Collaborator

Hello,

This PR add the new FB insights API to the awesome Botkit.

Fix #1126

Enjoy

lib/Facebook.js Outdated
uri: uri
}, function (err, res, body) {
if (err) {
facebook_botkit.log('Could call Facebook Insights API');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this log message is wrong.
Also logging err.message helps with debugging.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch 👍

} else {
if (body.error) {
facebook_botkit.log('ERROR in Facebook Insights API call: ', body.error.message);
cb(body.error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

body.error is not an instance of Error, is it?
If it isn't, then it would probably be better to use cb(new Error(body.error.message));

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's not, body.error is the error object returned by FB, it contains other useful data than message.

If we call the callback with an error object and juste the message, we couldn't use those useful data (code, ...).

@@ -488,6 +488,11 @@ controller.hears(['send tagged message'], 'message_received', function (bot, mes
bot.reply(message, taggedMessage);
});

controller.hears(['insights'], 'message_received', function (bot, message) {
controller.api.insights.get_insights(['page_messages_active_threads_unique', 'page_messages_blocked_conversations_unique'], null, null, function (err, body) {
bot.reply(message, JSON.stringify(body.data));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit of error handling would be nice here, especially because many people use examples with little modification.
body can be undefined, if that's the case then accessing body.data would throw an error.

if (err) {
    bot.reply(message, 'Insights error');
} else {
    bot.reply(message, JSON.stringify(body.data));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks nice, done 👍

@ouadie-lahdioui
Copy link
Collaborator Author

@Naktibalda thanks for the review 👍

@benbrown benbrown merged commit 85ae1c9 into howdyai:master Jan 18, 2018
@ouadie-lahdioui ouadie-lahdioui deleted the feature/FbMessagingInsightsAPI branch January 19, 2018 10:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants