fb-bot-framework is a node framework for Facebook Messenger Platform(FB Messenger Bot).
npm install fb-bot-framework --save- work as Express Middleware
- implement of Send API.
- customize notification type(REGULAR, SILENT_PUSH, NO_PUSH) for each individual message
- implement of Thread Settings API (Greeting Text / Get Started Button / Persistent Menu)
You can use bot.middleware() to hook any request to the path /webhook to the bot.
var express = require('express');
var app = express();
var FBBotFramework = require('fb-bot-framework');
// Initialize
var bot = new FBBotFramework({
page_token: "THIS_IS_PAGE_TOKEN",
verify_token: "THIS_IS_VERIFY_TOKEN"
});
// Setup Express middleware for /webhook
app.use('/webhook', bot.middleware());
// Setup listener for incoming messages
bot.on('message', function(userId, message){
bot.sendTextMessage(userId, "Echo Message:" + message);
});
// Config the Get Started Button and register a callback
bot.setGetStartedButton("GET_STARTED");
bot.on('postback', function(userId, payload){
if (payload == "GET_STARTED") {
getStarted(userId);
}
// Other postback callbacks here
// ...
});
function getStarted(userId){
// Get started process
}
// Setup listener for attachment
bot.on('attachment', function(userId, attachment){
// Echo the audio attachment
if (attachment[0].type == "audio") {
bot.sendAudioAttachment(userId, attachment[0].payload.url);
}
});
// Make Express listening
app.listen(3000);Triggered when an user sends a typed message to the bot.
userId- String: The Facebook Id of the sender.message- String: The actual message sent by the user.
Triggered when an user clicks a button which will send a postback message.
userId- String: The Facebook Id of the sender.payload- String: The payload associated with the button clicked by the user.
Set the greeting message for new conversations.
text- The greeting textcb- Optional, callback with arguments oferrandresult.
Set the Get Started button postback callback in the Welcome Screen.
payload- The postback callback to be calledcb- Optional, callback with arguments oferrandresult.
Set the Persistent Menu which is always available to the user.
menuButtons- The menu button for the Persistent Menucb- Optional, callback with arguments oferrandresult.
var menuButtons = [
{
"type": "postback",
"title": "Help",
"payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_HELP"
},
{
"type": "postback",
"title": "Start a New Order",
"payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_START_ORDER"
},
{
"type": "web_url",
"title": "View Website",
"url": "http://petersapparel.parseapp.com/"
}
];
bot.setPersistentMenu(menuButtons);Send a text message to a specific user.
userId- The recipient's Facebook Idtext- The actual textnotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
Send an image message to a specific user.
userId- The recipient's Facebook IdimageUrl- The url of the imagenotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
Send an image message to a specific user.
userId- The recipient's Facebook IdaudioUrl- The url of the audio filenotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
Send an image message to a specific user.
userId- The recipient's Facebook IdvideoUrl- The url of the video filenotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
Send an image message to a specific user.
userId- The recipient's Facebook IdfileUrl- The url of the filenotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
Send a message with buttons to a specific user.
userId- The recipient's Facebook Idtext- The title of the messagebuttons- The buttons of the messagenotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
var text = "What do you want to do next?";
var buttons = [
{
"type": "web_url",
"url": "https://petersapparel.parseapp.com",
"title": "Show Website"
},
{
"type": "postback",
"title": "Start Chatting",
"payload": "USER_DEFINED_PAYLOAD"
}
];
bot.sendButtonMessage(recipient, text, buttons);Send a message with bubbles(horizontal scroll cards) to a specific user.
userId- The recipient's Facebook Idelements- The elements of the messagenotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
var elements = [
{
"title": "Classic White T-Shirt",
"image_url": "http://petersapparel.parseapp.com/img/item100-thumb.png",
"subtitle": "Soft white cotton t-shirt is back in style",
"buttons": [
{
"type": "web_url",
"url": "https://petersapparel.parseapp.com/view_item?item_id=100",
"title": "View Item"
},
{
"type": "web_url",
"url": "https://petersapparel.parseapp.com/buy_item?item_id=100",
"title": "Buy Item"
},
{
"type": "postback",
"title": "Bookmark Item",
"payload": "USER_DEFINED_PAYLOAD_FOR_ITEM100"
}
]
},
{
"title": "Classic Grey T-Shirt",
"image_url": "http://petersapparel.parseapp.com/img/item101-thumb.png",
"subtitle": "Soft gray cotton t-shirt is back in style",
"buttons": [
{
"type": "web_url",
"url": "https://petersapparel.parseapp.com/view_item?item_id=101",
"title": "View Item"
},
{
"type": "web_url",
"url": "https://petersapparel.parseapp.com/buy_item?item_id=101",
"title": "Buy Item"
},
{
"type": "postback",
"title": "Bookmark Item",
"payload": "USER_DEFINED_PAYLOAD_FOR_ITEM101"
}
]
}
];
bot.sendGenericMessage(recipient, elements, buttons);It is the same as sendGenericMessage()
Send a message with receipt to a specific user.
userId- The recipient's Facebook Idelements- The elements of the messagenotificationType- Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSHcb- Optional, callback with arguments oferrandresult.
var receipt = {
"recipient_name": "Stephane Crozatier",
"order_number": "12345678902",
"currency": "USD",
"payment_method": "Visa 2345",
"order_url": "http://petersapparel.parseapp.com/order?order_id=123456",
"timestamp": "1428444852",
"elements": [
{
"title": "Classic White T-Shirt",
"subtitle": "100% Soft and Luxurious Cotton",
"quantity": 2,
"price": 50,
"currency": "USD",
"image_url": "http://petersapparel.parseapp.com/img/whiteshirt.png"
},
{
"title": "Classic Gray T-Shirt",
"subtitle": "100% Soft and Luxurious Cotton",
"quantity": 1,
"price": 25,
"currency": "USD",
"image_url": "http://petersapparel.parseapp.com/img/grayshirt.png"
}
],
"address": {
"street_1": "1 Hacker Way",
"street_2": "",
"city": "Menlo Park",
"postal_code": "94025",
"state": "CA",
"country": "US"
},
"summary": {
"subtotal": 75.00,
"shipping_cost": 4.95,
"total_tax": 6.19,
"total_cost": 56.14
},
"adjustments": [
{
"name": "New Customer Discount",
"amount": 20
},
{
"name": "$10 Off Coupon",
"amount": 10
}
]
};
bot.sendReceiptMessage(recipient, receipt);Get the Facebook profile from a specific user.
userId- The target's Facebook Idcb- callback with arguments oferrandprofile.
bot.getUserProfile(userId, function (err, profile) {
console.log(profile);
});