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

psid-to-fbid

v1.2.0

Published

Get/map psid to fbid using Facebook messenger's webhook

Downloads

13

Readme

Mapping PSID to FBID

This package allows you to match page-specific id (psid) with user's real id (id starts with 1000...) from webhook events.

Requirement: You must have admin or editor role on that page.

Getting started

This package uses Android or iOS access_token. You can generate one by using this code:

const PsidToFbid = require('psid-to-fbid');
const email = "[email protected]";
const password = "this_is_my_facebook_password"
console.log(PsidToFbid.genLoginURLiOS(email, password));

The code will print out an URL. You must access this URL using a browser, on client side, in order to prevent being locked down your facebook account.

The code will return some JSON stuffs. Just find the pattern starts with EAAA.... That's your access_token.

This action should be done every time you change your password, since facebook will invalidate all your access_tokens.


How to use

npm install psid-to-fbid --save

For example you have a page with id = 182794865548469, and your Android token is EAAAAUaZA8jlABAIv...

Firstly, you must setup the package like this:

const PsidToFbid = require('psid-to-fbid');
const psidToFbid = new PsidToFbid("182794865548469")
psidToFbid.fetchPageToken("EAAAAUaZA8jlABAIv...")
	.then((page_token) => {
	    console.log("Setup complete", page_token);
	}).catch(() => {
	    console.log("Setup failed");
	})

After that, you can use these functions:


getFromWebhookEvent(messaging_event)

Get fbid from messaging_event of webhook

This does NOT work with postback from buttons / Get Started button (but works with quick replies)

Arguments

  • @param {Object} messaging_event messaging_event from webhook.
  • @returns {Promise} Returns Promise resolve(fbid), with fbid = null if there's an error.

Example

app.post('/webhook/', function (req, res) {
	let messaging_events = req.body.entry[0].messaging;
	for (let i = 0; i < messaging_events.length; i++) {
		let event = req.body.entry[0].messaging[i];
		let psid = event.sender.id;
		psidToFbid.getFromWebhookEvent(event).then(fbid => {
		    console.log("Got psid = "+psid+", fbid = "+fbid);
		})
	}
	res.sendStatus(200);
})

getFromMid(mid, psid)

Get fbid from mid (message_id)

For example, each time you send a message via /me/messages endpoint, you receive an object which contains message_id. The message_id can then be passed into getFromMid to get fbid of the receiver.

Arguments

  • @param {String} mid message_id.
  • @param {String} psid Optional, user's psid. Used for cache feature.
  • @returns {Promise} Returns Promise resolve(fbid), with fbid = null if there's an error.

Example

request({
	url: 'https://graph.facebook.com/v2.6/me/messages',
	qs: {access_token:token},
	method: 'POST',
	json: {
		recipient: {id:psid},
		message: messageData,
	}
}, function(error, response, body) {
	if (!error && !response.body.error && response.body.message_id) {
	    var mid = response.body.message_id;
		psidToFbid.getFromMid(mid, psid).then(fbid => {
		    console.log("Got psid = "+psid+", fbid = "+fbid);
		})
	}
})

Author

  • ngxson (Nui Nguyen)
  • Email: contact at ngxson dot com
  • My website: https://ngxson.com