Client: Class

The Client class is used to create a new Messenger client, enabling you to send and receive messages from the Facebook Messenger API. Additionally, the client facilitates server initialization and plugin registration.

Usage

const { Client } = require('chat-bridge')

Constructor

To create a new Client instance, use the following code:

const client = new Client(options)

Options

  • accessToken: The access token for the Facebook Messenger API.
  • verifyToken: The verify token for the Facebook Messenger API.
  • appSecret: The app secret for the Facebook Messenger API.
  • port: The port to run the server on. Defaults to 3000.
  • host: The host to run the server on. Defaults to localhost.
⚠️

Warning: The accessToken and verifyToken options are required.

Methods

Start the server

  • callback: Optional callback function to execute when the server starts.
client.start(() => {
    console.log('Server started!')
})

Register a plugin

client.register(require('fastify-cors'), {
    origin: '*',
})

Send an API message

  • recipientId: The ID of the user to send the message to.
  • object: The message object to send.
client.sendApiMessage('USER_ID', {
    text: 'Hello, world!',
    quick_replies: [
        {
            content_type: 'text',
            title: 'Hello',
            payload: 'HELLO',
        },
    ],
})

Tips: You can use the send method as a shorthand for the sendApiMessage method.

client.send('USER_ID', {
    text: 'Hello, world!',
    quick_replies: [
        {
            content_type: 'text',
            title: 'Hello',
            payload: 'HELLO',
        },
    ],
})

Get page information

const page = await client.getPageInfo()
console.log(page) // { name: "Page Name", id: "PAGE_ID" }

Send a text message

  • recipientId: The ID of the user to send the message to.
  • text: The text to send.
client.sendTextMessage('USER_ID', 'Hello, world!')

Send an attachment

  • recipientId: The ID of the user to send the attachment to.
  • type: The type of attachment to send. Can be image, audio, video, or file.
  • url: The URL of the attachment to send.
client.sendAttachment('USER_ID', 'image', 'https://example.com/image.jpg')

Send an attachment

  • recipientId: The ID of the user to send the attachment to.
  • type: The type of attachment to send. Can be image, audio, video, or file.
  • url: The URL of the attachment to send.
client.sendAttachment('USER_ID', 'image', 'https://example.com/image.jpg')

Send an image

  • recipientId: The ID of the user to send the image to.
  • url: The URL of the image to send.
client.sendImage('USER_ID', 'https://example.com/image.jpg')

Send an audio file

  • recipientId: The ID of the user to send the audio file to.
  • url: The URL of the audio file to send.
client.sendAudio('USER_ID', 'https://example.com/audio.mp3')

Send a video

  • recipientId: The ID of the user to send the video to.
  • url: The URL of the video to send.
client.sendVideo('USER_ID', 'https://example.com/video.mp4')

Send a file

  • recipientId: The ID of the user to send the file to.
  • url: The URL of the file to send.
client.sendFile('USER_ID', 'https://example.com/file.pdf')

Set typing indicator

  • recipientId: The ID of the user to send the typing indicator to.
  • typing: Whether to show the typing indicator. Defaults to true.
client.setTyping('USER_ID', true)

Events

The Client class extends the EventEmitter (opens in a new tab) class, emitting the following events:

Message

New message received.

{
    "sender": {
        "id": "USER_ID"
    },
    "recipient": {
        "id": "PAGE_ID"
    },
    "timestamp": 1458692752478,
    "message": {
        "mid": "mid.1457764197618:41d102a3e1ae206a38",
        "text": "hello, world!"
    }
}

Quick reply

Quick reply received.

{
    "sender": {
        "id": "USER_ID"
    },
    "recipient": {
        "id": "PAGE_ID"
    },
    "timestamp": 1458692752478,
    "message": {
        "mid": "mid.1458696618141:b4ef9d19ec21086067",
        "quick_reply": {
            "payload": "DEVELOPER_DEFINED_PAYLOAD"
        }
    }
}

Postback

Post Back received.

{
    "sender": {
        "id": "USER_ID"
    },
    "recipient": {
        "id": "PAGE_ID"
    },
    "timestamp": 1458692752478,
    "postback": {
        "payload": "USER_DEFINED_PAYLOAD"
    }
}

Basic Usage

Handle incoming messages:

client.on('message', (message) => {
    console.log(message)
})