Skip to main content
The Facebook Messenger provider enables building chatbots for Facebook Messenger, allowing you to create automated conversations on Facebook Pages.

Features

  • Text messaging
  • Media support
  • Quick replies and buttons
  • Templates and cards
  • Webhook integration
  • Page integration
  • User profile access

Prerequisites

1

Create Facebook Page

Create a Facebook Page for your business
2

Create Facebook App

Set up a Facebook App in the Meta Developer Portal
3

Configure Messenger

Add Messenger product to your app
4

Get Credentials

Obtain:
  • Page Access Token
  • App Secret
  • Verify Token

Installation

npm install @builderbot/bot @builderbot/provider-facebook-messenger

Configuration

Basic Setup

import { createBot, createProvider, createFlow } from '@builderbot/bot'
import { FacebookMessengerProvider } from '@builderbot/provider-facebook-messenger'
import { MemoryDB } from '@builderbot/bot'

const provider = createProvider(FacebookMessengerProvider, {
  pageAccessToken: 'YOUR_PAGE_ACCESS_TOKEN',
  appSecret: 'YOUR_APP_SECRET',
  verifyToken: 'YOUR_VERIFY_TOKEN',
  port: 3000
})

const { handleCtx, httpServer } = await createBot({
  flow: adapterFlow,
  provider: provider,
  database: new MemoryDB(),
})

httpServer(3000)

Environment Variables

FB_PAGE_ACCESS_TOKEN=your_page_access_token
FB_APP_SECRET=your_app_secret
FB_VERIFY_TOKEN=your_verify_token
PORT=3000

Basic Usage

Sending Text Messages

import { addKeyword } from '@builderbot/bot'

const welcomeFlow = addKeyword(['hi', 'hello'])
  .addAnswer('Hello! Welcome to our Facebook page')
  .addAnswer('How can we help you today?')

Sending Media

const mediaFlow = addKeyword('image')
  .addAnswer('Here is an image:', {
    media: 'https://example.com/image.jpg'
  })

Webhook Configuration

Webhook Endpoints

GET  /webhook - Webhook verification
POST /webhook - Receive messages

Facebook App Setup

  1. Go to your Facebook App Dashboard
  2. Navigate to Messenger > Settings
  3. Set webhook URL: https://your-domain.com/webhook
  4. Enter your verify token
  5. Subscribe to page events

Quick Replies

const quickReplyFlow = addKeyword('options')
  .addAnswer(
    'Choose an option:',
    {
      buttons: [
        { body: 'Option 1' },
        { body: 'Option 2' },
        { body: 'Option 3' }
      ]
    }
  )

Best Practices

  • Store tokens securely
  • Use environment variables
  • Rotate tokens periodically
  • Monitor token expiration
  • Validate webhook signatures
  • Use HTTPS in production
  • Verify request origin
  • Implement rate limiting

Further Resources

Build docs developers (and LLMs) love