From 38f294a000a8bef3f5c713094713a4b1f290025f Mon Sep 17 00:00:00 2001 From: miki2826 Date: Fri, 13 May 2016 18:08:08 +0300 Subject: [PATCH] - added notification type support per send - increased coverage to 100 --- README.md | 14 +++++------ lib/Botly.js | 33 +++++++++++++++----------- test/botly_test.js | 59 +++++++++++++++++++++++++++++----------------- 3 files changed, 63 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 21138ff..49c2303 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ You can also clone the repository and run a complete bot example from the `examp ### API -#### send (recipientId, message[, callback]) +#### send (recipientId, message[, notificationType][, callback]) ```javascript botly.send(userId, { text: "Hi There!" @@ -69,14 +69,14 @@ botly.send(userId, { }); ``` -#### sendText (recipientId, text[, callback]) +#### sendText (recipientId, text[, notificationType][, callback]) ```javascript botly.sendText(userId, "Hi There!", function (err, data) { //log it }); ``` -#### sendAttachment (recipientId, type, payload[, callback]) +#### sendAttachment (recipientId, type, payload[, notificationType][, callback]) ```javascript botly.sendAttachment(userId, Botly.CONST.ATTACHMENT_TYPE.IMAGE, { @@ -86,14 +86,14 @@ botly.sendAttachment(userId, Botly.CONST.ATTACHMENT_TYPE.IMAGE, }); ``` -#### sendImage (recipientId, imageURL[, callback]) +#### sendImage (recipientId, imageURL[, notificationType][, callback]) ```javascript botly.sendImage(userId, "http://example.com/image.png", function (err, data) { //log it }); ``` -#### sendButtons (recipientId, text, buttons[, callback]) +#### sendButtons (recipientId, text, buttons[, notificationType][, callback]) ```javascript let buttons = []; buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com")); @@ -104,7 +104,7 @@ botly.sendButtons(userId, "What do you want to do next?", buttons }); ``` -#### sendGeneric (recipientId, elements[, callback]) +#### sendGeneric (recipientId, elements[, notificationType][, callback]) ```javascript let buttons = []; buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com")); @@ -118,7 +118,7 @@ botly.sendGeneric(sender, element, function (err, data) { }); ``` -#### sendReceipt (recipientId, payload[, callback]) +#### sendReceipt (recipientId, payload[, notificationType][, callback]) ```javascript let payload = { "recipient_name": "Stephane Crozatier", diff --git a/lib/Botly.js b/lib/Botly.js index d26aed6..5530660 100644 --- a/lib/Botly.js +++ b/lib/Botly.js @@ -116,7 +116,12 @@ Botly.prototype.setWelcomeScreen = function (pageId, message, callback) { }); }; -Botly.prototype.send = function (recipientId, message, callback) { +Botly.prototype.send = function (recipientId, message, notificationType, callback) { + notificationType = notificationType || this.notificationType; + if (typeof notificationType === "function") { + callback = notificationType; + notificationType = this.notificationType; + } request.post( { url: FB_MESSENGER_URL, @@ -127,7 +132,7 @@ Botly.prototype.send = function (recipientId, message, callback) { body: { recipient: {id: recipientId}, message: message, - notification_type: this.notificationType + notification_type: notificationType } }, (err, res, body) => { @@ -137,40 +142,40 @@ Botly.prototype.send = function (recipientId, message, callback) { }); }; -Botly.prototype.sendAttachment = function (recipientId, type, payload, callback) { +Botly.prototype.sendAttachment = function (recipientId, type, payload, notificationType, callback) { this.send(recipientId, { attachment: { type: type, payload: payload } - }, callback); + }, notificationType, callback); }; -Botly.prototype.sendImage = function (recipientId, imageURL, callback) { +Botly.prototype.sendImage = function (recipientId, imageURL, notificationType, callback) { this.sendAttachment(recipientId, ATTACHMENT_TYPE.IMAGE, { url: imageURL - }, callback); + }, notificationType, callback); }; -Botly.prototype.sendText = function (recipientId, text, callback) { +Botly.prototype.sendText = function (recipientId, text, notificationType, callback) { this.send(recipientId, { text: text - }, callback); + }, notificationType, callback); }; -Botly.prototype.sendButtons = function (recipientId, text, buttons, callback) { +Botly.prototype.sendButtons = function (recipientId, text, buttons, notificationType, callback) { this.sendAttachment(recipientId, ATTACHMENT_TYPE.TEMPLATE, - this.createButtonTemplate(text, buttons), callback); + this.createButtonTemplate(text, buttons), notificationType, callback); }; -Botly.prototype.sendGeneric = function (recipientId, elements, callback) { +Botly.prototype.sendGeneric = function (recipientId, elements, notificationType, callback) { this.sendAttachment(recipientId, ATTACHMENT_TYPE.TEMPLATE, - this.createGenericTemplate(elements), callback); + this.createGenericTemplate(elements), notificationType, callback); }; -Botly.prototype.sendReceipt = function (recipientId, payload, callback) { +Botly.prototype.sendReceipt = function (recipientId, payload, notificationType, callback) { payload.template_type = TEMPLATE_TYPE.RECEIPT; - this.sendAttachment(recipientId, ATTACHMENT_TYPE.TEMPLATE, payload, callback); + this.sendAttachment(recipientId, ATTACHMENT_TYPE.TEMPLATE, payload, notificationType, callback); }; Botly.prototype.createWebURLButton = function (title, url) { diff --git a/test/botly_test.js b/test/botly_test.js index 3f44d98..1a63bf1 100644 --- a/test/botly_test.js +++ b/test/botly_test.js @@ -155,6 +155,35 @@ describe("Botly Tests", function () { }); + it("should emit error when there is one", done => { + + var botly = new Botly({ + accessToken: "myToken", + verifyToken: "myVerifyToken", + webHookPath: "/webhook", + notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH + }); + var router = botly.router(); + + botly.on("error", (err) => { + expect(err).to.be.defined; + done(); + }); + + var response = http.createResponse(); + var request = http.createRequest({ + method: "POST", + url: "/webhook", + body: { + "object": "page", + "entry": "blabla" + } + }); + + router.handle(request, response); + + }); + it("should handle delivery messages", done => { var mids = [ "mid.1458668856218:ed81099e15d3f4f233" @@ -378,7 +407,8 @@ describe("Botly Tests", function () { notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH }); - botly.sendText(USER_ID, "hi", ()=>{}); + botly.sendText(USER_ID, "hi", ()=> { + }); expect(request.post.calledOnce).to.be.true; expect(request.post.args[0][0].body).to.eql({ @@ -429,10 +459,7 @@ describe("Botly Tests", function () { notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH }); - var buttons = []; - buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com")); - buttons.push(botly.createPostbackButton("Continue", "continue")); - botly.sendButtons(USER_ID, "What do you want to do next?", buttons, function (err, data) { + botly.sendButtons(USER_ID, "What do you want to do next?", botly.createPostbackButton("Continue", "continue"), function (err, data) { }); expect(request.post.calledOnce).to.be.true; @@ -442,11 +469,6 @@ describe("Botly Tests", function () { "payload": { "buttons": [ - { - "title": "Go to Askrround", - "type": "web_url", - "url": "http://askrround.com" - }, { "payload": "continue", "title": "Continue", @@ -475,13 +497,10 @@ describe("Botly Tests", function () { notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH }); - var buttons = []; - buttons.push(botly.createWebURLButton("Go to Askrround", "http://askrround.com")); - buttons.push(botly.createPostbackButton("Continue", "continue")); var element = botly.createElement("What do you want to do next?", "https://upload.wikimedia.org/wikipedia/en/9/93/Tanooki_Mario.jpg", "https://upload.wikimedia.org/wikipedia/en/9/93/Tanooki_Mario.jpg", - "Choose now!", buttons); + "Choose now!", botly.createWebURLButton("Go to Askrround", "http://askrround.com")); botly.sendGeneric(USER_ID, element); expect(request.post.calledOnce).to.be.true; @@ -496,11 +515,6 @@ describe("Botly Tests", function () { "title": "Go to Askrround", "type": "web_url", "url": "http://askrround.com" - }, - { - "payload": "continue", - "title": "Continue", - "type": "postback" } ], "image_url": "https://upload.wikimedia.org/wikipedia/en/9/93/Tanooki_Mario.jpg", @@ -580,7 +594,7 @@ describe("Botly Tests", function () { } ] }; - botly.sendReceipt(USER_ID, payload); + botly.sendReceipt(USER_ID, payload, Botly.CONST.NOTIFICATION_TYPE.REGULAR); expect(request.post.calledOnce).to.be.true; expect(request.post.args[0][0].body).to.eql({ @@ -640,7 +654,7 @@ describe("Botly Tests", function () { "type": "template" } }, - "notification_type": "NO_PUSH", + "notification_type": "REGULAR", "recipient": { "id": "333" } @@ -680,7 +694,8 @@ describe("Botly Tests", function () { notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH }); - botly.setWelcomeScreen(PAGE_ID, {text:"hi"}, ()=>{}); + botly.setWelcomeScreen(PAGE_ID, {text: "hi"}, ()=> { + }); expect(request.post.calledOnce).to.be.true; expect(request.post.args[0][0].body).to.eql({