Sending responses to the user

You’re given a reply function in event emitters. When called, it sends the first argument, responseMessage, back to the user:

messenger.on('text', ({ reply, text }) => {
  reply(responseMessage)
})

The classic, deprecated syntax will also work if you only have one page:

messenger.on('text', ({ senderId, text }) => {
  messenger.send(senderId, responseMessage)
})

The reply version is preferred because it’s more concise and gracefully handles multiple Pages.

If you have multiple Pages or send messages out of band, use Messenger.pageSend():

messenger.on('text', ({ senderId, text, session }) => {
  const pageId = magic()
  messenger.pageSend(pageId, senderId, responseMessage)
})

Some factories for generating responseMessage are available at the top level and are also available in a responses object if you need a namespace:

const { Text, Image, Generic } = require('launch-vehicle-fbm');
const { responses } = require('launch-vehicle-fbm');
// responses.Text, responses.Image, responses.Generic, etc.

The most common response is text:

new Text('Hello World')

Images just need a url. These also show up in the “Shared Photos” rail.

new Image('https://i.imgur.com/ehSTCkO.gif')

The full list of responses you can make are:

class Text(text, args)

Create a text response message

Text supports gettext-like functionality if your project has a messages.js in its root. Using this sample messages.js:

module.exports = {
  greeting_msg: 'Hello World!',
  error_count: 'Errors found: %d'
};

new Text('greeting_msg') would be equivalent to new Text('Hello World!').

You can also use printf-like syntax, like:

  • new Text('error_count', 12)
  • new Text('I have %d %s', 20, 'cabbages')
Arguments:
  • text (string) – Text to send
  • args (mixed) – Any printf substitution arguments
Text.quickReplies(buttons)

Add quick replies to the Text message

Arguments:
  • buttons (Array.<Button>) – Buttons to attach. See quick-replies
Returns:

Text – returns itself for chaining

class Image(url)

Create an Image response message

Arguments:
  • url (string) – URL of the image
Image.quickReplies(buttons)

Add quick replies to the Image message

Arguments:
  • buttons (Array.<Button>) – Buttons to attach. See quick-replies
Returns:

Image – returns itself for chaining

class Generic(elements)

A Generic template. These are the rich elements you’ll use to create interactive elements and carousels.

Arguments:
  • elements (Array.<Object>) – Generic template elements