Created
May 26, 2017 14:49
-
-
Save georgeportillo/0c2b9312e68a7cd468d64b9f72223366 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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