Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ var menuButtons = [
bot.setPersistentMenu(menuButtons);
```

#### ```bot.sendMessage(userId, message, notificationType, cb)```
Send a custom message to a specific user.
* ```userId``` - The recipient's Facebook Id
* ```message``` - The custom message object. See [Facebook send API](https://developers.facebook.com/docs/messenger-platform/reference/send-api#message) for more detailed format.
* ```notificationType``` - Optional, push notification type: REGULAR (default), SILENT_PUSH, NO_PUSH
* ```cb``` - Optional, callback with arguments of ```err``` and ```result```.

```js
var message = {
quick_replies: [
{
"content_type": "text",
"title": "Good",
"payload": "thumbs_up"
},
{
"content_type": "text",
"title": "Bad",
"payload": "thumbs_down"
}],
attachment: {
type: "image",
payload: {
url: "http://placehold.it/240x240&text=helloworld"
}
}
};
bot.sendMessage(recipient, message);
```

#### ```bot.sendTextMessage(userId, text, notificationType, cb)```
Send a text message to a specific user.
* ```userId``` - The recipient's Facebook Id
Expand Down
25 changes: 12 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ FBBotFramework.prototype.verify = function (req, res) {

// Send API, Details please visit https://developers.facebook.com/docs/messenger-platform/send-api-reference#request

FBBotFramework.prototype.send = function (recipient, messageData, notificationType, cb) {
FBBotFramework.prototype.sendMessage = function (recipient, messageData, notificationType, cb) {
notificationType = notificationType || NOTIFICATION_TYPE.REGULAR;

if (typeof notificationType === 'function') {
Expand Down Expand Up @@ -81,10 +81,9 @@ FBBotFramework.prototype.send = function (recipient, messageData, notificationTy

};


FBBotFramework.prototype.sendTextMessage = function (recipient, text, notificationType, cb) {
var messageData = {text: text};
this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);
};

FBBotFramework.prototype.sendAudioAttachment = function (recipient, audioUrl, notificationType, cb) {
Expand All @@ -95,7 +94,7 @@ FBBotFramework.prototype.sendAudioAttachment = function (recipient, audioUrl, no
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);
};

FBBotFramework.prototype.sendVideoAttachment = function (recipient, videoUrl, notificationType, cb) {
Expand All @@ -106,7 +105,7 @@ FBBotFramework.prototype.sendVideoAttachment = function (recipient, videoUrl, no
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);
};

FBBotFramework.prototype.sendFileAttachment = function (recipient, fileUrl, notificationType, cb) {
Expand All @@ -117,7 +116,7 @@ FBBotFramework.prototype.sendFileAttachment = function (recipient, fileUrl, noti
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);
};

// TODO: Audio, Video and File Upload
Expand All @@ -130,7 +129,7 @@ FBBotFramework.prototype.sendImageMessage = function (recipient, imageUrl, notif
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);
};

FBBotFramework.prototype.sendButtonMessage = function (recipient, text, buttons, notificationType, cb) {
Expand All @@ -146,7 +145,7 @@ FBBotFramework.prototype.sendButtonMessage = function (recipient, text, buttons,
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);
};


Expand All @@ -168,7 +167,7 @@ FBBotFramework.prototype.sendBubbleMessage = FBBotFramework.prototype.sendGeneri
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);

};

Expand All @@ -185,7 +184,7 @@ FBBotFramework.prototype.sendReceiptMessage = function (recipient, receipt, noti
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);
};

FBBotFramework.prototype.getUserProfile = function (userId, cb) {
Expand Down Expand Up @@ -368,7 +367,7 @@ FBBotFramework.prototype.sendQuickReplies = function (recipient, text, replies,
quick_replies: replies
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);

};

Expand All @@ -378,7 +377,7 @@ FBBotFramework.prototype.sendLocationRequest = function (recipient, text, notifi
quick_replies: [{content_type: "location"}]
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);

};

Expand All @@ -395,7 +394,7 @@ FBBotFramework.prototype.sendListMessage = function (recipient, elements, notifi
}
};

this.send(recipient, messageData, notificationType, cb);
this.sendMessage(recipient, messageData, notificationType, cb);


};
Expand Down
42 changes: 42 additions & 0 deletions test/test.send.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,46 @@ describe('Send API', function () {

});

it('should send custom message', function (done) {

var message = {
quick_replies: [
{
"content_type": "text",
"title": "Good",
"payload": "thumbs_up"
},
{
"content_type": "text",
"title": "Bad",
"payload": "thumbs_down"
}],
attachment: {
type: "image",
payload: {
url: "http://placehold.it/240x240&text=helloworld"
}
}
};

var payload = {
recipient: {
id: recipient
},
message: message
};

nock('https://graph.facebook.com')
.post('/v2.6/me/messages', payload)
.query({access_token: bot.page_token})
.reply(200, dummyResponse);

bot.sendMessage(recipient, message, function (err, result) {
expect(err).to.be.null;
expect(result).to.deep.equal(dummyResponse);
done();
});

})

});