botly
Simple Facebook Messenger Platform Bot API
- Install
- Example
- API
- send (recipientId, message[, notificationType][, callback])
- sendText (recipientId, text[, notificationType][, callback])
- sendAttachment (recipientId, type, payload[, notificationType][, callback])
- sendImage (recipientId, imageURL[, notificationType][, callback])
- sendButtons (recipientId, text, buttons[, notificationType][, callback])
- sendGeneric (recipientId, elements[, notificationType][, callback])
- sendReceipt (recipientId, payload[, notificationType][, callback])
- setWelcomeScreen (pageId, message[, callback])
- getUserProfile (userId[, callback])
- createWebURLButton (title, url)
- createPostbackButton (title, payload)
- createButtonTemplate (text, buttons)
- createGenericTemplate (elements)
- createElement (title, itemURL, imageURL, subtitle, buttons)
- handleMessage (req)
- Events
Install
npm i botly --save
Example
const express = require("express");
const Botly = require("botly");
const botly = new Botly({
accessToken: pageAccessToken,
verifyToken: verificationToken,
webHookPath: yourWebHookPath,
notificationType: Botly.CONST.REGULAR
});
botly.on("message", (senderId, message, data) => {
let text = `echo: ${data.text}`;
botly.sendText(senderId, text);
});
const app = express();
app.use("/webhook", botly.router());
app.listen(3000);
You can also clone the repository and run a complete bot example from the example
folder.
API
send (recipientId, message[, notificationType][, callback])
botly.send(userId, {
text: "Hi There!"
}, function (err, data) {
});
sendText (recipientId, text[, notificationType][, callback])
botly.sendText(userId, "Hi There!", function (err, data) {
});
sendAttachment (recipientId, type, payload[, notificationType][, callback])
botly.sendAttachment(userId, Botly.CONST.ATTACHMENT_TYPE.IMAGE,
{
url: "http://example.com/image.png"
}, function (err, data) {
});
sendImage (recipientId, imageURL[, notificationType][, callback])
botly.sendImage(userId, "http://example.com/image.png", function (err, data) {
});
sendButtons (recipientId, text, buttons[, notificationType][, callback])
let buttons = [];
buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com"));
buttons.push(botly.createPostbackButton("Continue", "continue"));
botly.sendButtons(userId, "What do you want to do next?", buttons
, function (err, data) {
});
sendGeneric (recipientId, elements[, notificationType][, callback])
let buttons = [];
buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com"));
buttons.push(botly.createPostbackButton("Continue", "continue"));
let element = botly.createElement("What do you want to do next?",
"http://example.com",
"http://example.com/image.png",
"Choose now!", buttons);
botly.sendGeneric(sender, element, function (err, data) {
console.log("send generic cb:", err, data);
});
sendReceipt (recipientId, payload[, notificationType][, callback])
let payload = {
"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
}
]
};
botly.sendReceipt(sender, payload, function (err, data) {
console.log("send generic cb:", err, data);
});
setWelcomeScreen (pageId, message[, callback])
botly.setWelcomeScreen("myPage", {
text: "Welcome to my page!"
}, function (err, body) {
});
getUserProfile (userId[, callback])
botly.getUserProfile(senduserIder, function (err, info) {
});
createWebURLButton (title, url)
createPostbackButton (title, payload)
createButtonTemplate (text, buttons)
Where buttons
can be a single button or an array of buttons.
createGenericTemplate (elements)
Where elements
can be a single element or an array of elements.
createElement (title, itemURL, imageURL, subtitle, buttons)
Where buttons
can be a single button or an array of buttons.
handleMessage (req)
If you are not using express, you can use this function to parse the request from facebook in order to generate the proper events.
req
should have a body property.
Events
botly.on("message", (sender, message, data) => {
});
botly.on("postback", (sender, message, postback) => {
});
botly.on("delivery", (sender, message, mids) => {
});
botly.on("optin", (sender, message, optin) => {
});
botly.on("error", (ex) => {
});