npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fb-page-comment-event

v1.0.0

Published

Run a schedule job to track your Facebook page Posts' comment/reply events.

Downloads

7

Readme

fb-page-comment-event

npm version node Codecov branch Build

dependencies Status devDependencies Status

Project Page


Description

This module can keep track your Facebook page posts' first level comment events. Hence, if someone commented on your post, you will be notified by an event.

This module support running an event-engine which would manage a background schedule job to check new comment and generating events. This module also support raw APIs which you can check new comments and digest as events by yourself via APIs.


Running the event engine

You may use the lib.pageCommentEventApp(options) API to get a event engine app and then start it with run function. This event engine app would manage a schedule job to auto pull data.

Sample:

'use strict';

let fbPageCommentEventLib = require('fb-page-comment-event');

const pageCommentEventApp = fbPageCommentEventLib.pageCommentEventApp({
    accessToken: 'EAASHAlSbbMUBALwwZCxZB1T7eDvFZBR......GQfNPb5Bxo5b2wdzMb45gJxcdZAFOQZDZD',
    pullInterval: 15 * 1000
});

pageCommentEventApp.registerMonitorPost({ pageId: 'xxxxx', postId: 'yyyyy' });

pageCommentEventApp.run((events) => {
    console.log(JSON.stringify(events, null, 2));
    return;
});
  • Remarks: pageId is the facebook page ID. postId is the facebook page post ID of post which you want to monitor its comment. accessToken is the Facebook page access token(remember to use the long live token) which Facebook page owner can generate in developer dashboard.

Sample console log output:

>node sample.js
{"message":"2017-12-21T19:18:48.701Z: Fetching feed items with comments newer than lastScannedCommentTime(2017-12-21T19:14:18.000Z).","level":"info"}
{"message":"2017-12-21T19:18:48.960Z: Fetched and filtered and returning 1 feed items which containing new comments. \"newLastScannedCommentTime\" reset to 2017-12-21T19:14:18.000Z.","level":"info"}
[
  {
    "eventType": "comment",
    "data": {
      "postId": "xxxxxx",
      "commentId": "yyyyyy",
      "from": {
        "name": "zzzzzz",
        "id": "aaaaaa"
      },
      "commentCreateTime": 1513883658000,
      "message": "test comment",
      "link": "https://www.facebook.com/permalink.php?story_fbid=xxxxxx&id=aaaaaa&comment_id=cccccc"
    }
  }
]

(DIY) Use the APIs to check new comments and generating events

Sample:

let fbPageCommentEventLib = require('fb-page-comment-event');

let pageId = 'aaa';
let postId = 'bbb';
let accessToken = 'XXX...ZZZ';

let postDigestor = lib.api.getPostDigestor();

let since = Math.trunc(Date.now() / 1000) - 10 * 60; //since 10 minutes before

(async function () {

    let queryPostCommentAgent = lib.api.getQueryPostCommentAgent({ accessToken }); //initialize query agent with access token
    let postCommentFetcher = lib.api.getPostCommentFetcher(queryPostCommentAgent); //initial comment fetcher

    let postWithNewComments = await postCommentFetcher.fetch(`${pageId}_${postId}`, since); //fetch target post with new comments
    let postWithNewCommentEvents = await postDigestor.digest(postWithNewComments); //digest the post object into new comment events

    console.log(`postWithNewCommentEvents: ${JSON.stringify(postWithNewCommentEvents, null, 2)}`);
})();

event engine APIs

let app = lib.pageCommentEventApp({accessToken, pullInterval})

Description:

Initializing the event engine app.

parameters:

  • accessToken: The required accessToken(need permission manage_pages) for the API calls.
  • pullInterval: The sleep time(ms) interval between each scheduled page post new comment checking.

return:

The event engine app.

app.registerMonitorPost({pageId, postId})

Description:

Registering a page post which the event engine would tracking for new comments

parameters:

  • pageId: The page ID.
  • postId: The post ID.

app.run(eventsCallback)

Description:

Start running the event engine and register the new-comment-events callback. Once users write new comments, new-comment-events would be fired and handled by the callback.

parameters:

  • eventsCallback: The batch new-comment-events handling function.

app.stop()

Description:

Stop the event engine


DIY APIs

let queryPostCommentAgent = lib.api.getQueryPostCommentAgent(options)

Description:

Initialize a agent for query post comments.

parameters:

  • options: object with attribute accessToken which holding the required accessToken(need permission manage_pages) for the API calls.

return:

The agent object.

let postCommentFetcher = lib.api.getPostCommentFetcher(queryPostCommentAgent)

Description:

Initialize a fetcher for fetching new post comment.

parameters:

  • queryPostCommentAgent: The agent object which come from lib.api.getQueryPostCommentAgent(options)

return:

The fetcher object.

let postCommentObj = await postCommentFetcher.fetch(postObjId, since)

Description:

Use the fetcher object to fetch post object with comments. We can use this API to get all new comments under the target post which those comments are created since the since time.

parameters:

  • postObjId: The post object ID which is in format of ${pageId}_${postId} in Facebook Graph API.
  • since: The timestamp in second

return:

The post object with comments (All comments child objects are new comments).

let postDigestor = lib.api.getPostDigestor();

Description:

Getting a digestor object which can turn the post-comment object from postCommentFetcher.fetch() result into new-comment-events.

return:

The digestor object.

let newCommentEvents = await postDigestor.digest(postWithNewComments)

Description:

Use the digestor object to digest post-comment object into new-comment-events

parameters:

  • postWithNewComments: The post object with attaching new comments.

return:

New comment event objects


Author