Simple Facebook Messenger Platform Bot API
- Install
- Example
- API
- send (options[, callback])
- upload (options[, callback])
- sendText (options[, callback])
- sendAttachment (options[, callback])
- sendImage (options[, callback])
- sendButtons (options[, callback])
- sendGeneric (options[, callback])
- sendList (options[, callback])
- sendOpenGraph (options[, callback])
- sendAction (options[, callback])
- sendReceipt (options[, callback])
- setGetStarted (options[, callback])
- setGreetingText (options[, callback])
- setTargetAudience (options[, callback])
- setWhitelist (options[, callback])
- setPersistentMenu (options[, callback])
- removePersistentMenu (options[, callback])]
- getUserProfile (options[, callback])
- getPSID (accountLinkingToken[, callback])
- createWebURLButton (title, url[, heightRatio][, supportExtension][, fallbackURL][, disableShare])
- createAccountLinkButton (url)
- createPostbackButton (title, payload)
- createShareButton ()
- createQuickReply (title, payload[, imageURL])
- createShareLocation ()
- createListElement (options)
- createOpenGraphElement (options)
- createButtonTemplate (text, buttons)
- createGenericTemplate (elements[, aspectRatio])
- createListTemplate (options)
- [createOpenGraphTemplate (elements])](#createopengraphtemplate-elements-aspectratio)
- handleMessage (req)
- Events
- Change Log
Install
npm i botly --save
Example
const express = require("express");
const bodyParser = require("body-parser");
const Botly = require("botly");
const botly = new Botly({
accessToken: pageAccessToken,
verifyToken: verificationToken,
webHookPath: yourWebHookPath,
notificationType: Botly.CONST.REGULAR,
FB_URL: 'https://graph.facebook.com/v2.6/'
});
botly.on("message", (senderId, message, data) => {
let text = `echo: ${data.text}`;
botly.sendText({
id: senderId,
text: text
});
});
const app = express();
app.use(bodyParser.json({
verify: botly.getVerifySignature(process.env.APP_SECRET)
}));
app.use(bodyParser.urlencoded({ extended: false }));
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 (options[, callback])
botly.send({
id: userId,
message: {text: "Hi There!"}
}, function (err, data) {
});
upload (options[, callback])
botly.upload({
type: Botly.CONST.ATTACHMENT_TYPE.IMAGE,
payload: {url: "http://example.com/image.png"}
}, (err, data) => {
});
sendText (options[, callback])
botly.sendText({id: userId, text: "Hi There!"}, function (err, data) {
});
sendAttachment (options[, callback])
Also supports options.filedata = '@/tmp/receipt.pdf'
.
botly.sendAttachment({
id: userId,
type: Botly.CONST.ATTACHMENT_TYPE.IMAGE,
payload: {url: "http://example.com/image.png"}
}, (err, data) => {
});
sendImage (options[, callback])
botly.sendImage({id: userId, url: "http://example.com/image.png"}, (err, data) => {
});
sendButtons (options[, callback])
let buttons = [];
buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com"));
buttons.push(botly.createPostbackButton("Continue", "continue"));
botly.sendButtons({id: userId, text: "What do you want to do next?", buttons: buttons}
, (err, data) => {
});
sendGeneric (options[, callback])
let buttons = [];
buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com"));
buttons.push(botly.createPostbackButton("Continue", "continue"));
let element = {
title: "What do you want to do next?",
item_url: "http://example.com",
image_url: "http://example.com/image.png",
subtitle: "Choose now!",
buttons: buttons
}
botly.sendGeneric({id: userId, elements: element, aspectRatio: Botly.CONST.IMAGE_ASPECT_RATIO.HORIZONTAL}, (err, data) => {
console.log("send generic cb:", err, data);
});
sendList (options[, callback])
const element = botly.createListElement({
title: 'First Element',
image_url: 'https://peterssendreceiveapp.ngrok.io/img/collection.png',
subtitle: 'subtitle text',
buttons: [
{title: 'Payload Button', payload: 'first_element'},
],
default_action: {
'url': 'https://peterssendreceiveapp.ngrok.io/shop_collection',
}
});
const element2 = botly.createListElement({
title: 'Other Element',
image_url: 'https://peterssendreceiveapp.ngrok.io/img/collection.png',
subtitle: 'even more subtitle',
buttons: [
{title: "Go to Askrround", url: "http://askrround.com"},
],
default_action: {
'url': 'https://peterssendreceiveapp.ngrok.io/shop_collection',
}
});
botly.sendList({id: sender, elements: [element, element2], buttons: botly.createPostbackButton('More Plans', 'MORE_PLANS'), top_element_style: Botly.CONST.TOP_ELEMENT_STYLE.LARGE},function (err, data) {
console.log('send list cb:', err, data);
});
sendAction (options[, callback])
botly.sendAction({id: userId, action: Botly.CONST.ACTION_TYPES.TYPING_ON}, (err, data) => {
});
sendReceipt (options[, 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({id: sender, payload: payload}, function (err, data) {
console.log("send generic cb:", err, data);
});
setGetStarted (options[, callback])
botly.setGetStarted({pageId: "myPage", payload: "GET_STARTED_CLICKED"}, (err, body) => {
});
setGreetingText (options[, callback])
botly.setGreetingText({
pageId: "myPage",
greeting: [{
"locale":"default",
"text":"Hello!"
}, {
"locale":"en_US",
"text":"Timeless apparel for the masses."
}]}, (err, body) => {
});
setTargetAudience (options[, callback])
botly.setTargetAudience({
pageId: "myPage",
audience: {
"audience_type":"custom",
"countries":{
"whitelist":["US", "CA"]
}
}}, (err, body) => {
});
setWhitelist (options[, callback])
botly.setWhitelist({whiteList: ["https://askhaley.com"]}, (err, body) => {
});
botly.setPersistentMenu({
pageId: "myPage",
menu: [
{
"locale":"default",
"composer_input_disabled":true,
"call_to_actions":[
{
"title":"My Account",
"type":"nested",
"call_to_actions":[
{
"title":"Pay Bill",
"type":"postback",
"payload":"PAYBILL_PAYLOAD"
},
{
"title":"History",
"type":"postback",
"payload":"HISTORY_PAYLOAD"
},
{
"title":"Contact Info",
"type":"postback",
"payload":"CONTACT_INFO_PAYLOAD"
}
]
},
{
"type":"web_url",
"title":"Latest News",
"url":"http://petershats.parseapp.com/hat-news",
"webview_height_ratio":"full"
}
]
},
{
"locale":"zh_CN",
"composer_input_disabled":false
}
]}, (err, body) => {
});
botly.removePersistentMenu(
{
pageId: "myPage",
},
(err, body) => {
});
getUserProfile (options[, callback])
Used to retrieve basic profile details by user page-scoped ID (PSID). You can pass the userID
directly, in which case a default set of fields (first_name
, last_name
, profile_pic
) are requested.
Also supports passing an object as
const options = {
id: userId,
fields: [
Botly.CONST.USER_PROFILE_FIELD.FIRST_NAME,
Botly.CONST.USER_PROFILE_FIELD.LAST_NAME
],
accessToken: OTHER_TOKEN
}
botly.getUserProfile(options, function (err, info) {
});
or
botly.getUserProfile(userId, function (err, info) {
});
getPSID (accountLinkingToken[, callback])
Used to retrieve the user page-scoped ID (PSID) during the linking flow.
Also supports passing an object as {token: accountLinkingToken, accessToken: OTHER_TOKEN}
botly.getUserProfile(accountLinkingToken, function (err, info) {
});
createWebURLButton (title, url[, heightRatio][, supportExtension][, fallbackURL][, disableShare])
createAccountLinkButton (url)
createPostbackButton (title, payload)
createShareButton ()
createQuickReply (title, payload[, imageURL])
sendAttachment
and sendText
both support optional quick_replies
createShareLocation ()
share location quick reply
createListElement (options)
Will create a list element. default_action
will be added web_url
type, and will create button according to properties (url
means web_url
and payload
means postback
)
createButtonTemplate (text, buttons)
Where buttons
can be a single button or an array of buttons.
createGenericTemplate (elements[, aspectRatio])
Where elements
can be a single element or an array of elements.
and aspectRatio
defaults to horizontal
createListTemplate (options)
Where options
has bottons
and elements
- an array will be created automatically if a single item was passed.
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, ref) => {
});
botly.on("delivery", (sender, message, mids) => {
});
botly.on("optin", (sender, message, optin) => {
});
botly.on("error", (ex) => {
});
botly.on("sent", (to, message) => {
});
botly.on("echo", (sender, message, content, recipient) => {
});
botly.on("account_link", (sender, message, link) => {
});
botly.on("referral", (sender, message, ref) => {
});
Change Log
version 1.5.0
- added required
messaging_type
parameter when sending message - added the ability to override the
FB_URL
for testing purposes - added
getVerifySignature(APP_SECRET)
function to allow signature verification - provide the result to bodyParser.json({verify})
version 1.4.0
- support version 1.4 of messenger api
- new
setPersistentMenu
API aligned with v1.4 - added
setGreetingText
, setAccountLinkingURL
, setTargetAudience
API - aligned all thread settings to the new profile API
- added support for filedata upload in the
sendAttachment
- added support for the new upload attachment API,
- support for new image_aspect_ratio in generic template
version 1.3.0
- support version 1.3 of messenger including the new list template
- support for referral params on m.me links
version 1.2.0
- added support for webview height in web url button
- added support setWhitelist for webview items
- added createShare button
- added support for location share quick reply
- added imageURL to quick reply
version 1.1.6
- Send 403 status code when verify token is invalid
version 1.1.5
- fixed duplicate messages on echo
- added echo event support
version 1.1.4
- added support for account linking functionality (event, getPSID)
- added ability to override accessToken on all APIs for multiple pages support
version 1.1.0
- added support for sender actions using
sendAction
(mark seen/ typing on/ typing off)
version 1.0.3
- added send event - useful for tracking
version 1.0.1
- quick replies are considered as postback and not regular message
version 1.0.0
- removed
createTemplate
function - was too verbose - moved to object parameters - too many parameters
- added support for quick replies
- add support for persistent menu
- added support for audio/video/file attachments
- renamed setWelcomeScreen to setGetStarted since no longer supported by facebook.