npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tekpill-messenger-bot

v2.2.0

Published

FB Messenger Platform client [fork from https://github.com/remixz/messenger-bot]. All credits to original author (including package author name). Open source rocks!

Downloads

2

Readme

messenger-bot

Build Status Coverage Status npm version js-standard-style

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: Nitrous Quickstart

  • Setup PAGE_TOKEN, VERIFY_TOKEN, APP_SECRET and start the example by Run > Start Messenger Echo Bot.
  • Your Webhook URL is available at Preview > 3000 in the IDE.
const http = require('http')
const Bot = require('messenger-bot')

let bot = new Bot({
  token: 'PAGE_TOKEN',
  verify: 'VERIFY_TOKEN',
  app_secret: 'APP_SECRET'
})

bot.on('error', (err) => {
  console.log(err.message)
})

bot.on('message', (payload, reply) => {
  let text = payload.message.text

  bot.getProfile(payload.sender.id, (err, profile) => {
    if (err) throw err

    reply({ text }, (err) => {
      if (err) throw err

      console.log(`Echoed back to ${profile.first_name} ${profile.last_name}: ${text}`)
    })
  })
})

http.createServer(bot.middleware()).listen(3000)
console.log('Echo bot server running at port 3000.')

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 = require('http')
const Bot = require('messenger-bot')

let bot = new Bot({
  token: 'PAGE_TOKEN',
  verify: 'VERIFY_TOKEN'
})

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)

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, and info 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, and info contains the response from Facebook, in this format:
{
  "first_name": "Zach",
  "last_name": "Bruggeman",
  "profile_pic": "<url to profile picture>"
}

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: The http request object.
  • res - Response: The http 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 calls bot.sendMessage, with the recipient automatically set to the message sender's Facebook ID. Example usage:
bot.on('message', (payload, reply) => {
  reply({ text: 'hey!'}, (err, info) => {})
})

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 calls bot.sendMessage, with the recipient automatically set to the message sender's Facebook ID. Example usage:
bot.on('postback', (payload, reply) => {
  reply({ text: 'hey!'}, (err, info) => {})
})

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 calls bot.sendMessage, with the recipient automatically set to the message sender's Facebook ID. Example usage:
bot.on('delivery', (payload, reply) => {
  reply({ text: 'hey!'}, (err, info) => {})
})