messenger-bot
A Node client for the Facebook Messenger Platform.
Requires Node >=4.0.0.
Installation
npm install messenger-bot
Example
See more examples in the examples folder.
Run this example in the cloud:
- Setup
PAGE_TOKEN
,VERIFY_TOKEN
,APP_SECRET
and start the example byRun > Start Messenger Echo Bot
. - Your Webhook URL is available at
Preview > 3000
in the IDE.
const http = const Bot = let bot = token: 'PAGE_TOKEN' verify: 'VERIFY_TOKEN' app_secret: 'APP_SECRET' bot bot httpconsole
Usage
Functions
let bot = new Bot(opts)
Returns a new Bot instance.
opts
- Object
token
- String: Your Page Access Token, found in your App settings. Required.verify
- String: A verification token for the first-time setup of your webhook. Optional, but will be required by Facebook when you first set up your webhook.app_secret
- String: Your App Secret token used for message integrity check. If specified, every POST request will be tested for spoofing. Optional.
bot.middleware()
The main middleware for your bot's webhook. Returns a function. Usage:
const http = const Bot = let bot = token: 'PAGE_TOKEN' verify: 'VERIFY_TOKEN' http
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)
Sends a message with the payload
to the target recipient
, and calls the callback. See Send API.
recipient
- Number: The Facebook ID of the intended recipient.payload
- Object: The message payload. Should follow the Send API format.callback
- Function: Called with(err, info)
once the request has completed.err
contains an error, if any, andinfo
contains the response from Facebook, usually with the new message's ID.
bot.getProfile(target, callback)
Returns profile information of the target
, called in the callback
. See User Profile API.
target
- Number: The Facebook ID of the intended target.callback
- Function: Called with(err, profile)
once the request has completed.err
contains an error, if any, andinfo
contains the response from Facebook, in this format:
bot._handleMessage(payload)
The underlying method used by bot.middleware()
to parse the message payload, and fire the appropriate events. Use this if you've already implemented your own middleware or route handlers to receive the webhook request, and just want to fire the events on the bot instance. See the echo bot implemented in Express for an example.
payload
- Object: The payload sent by Facebook to the webhook.
bot._verify(req, res)
The underlying method used by bot.middleware()
for the initial webhook verification. Use this if you've already implemented your own middleware or route handlers, and wish to handle the request without implementing bot.middleware()
. See the echo bot implemented in Express for an example.
req
- Request: Thehttp
request object.res
- Response: Thehttp
response object.
Events
bot.on('message', (payload, reply))
Triggered when a message is sent to the bot.
payload
- Object: An object containing the message event's payload from Facebook. See Facebook's documentation for the format.reply
- Function: A convenience function that callsbot.sendMessage
, with the recipient automatically set to the message sender's Facebook ID. Example usage:
bot
bot.on('postback', (payload, reply))
Triggered when a postback is triggered by the sender in Messenger.
payload
- Object: An object containing the postback event's payload from Facebook. See Facebook's documentation for the format.reply
- Function: A convenience function that callsbot.sendMessage
, with the recipient automatically set to the message sender's Facebook ID. Example usage:
bot
bot.on('delivery', (payload, reply))
Triggered when a message has been successfully delivered.
payload
- Object: An object containing the delivery event's payload from Facebook. See Facebook's documentation for the format.reply
- Function: A convenience function that callsbot.sendMessage
, with the recipient automatically set to the message sender's Facebook ID. Example usage:
bot