Facebook Messenger Platform App
Node API adapter in ECMAScript 2015 (ES6)
Installation
Install the fb-messenger-app
package in your node server app's local node_modules
folder.
npm install --Save fb-messenger-app
How to start
I take for granted that you've already setup your Messenger Platform app:
- Created a Facebook App and Page
- Setup the Webhook
- Got a Page Access Token
- Subscribed the App to the Page
After installing the fb-messenger-app package with npm as shown above, import it in your app
const MessengerApp =
Then create a new messenger instance passing your Facebook App page access token (this token will include all messenger permissions)
var messenger = MY_PAGE_ACCESS_TOKEN
Receive Messages
You'll have to listen for POST calls at your webhook. Callbacks will be made to this webhook. For this purpose, _handleCallback
will listen and dispatch each callback type by emitting its corresponding event.
app
Send Messages to the user
All messages must be send through the sendApiMessage
method. It'll make POST calls to facebook's Graph API with your app's page access token, and a JSON payload with the 'recipient's id' and the message object to be send. For more information about the payload supported by facebook go ahead an read more here.
messenger var myImage = attachment: type: 'image' payload: url: 'https://petersapparel.com/img/shirt.png' messenger
Using callbacks
A successful send API request returns a JSON with the identifier of the user and the message.
On the other hand, a when the send API request fails, a JSON is returned with the corresponding error code and message. Messenger Platform errors are grouped by code, with a different message depending on the error condition. Read more here
Finally, an example would be the following.
messenger
Notifications
Optionally, depending on the case, sometimes you'll want to bring the user attention with a normal push. Other times, a silent notification would be enough, and, why not, it would be appropiate not to bother the user at all. We can achieve this by adding the notificationType parameter.
Types
If missing, a regular push notification will be sent.
- REGULAR : will emit a sound/vibration and a phone notification
- SILENT_PUSH : will just emit a phone notification
- NO_PUSH : will not emit either
messengermessengermessenger
Configure the notification type default
Set the bot's default notification type when instantiating it.
var messenger = MY_PAGE_ACCESS_TOKEN 'SILENT_PUSH'
Sender Actions
Set typing indicators or send read receipts.
- mark_seen : Mark last message as read
- typing_on : Turn typing indicators on
- typing_off : Turn typing indicators off
messengersenderAction
You can send specific actions during given time intervals.
Make the bot type for a given period of time
messengersenderAction
Mark the last message as read when after a given timeout
messengersenderAction
Verification
There are two moments where you'll want to verify that things are fine.
validate your webhook to make sure you're subscribed to any change
app
validate the requests signature to make sure they come from facebook (express.js)
const app = app
API
Constructor
var messenger = token options
The following table describes the properties of the options
object.
Property | Description | Type | Default |
---|---|---|---|
notificationType | Determines how messages will be pushed to the user | String | 'REGULAR' |
apiUrl | Facebook's Graph API | String | 'https//graph.facebook.com/v2.6/' |
Methods
messenger messenger messenger messenger messenger messenger messengerthreadSetting messengerthreadSetting messengerthreadSetting messengerthreadSetting messengerthreadSetting messengerthreadSetting messengersenderAction messengersenderAction messengersenderAction messenger messenger messengerverify messengerverify