Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility of sending message with tag #89

Merged
merged 10 commits into from
Jul 1, 2018
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ http.createServer(bot.middleware()).listen(3000)

As well, it mounts `/_status`, which will return `{"status": "ok"}` if the middleware is running. If `verify` is specified in the bot options, it will mount a handler for `GET` requests that verifies the webhook.

#### `bot.sendMessage(recipient, payload, [callback])`
#### `bot.sendMessage(recipient, payload, [callback], [messagingType], [tag])`

Sends a message with the `payload` to the target `recipient`, and calls the callback if any. Returns a promise. See [Send API](https://developers.facebook.com/docs/messenger-platform/send-api-reference#request).

* `recipient` - Number: The Facebook ID of the intended recipient.
* `payload` - Object: The message payload. Should follow the [Send API format](https://developers.facebook.com/docs/messenger-platform/send-api-reference).
* `callback` - (Optional) Function: Called with `(err, info)` once the request has completed. `err` contains an error, if any, and `info` contains the response from Facebook, usually with the new message's ID.
* `messagingType` - (Optional) String: The message type. [Supported Messaging Type](https://developers.facebook.com/docs/messenger-platform/send-messages#messaging_types).
* `tag` - (Optional) String: The tag's message. [Supported Tags](https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags#supported_tags).

#### `bot.sendSenderAction(recipient, senderAction, [callback])`

Expand Down
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,27 @@ class Bot extends EventEmitter {
})
}

sendMessage (recipient, payload, cb) {
return request({
sendMessage (recipient, payload, cb, messagingType = null, tag = null) {
let options = {
method: 'POST',
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: this._getQs(),
json: {
recipient: { id: recipient },
message: payload
}
})
}
if (messagingType === 'MESSAGE_TAG') {
if (tag != null) {
options.json.tag = tag
} else {
cb(new Error('You must specify a Tag'))
}
}
if (messagingType != null) {
options.json.messaging_type = messagingType
}
return request(options)
.then(body => {
if (body.error) return Promise.reject(body.error)
if (!cb) return body
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"devDependencies": {
"nock": "^9.0.0",
"standard": "^11.0.1",
"tap": "^11.1.5"
"tap": "10.7.3"
}
}