facebook-bot-messenger
==
SDK of the Facebook Messenger Platform for Node.js
Installation
The Facebook Messenger Platform SDK can be installed with NPM.
$ npm install facebook-bot-messenger
Getting started
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;
Call API
You can call API through the bot client instance.
Get profile
Get detail information of user.
bot;
When MessengerPlatform#getProfile() success return JSON object.
Send action
Send readed message
bot;
Send typing on
bot;
Send typing off
bot;
Send message
sample is following
bot;
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
bot;
Send audio attachment message
bot;
Send video attachment message
bot;
Send file attachment message
bot;
Message builder
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
TextMessageBuilder
var builder = '<message>';
QuickRepliesMessageBuilder
var builder = 'Pick a color:';builder ;
AttachmentMessageBuilder
var builder = ;builder;
Template attachment message:
- Generic
var element = 'Welcome to Peter\'s Hats' 'https://petersfancybrownhats.com' 'https://petersfancybrownhats.com/company_image.png' 'We\'ve got the right hat for everyone.';element1;var template = ;template;var builder = template;
- Button
var btnUrl = 'Show Website' 'https://petersapparel.parseapp.com';var btnPostback = 'Start Chatting' 'DEVELOPER_DEFINED_PAYLOAD';var template = 'What do you want to do next?' btnUrl btnPostback;var builder = template;
- List
var element1 = 'Classic Black T-Shirt' 'https://peterssendreceiveapp.ngrok.io/img/black-t-shirt.png' '100% Cotton, 200% Comfortable';element1 ; var element2 = 'Classic Gray T-Shirt' 'https://peterssendreceiveapp.ngrok.io/img/gray-t-shirt.png' '100% Cotton, 200% Comfortable';element2 ;var template = ;template ;var builder = template;
- Receipt
var template = 'Stephane Crozatier' '12345678902' 'USD' 'Visa 2345';template ;var builder = template;
Webhook
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.
Webhook events:
- 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.