Getting Started

Getting Started with Chat Bridge

Follow these simple steps to create a bot that echoes back what you send it.

Create a Facebook App and Page

To use Chat Bridge, you need to create a Facebook App (opens in a new tab) and Page (opens in a new tab). The Facebook App is the container for your bot, and the Page is the identity that your bot assumes when talking to users.

Step 1: Install Chat Bridge

To integrate Facebook Messenger webhook handling into your Node.js applications, install Chat Bridge using the following command in your terminal:

Terminal
npm i chat-bridge

Step 2: Configure Environment Variables

Chat Bridge uses environment variables to configure your bot. Create a new file named .env and add the following:

.env
ACCESS_TOKEN = YOUR_ACCESS_TOKEN
VERIFY_TOKEN = YOUR_VERIFY_TOKEN
  • ACCESS_TOKEN is your Facebook Page's access token (found in your App Dashboard under the "Messenger" tab).
  • VERIFY_TOKEN is any string of your choosing, used to verify the webhook's authenticity.

Step 3: Create a Bot

Create a new file named index.js and add the following code:

index.js
// Import the Client class
const { Client } = require('chat-bridge')
const dotenv = require('dotenv')
 
// Load environment variables from .env file
dotenv.config()
 
// Create a new Client instance
const client = new Client({
    accessToken: 'YOUR_ACCESS_TOKEN',
    verifyToken: 'YOUR_VERIFY_TOKEN',
})
 
// Listen for 'message' events
client.on('message', (event) => {
    const { sender, message } = event
 
    // Send a text message back to the user
    client.sendTextMessage(sender.id, `You wrote: ${message.text}`)
})
 
// Start the bot
client.start(() => {
    console.log('Bot is running')
})

Step 4: Start the Bot

Run the following command to start your bot:

Terminal
# Start the Bot
node index.js # or whatever you named your file

Visit http://localhost:8080/ (opens in a new tab) to test that the bot is running. If everything is working, you should see the message "Bot is running."