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
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);
});
// Setup listener for postback
bot.on('postback', function(userId, payload){
// Send message with slient push
bot.sendTextMessage(userId, "Echo postback:" + payload, FBBotFramework.NOTIFICATION_TYPE.SILENT_PUSH);
});
// 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.
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 oferr
andresult
.
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 oferr
andresult
.
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 oferr
andresult
.
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 oferr
andresult
.
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 oferr
andresult
.
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 oferr
andprofile
.
bot.getUserProfile(userId, function (err, profile) {
console.log(profile);
});