fb-messenger-es6
Library to work with Facebook Messenger APIs.
Table of Contents
Setup
Install
npm install fb-messenger-es6
Import
;
Initialize
const facebook = <PAGE_ACCESS_TOKEN>;
Using proxy
const facebook = <PAGE_ACCESS_TOKEN> hostname:<PROXY_HOSTNAME> port: <PROXY_PORT> ;
Specify the Graph API version
Accepted versions: v2.6
, v2.7
, v2.8
, v2.9
const facebook = <PAGE_ACCESS_TOKEN> null API_VERSION;
Creating facebook app
Conversation
Sending Messages
Text Message
// basic messagefacebook; // message with quick replyfacebook;
Sender Actions
// Mark last message as readfacebook; // Toggle typing indicators on or offfacebook; // true - on; false - off // Alternativelyfacebook;
Attachment Reuse
const attachmentId = facebook; // subsequent calls using attachment_idfacebook;
Supported Messages
- Text Messages
- Rich Media Messages (with attachment reuse)
- Image
- Video
- Audio
- File
- Structured Messages
- Button Template
- Generic Template
- List Template
- Buttons
- URL button
- Postback button
- Call button
- Share button
- Log In and Log Out buttons
- Quick Replies (works in text, rich media & structured messages)
More to be added later
Receiving Messages
This section has code snippets for an express.js app
You should validate the signature before proceeding
;;...const validator = APP_SECRET;app;
You can process the callbacks using ProcessIncoming
which returns a minimal version of the callback to make it easier to handle
;...app;
ProcessIncoming
Messages are grouped by PAGE_ID and contain callback default fields (sender
, recipient
, timestamp
, type
) and additional callback specific fields
PAGE_ID: sender: 'USER_ID' recipient: 'PAGE_ID' timestamp: 1458692752478 type: '???' // message or message_read or message_echo or message_delivery or postback or account_linking ...CALLBACK_SPECIFIC_FIELDS
Below are the various CALLBACK_SPECIFIC_FIELDS
returned after processing a callback:
Message
The callback default field type
is message
Text Message
...CALLBACK_DEFAULT_FIELDS text: 'hello, world!' quick_reply: 'DEVELOPER_DEFINED_PAYLOAD' // optional
Rich Media Messages
NOTE: attachment.type
can be image
, audio
, video
or file
...CALLBACK_DEFAULT_FIELDS attachments: type: '???' // image or audio or video or file url: 'ATTACHMENT_URL'
Stickers
When processing sticker messages, pass in your stickers map to text so you get sticker_type
...CALLBACK_DEFAULT_FIELDS attachments: type: 'sticker' sticker_id: 369239343222814 sticker_type: 'LIKE'
Location
...CALLBACK_DEFAULT_FIELDS attachments: type: 'location' long: '-79.411079' lat: '43.761539'
Message Delivered, Message Read & Message Echo
The callback default field type
changes to message_delivery
, message_read
& message_echo
respectively.
There are no callback specific fields
Postback
The callback default field type
is postback
NOTE: As this could contain referral attribute, implementation could prioritize that over the payload
...CALLBACK_DEFAULT_FIELDS payload: 'USER_DEFINED_PAYLOAD' source: 'SHORTLINK' ref: 'REF_DATA_SPECIFIED' source: 'SOURCE' // e.g 'SHORTLINK', 'ADS', 'MESSENGER_CODE' or 'DISCOVER_TAB' ad_id: 'AD_ID' // if source === 'ADS' type: 'OPEN_THREAD'
Account Linking Event
The callback default field type
is account_linking
...CALLBACK_DEFAULT_FIELDS status: 'linked' authorization_code: 'PASS_THROUGH_AUTHORIZATION_CODE'
Referral
The callback default field type
is referral
...CALLBACK_DEFAULT_FIELDS source: 'SHORTLINK' ref: 'REF_DATA_SPECIFIED' source: 'SOURCE' // e.g 'SHORTLINK', 'ADS', 'MESSENGER_CODE' or 'DISCOVER_TAB' ad_id: 'AD_ID' // if source === 'ADS' type: 'OPEN_THREAD'
User Profile
// if no second arg, it defaults to all// possible fields ['first_name', 'last_name', 'profile_pic', 'locale', 'timezone', 'gender', 'is_payment_enabled', 'last_ad_referral']facebook; // To specify fields, pass in a second argfacebook;
Messenger Profile
;
Setting properties
facebook;
Reading properties
facebook;
Deleting properties
facebook;
Contributing
If you would like to contribute, you can fork the repository and send us pull reqs.
When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible.