Comparing version 1.2.0 to 1.3.0
@@ -53,2 +53,29 @@ #!/usr/bin/env node | ||
} | ||
else if (data && data.text && data.text.indexOf("list") !== -1) { | ||
let element = botly.createListElement({ | ||
title: "Classic T-Shirt Collection", | ||
image_url: "https://peterssendreceiveapp.ngrok.io/img/collection.png", | ||
subtitle: "See all our colors", | ||
buttons: [ | ||
{title: "DO WORK", payload: "DO_WORK"}, | ||
], | ||
default_action: { | ||
"url": "https://peterssendreceiveapp.ngrok.io/shop_collection", | ||
} | ||
}); | ||
let element2 = botly.createListElement({ | ||
title: "Number 2", | ||
image_url: "https://peterssendreceiveapp.ngrok.io/img/collection.png", | ||
subtitle: "See all our colors", | ||
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("Continue", "continue"), top_element_style: Botly.CONST.TOP_ELEMENT_STYLE.LARGE},function (err, data) { | ||
console.log("send list cb:", err, data); | ||
}); | ||
} | ||
else if (data && data.text && data.text.indexOf("quick") !== -1) { | ||
@@ -116,3 +143,3 @@ botly.sendText({id: sender, text:"some question?", quick_replies: [botly.createQuickReply('option1', 'option_1')]}, function (err, data) { | ||
botly.send({id: sender, message: { | ||
text: `Sorry ${users[sender].last_name}, you are annoying ` | ||
text: `${users[sender].last_name}, try sending 'list'/'generic'/'receipt'/'quick'/'image'/'buttons' to try out the different types of messages` | ||
}}, function (err, data) { | ||
@@ -127,3 +154,3 @@ console.log("regular send cb:", err, data); | ||
botly.sendText({id: sender, text: text + users[sender].first_name}, function (err, data) { | ||
botly.sendText({id: sender, text: `${text} ${users[sender].first_name}`}, function (err, data) { | ||
console.log("send text cb:", err, data); | ||
@@ -130,0 +157,0 @@ }); |
105
lib/Botly.js
@@ -14,3 +14,4 @@ 'use strict'; | ||
ACCOUNT_LINK: 'account_link', | ||
SHARE: 'element_share' | ||
SHARE: 'element_share', | ||
CALL: 'phone_number' | ||
}; | ||
@@ -21,3 +22,4 @@ | ||
RECEIPT: 'receipt', | ||
GENERIC: 'generic' | ||
GENERIC: 'generic', | ||
LIST: 'list' | ||
}; | ||
@@ -56,2 +58,7 @@ | ||
const TOP_ELEMENT_STYLE = { | ||
LARGE: 'large', | ||
COMPACT: 'compact' | ||
}; | ||
function Botly(options) { | ||
@@ -173,2 +180,26 @@ if (!(this instanceof Botly)) { | ||
Botly.prototype.setGreetingText = function (options, callback) { | ||
const PAGE_URL = `${FB_URL}${options.pageId}/thread_settings`; | ||
request.post( | ||
{ | ||
url: PAGE_URL, | ||
json: true, | ||
qs: { | ||
access_token: this.accessToken || options.accessToken | ||
}, | ||
body: { | ||
setting_type: 'greeting', | ||
"greeting":{ | ||
"text": options.text | ||
} | ||
} | ||
}, (err, res, body) => { | ||
if (callback) { | ||
callback(err, body); | ||
} | ||
}); | ||
}; | ||
Botly.prototype.setPersistentMenu = function (options, callback) { | ||
@@ -309,2 +340,8 @@ const PAGE_URL = `${FB_URL}${options.pageId}/thread_settings`; | ||
Botly.prototype.sendList = function (options, callback) { | ||
options.payload = this.createListTemplate(options); | ||
options.type = ATTACHMENT_TYPE.TEMPLATE; | ||
this.sendAttachment(options, callback); | ||
}; | ||
Botly.prototype.sendReceipt = function (options, callback) { | ||
@@ -317,2 +354,9 @@ options.payload.template_type = TEMPLATE_TYPE.RECEIPT; | ||
Botly.prototype.createWebURLButton = function (title, url, heightRatio, supportExtension, fallbackURL) { | ||
if (typeof title === 'object') { | ||
fallbackURL = title.fallbackURL; | ||
supportExtension = title.supportExtension; | ||
heightRatio = title.heightRatio; | ||
url = title.url; | ||
title = title.title; | ||
} | ||
let button = { | ||
@@ -385,2 +429,36 @@ type: BUTTON_TYPE.WEB_URL, | ||
Botly.prototype.createListElement = function (options) { | ||
let element = { | ||
title: options.title | ||
}; | ||
let buttons = options.buttons; | ||
if (buttons) { | ||
if(!Array.isArray(buttons)) { | ||
buttons = [buttons]; | ||
} | ||
buttons = buttons.map(button => { | ||
if (button.url) { | ||
return this.createWebURLButton(button); | ||
} else if (button.payload) { | ||
return this.createPostbackButton(button.title, button.payload); | ||
} else { | ||
return button; | ||
} | ||
}); | ||
element.buttons = buttons; | ||
} | ||
if (options.subtitle) { | ||
element.subtitle = options.subtitle; | ||
} | ||
if (options.image_url) { | ||
element.image_url = options.image_url; | ||
} | ||
if (options.default_action) { | ||
options.default_action.type = BUTTON_TYPE.WEB_URL; | ||
element.default_action = options.default_action; | ||
} | ||
return element; | ||
}; | ||
Botly.prototype.createGenericTemplate = function (elements) { | ||
@@ -396,2 +474,22 @@ if (!Array.isArray(elements)) { | ||
Botly.prototype.createListTemplate = function (options) { | ||
let elements = options.elements; | ||
let buttons = options.buttons; | ||
if (!Array.isArray(elements)) { | ||
elements = [elements]; | ||
} | ||
let template = { | ||
top_element_style: TOP_ELEMENT_STYLE.LARGE || options.top_element_style, | ||
template_type: TEMPLATE_TYPE.LIST, | ||
elements: elements | ||
}; | ||
if (buttons) { | ||
if(!Array.isArray(buttons)) { | ||
buttons = [buttons]; | ||
} | ||
template.buttons = buttons; | ||
} | ||
return template; | ||
}; | ||
Botly.prototype.handleMessage = function (req) { | ||
@@ -480,5 +578,6 @@ const body = _clone(req.body); | ||
CONTENT_TYPE: CONTENT_TYPE, | ||
WEBVIEW_HEIGHT_RATIO: WEBVIEW_HEIGHT_RATIO | ||
WEBVIEW_HEIGHT_RATIO: WEBVIEW_HEIGHT_RATIO, | ||
TOP_ELEMENT_STYLE: TOP_ELEMENT_STYLE | ||
}; | ||
module.exports = Botly; |
{ | ||
"name": "botly", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Simple Facebook Messenger Bot API", | ||
@@ -48,6 +48,6 @@ "main": "index.js", | ||
"grunt-istanbul": "^0.7.0", | ||
"grunt-mocha-test": "^0.12.7", | ||
"grunt-mocha-test": "^0.13.2", | ||
"load-grunt-config": "^0.19.2", | ||
"mocha": "^2.4.5", | ||
"mockery": "^1.7.0", | ||
"mocha": "^3.1.2", | ||
"mockery": "^2.0.0", | ||
"node-mocks-http": "^1.5.2", | ||
@@ -54,0 +54,0 @@ "sinon": "^1.17.4", |
@@ -11,3 +11,3 @@ <img src="https://raw.githubusercontent.com/Askrround/botly/master/botly_logo.png" width="250" height="250" /> | ||
[![license](https://img.shields.io/npm/l/botly.svg)](LICENSE) | ||
[![NPM](https://nodei.co/npm/botly.png)](https://nodei.co/npm/botly/) | ||
[![NPM](https://nodei.co/npm/botly.png?downloads=true&downloadRank=true)](https://nodei.co/npm/botly/) | ||
@@ -25,2 +25,3 @@ > Simple Facebook Messenger Platform Bot API | ||
- [sendGeneric (options[, callback])](#sendgeneric-options-callback) | ||
- [sendList (options[, callback])](#sendlist-options-callback) | ||
- [sendAction (options[, callback])](#sendaction-options-callback) | ||
@@ -36,7 +37,9 @@ - [sendReceipt (options[, callback])](#sendreceipt-options-callback) | ||
- [createPostbackButton (title, payload)](#createpostbackbutton-title-payload) | ||
- [createShareButton ()](#createsharebutton) | ||
- [createShareButton ()](#createsharebutton-) | ||
- [createQuickReply (title, payload[, imageURL])](#createquickreply-title-payload-imageurl) | ||
- [createShareLocation ()](#createsharelocation) | ||
- [createShareLocation ()](#createsharelocation-) | ||
- [createListElement (options)](#createlistelement-options) | ||
- [createButtonTemplate (text, buttons)](#createbuttontemplate-text-buttons) | ||
- [createGenericTemplate (elements)](#creategenerictemplate-elements) | ||
- [createListTemplate (options)](#createlisttemplate-options) | ||
- [handleMessage (req)](#handlemessage-req) | ||
@@ -141,2 +144,20 @@ - [Events](#events) | ||
#### sendList (options[, callback]) | ||
```javascript | ||
let buttons = []; | ||
buttons.push(botly.createPostbackButton("Continue", "continue")); | ||
let element = botly.createListElement({ | ||
title: "Classic T-Shirt Collection", | ||
image_url: "https://peterssendreceiveapp.ngrok.io/img/collection.png", | ||
subtitle: "See all our colors", | ||
buttons: {title: "Go to Askrround", url: "http://askrround.com"}, | ||
default_action: { | ||
"url": "https://peterssendreceiveapp.ngrok.io/shop_collection", | ||
} | ||
}); | ||
botly.sendList({id: userId, elements: element, buttons: buttons}, function (err, data) { | ||
console.log("send generic cb:", err, data); | ||
}); | ||
``` | ||
#### sendAction (options[, callback]) | ||
@@ -260,2 +281,5 @@ ```javascript | ||
#### 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) | ||
@@ -267,2 +291,5 @@ Where `buttons` can be a single button or an array of buttons. | ||
#### createListTemplate (options) | ||
Where `options` has `bottons` and `elements` - an array will be created automatically if a single item was passed. | ||
#### handleMessage (req) | ||
@@ -331,2 +358,5 @@ If you are not using express, you can use this function to parse the request from facebook in order to generate the proper events. | ||
### version 1.3.0 | ||
- support version 1.3 of messenger including the new list template | ||
### version 1.2.0 | ||
@@ -333,0 +363,0 @@ - added support for webview height in web url button |
@@ -742,2 +742,77 @@ var expect = require('chai').expect; | ||
it('should send list template', () => { | ||
var botly = new Botly({ | ||
accessToken: 'myToken', | ||
verifyToken: 'myVerifyToken', | ||
webHookPath: '/webhook', | ||
notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH | ||
}); | ||
var element = botly.createListElement({ | ||
title: "Classic T-Shirt Collection", | ||
image_url: "https://peterssendreceiveapp.ngrok.io/img/collection.png", | ||
subtitle: "See all our colors", | ||
buttons: [ //Just to test coverage - you should not send more than one button in a list element | ||
{title: "Go to Askrround", url: "http://askrround.com"}, | ||
{title: "DO WORK", payload: "DO_WORK"}, | ||
{type: "element_share"} | ||
], | ||
default_action: { | ||
"url": "https://peterssendreceiveapp.ngrok.io/shop_collection", | ||
} | ||
}); | ||
botly.sendList({id: USER_ID, elements: element, buttons: botly.createPostbackButton("Continue", "continue"), top_element_style: Botly.CONST.TOP_ELEMENT_STYLE.LARGE}); | ||
expect(request.post.calledOnce).to.be.true; | ||
expect(request.post.args[0][0].body).to.eql({ | ||
'message': { | ||
'attachment': { | ||
'payload': { | ||
'elements': [ | ||
{ | ||
'buttons': [ | ||
{ | ||
'title': 'Go to Askrround', | ||
'type': 'web_url', | ||
'url': 'http://askrround.com' | ||
}, | ||
{ | ||
'type': 'postback', | ||
'title': 'DO WORK', | ||
'payload': 'DO_WORK' | ||
}, | ||
{ | ||
'type': 'element_share' | ||
} | ||
], | ||
'default_action': { | ||
'type': 'web_url', | ||
'url': 'https://peterssendreceiveapp.ngrok.io/shop_collection' | ||
}, | ||
'image_url': 'https://peterssendreceiveapp.ngrok.io/img/collection.png', | ||
'subtitle': 'See all our colors', | ||
'title': 'Classic T-Shirt Collection' | ||
} | ||
], | ||
'buttons': [ | ||
{ | ||
'type': 'postback', | ||
'title': 'Continue', | ||
'payload': 'continue' | ||
} | ||
], | ||
"top_element_style": "large", | ||
'template_type': 'list' | ||
}, | ||
'type': 'template' | ||
} | ||
}, | ||
'notification_type': 'NO_PUSH', | ||
'recipient': { | ||
'id': '333' | ||
} | ||
}); | ||
}); | ||
it('should send webview buttons', () => { | ||
@@ -796,3 +871,62 @@ var botly = new Botly({ | ||
it('should send webview buttons when passing object', () => { | ||
var botly = new Botly({ | ||
accessToken: 'myToken', | ||
verifyToken: 'myVerifyToken', | ||
webHookPath: '/webhook', | ||
notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH | ||
}); | ||
var element = { | ||
title: 'What do you want to do next?', | ||
item_url: 'https://upload.wikimedia.org/wikipedia/en/9/93/Tanooki_Mario.jpg', | ||
image_url: 'https://upload.wikimedia.org/wikipedia/en/9/93/Tanooki_Mario.jpg', | ||
subtitle: 'Choose now!', | ||
buttons: [botly.createWebURLButton({ | ||
title: 'Go to Askrround', | ||
url: 'http://askrround.com', | ||
heightRatio: Botly.CONST.WEBVIEW_HEIGHT_RATIO.COMPACT, | ||
supportExtension: true, | ||
fallbackURL: 'http://askrround.com' | ||
})] | ||
}; | ||
botly.sendGeneric({id: USER_ID, elements: element}); | ||
expect(request.post.calledOnce).to.be.true; | ||
expect(request.post.args[0][0].body).to.eql({ | ||
'message': { | ||
'attachment': { | ||
'payload': { | ||
'elements': [ | ||
{ | ||
'buttons': [ | ||
{ | ||
'title': 'Go to Askrround', | ||
'type': 'web_url', | ||
'url': 'http://askrround.com', | ||
'webview_height_ratio': 'compact', | ||
'messenger_extensions': true, | ||
'fallback_url': 'http://askrround.com' | ||
} | ||
], | ||
'image_url': 'https://upload.wikimedia.org/wikipedia/en/9/93/Tanooki_Mario.jpg', | ||
'item_url': 'https://upload.wikimedia.org/wikipedia/en/9/93/Tanooki_Mario.jpg', | ||
'subtitle': 'Choose now!', | ||
'title': 'What do you want to do next?' | ||
} | ||
], | ||
'template_type': 'generic' | ||
}, | ||
'type': 'template' | ||
} | ||
}, | ||
'notification_type': 'NO_PUSH', | ||
'recipient': { | ||
'id': '333' | ||
} | ||
}); | ||
}); | ||
it('should send receipt messages', () => { | ||
@@ -799,0 +933,0 @@ var botly = new Botly({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109768
1871
389