SDK of the Facebook Messenger Platform for Node.js
The Facebook Messenger Platform SDK can be installed with NPM.
$ npm install facebook-bot-messenger
Require the SDK
var MessengerPlatform = ;
Create bot server with bot client instance (support on v.1.1.x)
sample is following
var bot = MessengerPlatform;bot;bot;bot;
Using with Node http server
var server = ;var bot = MessengerPlatform;bot;bot;server;
Using with Express 3/4
var app = ;var server = ;var bot = MessengerPlatform;app;bot;server;
Create the bot client instance
Instance of bot client is a handler of the Messenger Platform.
var bot = MessengerPlatform;
You can call API through the bot client instance.
Get detail information of user.
When MessengerPlatform#getProfile() success return JSON object.
Send readed message
Send typing on
Send typing off
sample is following
This procedure sends a message to the destination that is associated with .
More advanced sample is below;
var textMessageBuilder = '<message>';bot;
And other send method;
Send image attachment message
Send audio attachment message
Send video attachment message
Send file attachment message
Type of message depends on the type of instance of MessageBuilder. That means this method sends text message if you pass TextMessageBuilder
The type of instance of MessageBuilder
var builder = '<message>';
var builder = 'Pick a color:';builder;
var builder = ;builder;
Template attachment message:
var element = 'Welcome to Peter\'s Hats' '' '' 'We\'ve got the right hat for everyone.';element1;var template = ;template;var builder = template;
var btnUrl = 'Show Website' '';var btnPostback = 'Start Chatting' 'DEVELOPER_DEFINED_PAYLOAD';var template = 'What do you want to do next?' btnUrl btnPostback;var builder = template;
var element1 = 'Classic Black T-Shirt' '' '100% Cotton, 200% Comfortable';element1;var element2 = 'Classic Gray T-Shirt' '' '100% Cotton, 200% Comfortable';element2;var template = ;template;var builder = template;
var template = 'Stephane Crozatier' '12345678902' 'USD' 'Visa 2345';template;var builder = template;
Facebook's server sends user action (message, message delivered, message read and etc.) to your bot server. Request of that contains event(s); event is action of the user.
- MESSAGE Event name will occur when a message has been sent to your page. You may receive text messages or messages with attachments.
- MESSAGE_DELIVERED Event name will occur when a message a page has sent has been delivered.
- MESSAGE_READ Event name will occur when a message a page has sent has been read by the user.
- MESSAGE_ECHO Event name will occur when a message has been sent by your page. You may receive text messsages or messages with attachments.
- POSTBACK Event name will occur when a Postback button, Get Started button, Persistent menu or Structured Message is tapped.
- OPTIN Event name will occur when the Send-to-Messenger plugin has been tapped.
- REFERRAL Event name will occur when an m.me link is used with a referral param and only in a case this user already has a thread with this bot.
- PAYMENTS Event name will occurs when a persons taps the pay button from the checkout dialog rendered by the Buy Button.
- CHECKOUT_UPDATE Event name enables you to update pricing for flexible-amount transactions on the checkout dialog displayed by the Buy Button.
- ACCOUNT_LINKING Event name will occur when the Linked Account or Unlink Account call-to-action have been tapped.