---------
[![Built with Grunt](https://cdn.gruntjs.com/builtwith.svg)](http://gruntjs.com/)
[![Build Status](https://travis-ci.org/Askrround/botly.svg)](https://travis-ci.org/Askrround/botly)
[![Test Coverage](https://codeclimate.com/github/Askrround/botly/badges/coverage.svg)](https://codeclimate.com/github/Askrround/botly/coverage)
[![npm version](https://badge.fury.io/js/botly.svg)](http://badge.fury.io/js/botly)
[![Dependency Status](https://david-dm.org/Askrround/botly.svg?theme=shields.io)](https://david-dm.org/Askrround/botly)
[![devDependency Status](https://david-dm.org/Askrround/botly/dev-status.svg?theme=shields.io)](https://david-dm.org/Askrround/botly#info=devDependencies)
[![npm downloads](https://img.shields.io/npm/dm/botly.svg)](https://img.shields.io/npm/dm/botly.svg)
[![license](https://img.shields.io/npm/l/botly.svg)](LICENSE)
[![NPM](https://nodei.co/npm/botly.png)](https://nodei.co/npm/botly/)
Simple Facebook Messenger Platform Bot API
- Install
- Example
- API
- send (options[, callback])
- sendText (options[, callback])
- sendAttachment (options[, callback])
- sendImage (options[, callback])
- sendButtons (options[, callback])
- sendGeneric (options[, callback])
- sendAction (options[, callback])
- sendReceipt (options[, callback])
- setGetStarted (options[, callback])
- setPersistentMenu (options[, callback])
- getUserProfile (userId[, callback])
- getPSID (accountLinkingToken[, callback])
- createWebURLButton (title, url)
- createAccountLinkButton (url)
- createPostbackButton (title, payload)
- createQuickReply (title, payload)
- createButtonTemplate (text, buttons)
- createGenericTemplate (elements)
- handleMessage (req)
- Events
- Change Log
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 (options[, callback])
botly.send({
id: userId,
message: {text: "Hi There!"}
}, function (err, data) {
});
sendText (options[, callback])
botly.sendText({id: userId, text: "Hi There!"}, function (err, data) {
});
sendAttachment (options[, callback])
botly.sendAttachment({
id: userId,
type: Botly.CONST.ATTACHMENT_TYPE.IMAGE,
payload: {url: "http://example.com/image.png"}
}, function (err, data) {
});
sendImage (options[, callback])
botly.sendImage({id: userId, url: "http://example.com/image.png"}, function (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}
, function (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}, function (err, data) {
console.log("send generic cb:", err, data);
});
sendAction (options[, callback])
botly.sendAction({id: userId, action: Botly.CONST.ACTION_TYPES.TYPING_ON}, function (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"}, function (err, body) {
});
botly.setPersistentMenu({pageId: "myPage", buttons: [botly.createPostbackButton('reset', 'reset_me')]}, function (err, body) {
});
getUserProfile (userId[, callback])
botly.getUserProfile(senduserIder, function (err, info) {
});
getPSID (accountLinkingToken[, callback])
Used to retrieve the user page-scoped ID (PSID) during the linking flow.
botly.getUserProfile(accountLinkingToken, function (err, info) {
});
createWebURLButton (title, url)
createAccountLinkButton (url)
createPostbackButton (title, payload)
createQuickReply (title, payload)
sendAttachment
and sendText
both support optional quick_replies
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.
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) => {
});
botly.on("sent", (to, message) => {
});
botly.on("account_link", (sender, message, link) => {
});
Change Log
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.