diff --git a/README.md b/README.md index d2ac49e..545c278 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index c305148..cc7deac 100644 --- a/index.js +++ b/index.js @@ -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') { @@ -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) { @@ -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) { @@ -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) { @@ -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 @@ -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) { @@ -146,7 +145,7 @@ FBBotFramework.prototype.sendButtonMessage = function (recipient, text, buttons, } }; - this.send(recipient, messageData, notificationType, cb); + this.sendMessage(recipient, messageData, notificationType, cb); }; @@ -168,7 +167,7 @@ FBBotFramework.prototype.sendBubbleMessage = FBBotFramework.prototype.sendGeneri } }; - this.send(recipient, messageData, notificationType, cb); + this.sendMessage(recipient, messageData, notificationType, cb); }; @@ -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) { @@ -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); }; @@ -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); }; @@ -395,7 +394,7 @@ FBBotFramework.prototype.sendListMessage = function (recipient, elements, notifi } }; - this.send(recipient, messageData, notificationType, cb); + this.sendMessage(recipient, messageData, notificationType, cb); }; diff --git a/test/test.send.api.js b/test/test.send.api.js index 3b9ecd2..ea3be74 100644 --- a/test/test.send.api.js +++ b/test/test.send.api.js @@ -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(); + }); + + }) + }); \ No newline at end of file