Skip to content

Instantly share code, notes, and snippets.

@georgeportillo
Created May 26, 2017 14:49
Show Gist options
  • Save georgeportillo/0c2b9312e68a7cd468d64b9f72223366 to your computer and use it in GitHub Desktop.
Save georgeportillo/0c2b9312e68a7cd468d64b9f72223366 to your computer and use it in GitHub Desktop.
/**
* sendEngagement - Sends an engagement message to all users.
*
* @param {Object} req The request object.
* @param {Object} res The response object.
*/
export function sendEngagement(req, res) {
let engagement = req.body.engagement;
Bot.findOne({
pageId: engagement.pageId
}).exec().then(bot => {
if (!bot) {
return;
}
let engagementRef = new Engagement(engagement);
engagementRef.save()
.then(newEngagement => {
if(_.isEmpty(newEngagement.intents)) {
return;
}
let initialResponse =
newEngagement.intents.find(intent => intent.getStarted);
Follower.find({
pageId: engagementRef.pageId,
subscriptionType: 'realtime'
})
.exec()
.then(followers => {
followers.forEach(follower => {
// TODO: Needs to handle collection content
let output = formatResponse(initialResponse.messages);
const informationOject = {
bot: bot,
sender: follower.userId,
};
sendBotFacebookResponse(output, informationOject);
});
});
return res.status(200).json(newEngagement);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment