Package Exports
- fb-messenger-bot-api
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fb-messenger-bot-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
fb-messenger-bot-api
NodeJS Facebook Messenger API
Installation
npm install fb-messenger-bot-api
Table of Contents
- Features
- Setup
- Sending Messages
- Setting Messenger Profile
- Validating Facebook Webhook
- Complete Example
- Creating Facebook App
Features
- Promises and callback support on all functions, if no callback provided, promise returned, allows you to manage flow as you desire AND to ensure message ordering
- Supports proxying
- ES6+ code
- Using latest Facebook API v2.10
Setup
Import
const facebook = require('fb-messenger-bot-api');
Sending Messages
Initialize
const client = new facebook.MessagingClient(process.env.PAGE_ACCESS_TOKEN);
Using proxy
const client = new facebook.MessagingClient(process.env.PAGE_ACCESS_TOKEN, { hostname:process.env.PROXY_HOST, port: process.env.PROXY_PORT });
Defaults to http
if no protocol provided
Text Message
client.sendTextMessage(senderId, <MESSAGE>)
.then((result) => ...)
Image Message
client.sendImageMessage(senderId, <IMAGE_URL>)
.then((result) => ...)
This method will have the image cached by facebook so every receiver after the first will get it quickly.
Buttons Message
client.sendButtonsMessage(senderId, <BUTTONS TEXT> [<ARRAY OF BUTTONS>])
.then((result) => ...)
Quick Reply Message
client.sendQuickReplyMessage(senderId, <TEXT>, [<QUICK_REPLIES>])
.then((result) => ...)
Generic Template Message ( Horizontal Scroll List)
client.sendGenericTemplate(senderId, [ELEMENTS])
.then((result) => ...)
Generic Template element format
List Message ( Vertical Scroll List)
client.sendListMessage(senderId, [ELEMENTS], <firstElementStyle>, [FINAL_BUTTONS])
.then((result) => ...)
firstElementStyle
is optional. If not provided defaults to large
Mark as Seen
client.markSeen(senderId);
As per all methods, callback can be provided. If no callback provided returns promise. Recommended to send and continue processing without waiting for reply.
Toggle writing bubble
client.toggleTyping(senderId, <true/false>);
As per all methods, callback can be provided. If no callback provided returns promise. Recommended to send and continue processing without waiting for reply.
Defaults to false
if no boolean parameter provided.
User Profile
client.getUserProfile(senderId,[<PROPERTIES>])
.then((result) => ...)
Valid properties: first_name
,last_name
,profile_pic
,locale
,timezone
,gender
,is_payment_enabled
,last_ad_referral
If none are given defaults to first_name
only.
Setting Messenger Profile
Initialize
const client = new facebook.Profile(process.env.PAGE_ACCESS_TOKEN);
Using proxy
const client = new facebook.Profile(process.env.PAGE_ACCESS_TOKEN, { hostname:process.env.PROXY_HOST, port: process.env.PROXY_PORT });
Setting Greeting Message
client.setGreetingMessage('Message that will be visible first thing when opening chat window with your bot/page')
.then((result) => ...)
Setting Get Started button
client.setGetStartedAction(senderId, payload)
.then((result) => ...)
payload
is the value that will be first sent when new user sends first message, once per user interaction
Setting Persistent Menu
client.setPersistentMenu(senderId, [<MENU_ENTRIES>])
.then((result) => ...)
This is a burger menu appearing next to the chat input field where users can click and get direct interaction shortcuts to specific functionality of the bot. Persistent menu format
Validating Facebook Webhook
const facebook = require('fb-messenger-bot-api');
const router = require('express').Router();
router.get('/api/webhook',facebook.ValidateWebhook.validate);
Example based on usage with Express Router, can use any other middleware which passes in the req and response objects.
Complete example
const router = require('express').Router();
const facebook = require('fb-messenger-bot-api');
const client = new facebook.MessagingClient(process.env.PAGE_ACCESS_TOKEN);
...
router.get('/api/webhook',facebook.ValidateWebhook.validate);
...
client.markSeen(senderId)
.then(() => client.toggleTyping(senderId,true))
.catch((err) => console.log(error));
...
//promise based reaction on message send confirmation
client.sendTextMessage(senderId, 'Hello')
.then((result) => console.log(`Result sent with: ${result}`));
...
//callback based reaction on message confirmation
client.sendTextMessage(senderId, 'Hello',(result) => console.log(`Result sent with: ${result}`));
...
//silent message sending
client.sendTextMessage(senderId,'Hello');