function fbWebhookPost()

in SampleIntegrations/SampleBots/NodeJS/FileAnIssue/app.js [145:199]


function fbWebhookPost(req, res) {
  //console.log(JSON.stringify(req.body, null, 2));
    if(req.body && req.body.entry) {
        for(var i in req.body.entry) {
            var changes = req.body.entry[i].changes;
            for(var j in changes) {
                // Changes field type = 'posts' for new posts
                if(changes[j].field && changes[j].field === 'mention') {
                    if(changes[j].value && changes[j].value.item && changes[j].value.item == 'comment') {
                        // comment
                        var comment_id = changes[j].value.comment_id;
                        var comment_message = changes[j].value.message;
                        console.log('Mentioned in comment', comment_id, comment_message);

                        // Get the content of the parent post
                        var post_id = changes[j].value.post_id;
                        graphapi({
                            url: '/' + post_id,
                            qs: { 'fields': 'message,permalink_url' }
                        },function(error,response,body) {
                            if(body) {
                                var post = JSON.parse(body);
                                likePostOrCommentId(comment_id);
                                createGithubIssue(comment_message, post.message, comment_id, post.permalink_url);
                            }
                        });
                    } else if(changes[j].value && changes[j].value.item && changes[j].value.item == 'post') {
                        // post
                        var postid = changes[j].value.post_id;
                        console.log('Mentioned in post', postid);

                        // Get the content of the post
                        graphapi({
                            url: '/' + postid,
                            qs: { 'fields': 'message,from{name,email},formatting,permalink_url' }
                        },function(error,response,body) {
                            if(body) {
                                var post = JSON.parse(body);
                                likePostOrCommentId(post.id);
                                createGithubIssue('New Issue', post.message, post.id, post.permalink_url);
                            }
                        });
                    }
                } else {
                    // Not a mention webhook, do something else here
                    console.log('Not a mention webhook, do something else here');
                }
            }
        }
    } else {
        console.error('Webhook Callback',req.body);
    }
    // Always send back a 200 OK, otherwise Facebook will retry the callback later
    res.sendStatus(200);
}