Skip to content

Commit

Permalink
- added notification type support per send
Browse files Browse the repository at this point in the history
- increased coverage to 100
  • Loading branch information
miki2826 committed May 13, 2016
1 parent 3a4aa9b commit 38f294a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 43 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand All @@ -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,
{
Expand All @@ -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"));
Expand All @@ -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"));
Expand All @@ -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",
Expand Down
33 changes: 19 additions & 14 deletions lib/Botly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand All @@ -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) {
Expand Down
59 changes: 37 additions & 22 deletions test/botly_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
Expand All @@ -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",
Expand Down Expand Up @@ -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;
Expand All @@ -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",
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -640,7 +654,7 @@ describe("Botly Tests", function () {
"type": "template"
}
},
"notification_type": "NO_PUSH",
"notification_type": "REGULAR",
"recipient": {
"id": "333"
}
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 38f294a

Please sign in to comment.