1.2.4 • Published 6 months ago

nubitel-messenger-api v1.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Node.js Facebook Messenger API

Based on the Messenger Platform Sample

Install

npm i nubitel-messenger-api --save

Usage

See Setup for more details about setting up the bot/page.

Example

Get User Profile - User Profile API

const messengerAPI = require("nubitel-messenger-api");
const messenger = new messengerAPI().Messenger({
	appId: "",
	pageId: "",
	analyticsLogLevel: "",
	appSecret: "",
	pageAccessToken: "",
	validationToken: "",
});

var pageScopedUserID = "...";

messenger.getUserProfile(pageScopedUserID, (err, resp) => {
	if (err) {
		console.error(recipientId, "Sorry, looks like the backend is down :-(");
	} else {
		console.log(JSON.parse(resp));
	}
});

Send Generic Template Message - Generic Template

messenger.sendGenericMessage(pageScopeUserID, [
	{
		title: "Welcome to Peter's Hats",
		image_url: "https://petersfancybrownhats.com/company_image.png",
		subtitle: "We've got the right hat for everyone.",
		default_action: {
			type: "web_url",
			url: "https://peterssendreceiveapp.ngrok.io/view?item=103",
			messenger_extensions: true,
			webview_height_ratio: "tall",
			fallback_url: "https://peterssendreceiveapp.ngrok.io/",
		},
		buttons: [
			{
				type: "web_url",
				url: "https://petersfancybrownhats.com",
				title: "View Website",
			},
			{
				type: "postback",
				title: "Start Chatting",
				payload: "DEVELOPER_DEFINED_PAYLOAD",
			},
		],
	},
]);

Using Webhook Handler to receive Facebook Messenger Events - Webhook Reference

The example folder contains a sample app

npm install
cd example
export MESSENGER_ANALYTICS_LOG_LEVEL = "2";
export MESSENGER_APP_ID = "--yours--";
export MESSENGER_PAGE_ID = "--yours--";
export MESSENGER_APP_SECRET="--yours--"
export MESSENGER_VALIDATION_TOKEN="--yours--"
export MESSENGER_PAGE_ACCESS_TOKEN="--yours--"
node server

Or use this in your existing code

const express = require("express");

var ignores = ["/some-url/to-ignore"];
var verifySignature = true;

const messengerAPI = require("nubitel-messenger-api");
const messenger = new messengerAPI().Messenger({
	appId: "",
	pageId: "",
	analyticsLogLevel: "",
	appSecret: "",
	pageAccessToken: "",
	validationToken: "",
});
const webhookHandler = new messengerAPI().Webhook(
	messenger,
	{
		receivedAuthentication: function (event) {
			console.log("receivedAuthentication", event);
		},
		handleMessage: function (event) {
			console.log("handleMessage", event);
			messenger.sendTextMessage(event.sender.id, JSON.stringify(event));
		},
		receivedDeliveryConfirmation: function (event) {
			console.log("receivedDeliveryConfirmation", event);
		},
		receivedPostback: function (event) {
			console.log("receivedPostback", event);
		},
		receivedMessageRead: function (event) {
			console.log("receivedMessageRead", event);
		},
		doLinking: function (event) {
			console.log("doLinking", event);
		},
		doUnlinking: function (event) {
			console.log("doUnlinking", event);
		},
	},
	verifySignature,
	ignores,
	express.Router()
);

var app = express();
app.set("port", process.env.PORT || 3000);
app.use(express.static("public"));

app.use("/fb", webhookHandler);
app.listen(app.get("port"), function () {
	console.log("Node app is running in http mode on port", app.get("port"));
});

Analytics Events

You can now send analytics events through the following API (See App Events with Bots for Messenger - Logging Custom Events and App Events API for more details about the event types)

For performace reasons if you would like to throttle the events that you would like to send/track, you can use the env variable

export MESSENGER_ANALYTICS_LOG_LEVEL = "2";

to control the log levels. Set that to

LevelValueDescription
None99Don't send any analytics events
Critical2Send only critical analytics events
Verbose1Send all analytics events

The asynchronous callback from the analyticsEvent call would be either

{
	"success": true
}

if the event was successfully accepted or

{
	"skip": true
}

if the event was skipped due lower log levels

  • Custom Event
messenger.analyticsEvent(
	messengerapi.ANALYTICS_LEVEL_VERBOSE,
	event.sender.id,
	() => {
		return messenger.buildAnalyticsEvent("fb_mobile_verbose_event");
	},
	(err, resp) => {
		messenger.sendTextMessage(event.sender.id, JSON.stringify(err ? err : resp));
	}
);
  • Standard Purchase Event
messenger.analyticsEvent(
	messengerapi.ANALYTICS_LEVEL_CRITICAL,
	event.sender.id,
	() => {
		return messenger.buildAnalyticsEvent("fb_mobile_purchase", { _valueToSum: 9.99, fb_currency: "USD" });
	},
	(err, resp) => {
		messenger.sendTextMessage(event.sender.id, JSON.stringify(err ? err : resp));
	}
);
  • Standard Add Cart Event Using quickAnalytics
messenger.quickAnalytics(messengerapi.ANALYTICS_LEVEL_CRITICAL, event.sender.id, "fb_mobile_add_to_cart", {
	fb_content_type: "blah blah blah",
	fb_content_id: "123456789",
	_valueToSum: 9.99,
	fb_currency: "USD",
});
1.2.4

6 months ago

1.2.3

7 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.28

7 months ago

1.1.27

7 months ago

1.1.26

7 months ago

1.1.25

7 months ago

1.1.24

7 months ago

1.1.23

7 months ago

1.1.22

7 months ago

1.1.21

7 months ago

1.1.20

7 months ago

1.1.19

7 months ago

1.1.18

7 months ago

1.1.17

7 months ago

1.1.16

7 months ago

1.1.15

7 months ago

1.1.14

7 months ago

1.1.13

7 months ago

1.1.12

7 months ago

1.1.11

7 months ago

1.1.10

7 months ago

1.1.9

7 months ago

1.1.8

7 months ago

1.1.7

7 months ago

1.1.6

7 months ago

1.1.5

7 months ago

1.1.4

7 months ago

1.1.3

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.18

7 months ago

1.0.17

7 months ago

1.0.16

7 months ago

1.0.15

7 months ago

1.0.13

7 months ago

1.0.12

7 months ago

1.0.11

7 months ago

1.0.10

7 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago