Skip to main content

How to Connect Facebook Messenger to AI Agent

Connect your Flametree AI agent to Facebook Messenger to automatically process and respond to messages sent to your Facebook Page. This integration uses the official Facebook Graph API and supports multi-account, round-robin outbound messaging.


Prerequisites

  • Facebook Developer Account — Register at developers.facebook.com
  • Facebook Page — The agent will respond as this page
  • Facebook App — Created in the Facebook Developer Console
  • Admin access to your Flametree account

Setting Up Facebook Messenger Integration

Step 1: Create Facebook App and Page

  1. Create a Facebook Page

  2. Create a Facebook App

    • Go to developers.facebook.com/apps
    • Click Create App
    • Select Business as the app type
    • Enter app name, contact email, and select your business account
    • Go to App Settings > Basic and switch the app to Live mode to allow messages from real users
  3. Add Messenger Product

    • In your app dashboard, click Add Product
    • Find Messenger and click Set Up

Step 2: Generate Access Tokens and IDs

  1. Link Facebook Page to App

    • In Messenger > Settings, add your Facebook Page to the app
  2. Generate Page Access Token

    • Go to Messenger > Settings
    • Under Access Tokens, select your page and click Generate Token
    • Save this token — you’ll need it for Flametree
  3. Collect Required Parameters

    • Page ID: In Messenger > Settings or from your page URL
    • App ID: In Settings > Basic
    • App Secret: In Settings > Basic
    • Verify Token: Any value you make up yourself (for example, my_fb_webhook_2026). It works like a shared password — the same value must be entered in the Meta App webhook settings and in the Flametree channel configuration so the two systems can verify each other

Step 3: Configure Webhook

  1. Set up Webhook in Facebook App

    • In Messenger > Settings, click Add Callback URL
    • Enter the callback URL provided by Flametree (see below)
    • Enter your Verify Token (must match the value you provide in Flametree )
    • Subscribe to the following events: messages, messaging_postbacks
  2. Callback URL

    • The callback URL will be provided in the Flametree channel setup screen after you create the integration

Step 4: Collect Integration Parameters

Save these values from the Facebook Developer Console:

ParameterLocation in Meta Console
App IDSettings > Basic
App SecretSettings > Basic
Page IDMessenger > API Settings
Access TokenMessenger > API Settings

Configuring Flametree Integration

Step 1: Create Facebook Messenger Integration

  1. Navigate to Settings

    • Go to Settings > Channels
    • Open Facebook Messenger
    • Select Add +
  2. Fill Configuration

    FieldDescriptionRequiredValue Source
    NameCustom integration nameYour choice
    DescriptionIntegration purposeYour choice
    App IDDefault Meta app to subscribe webhookMeta Console
    App SecretStored near the App ID in Meta consoleMeta Console
    Page IDFacebook page IDMeta Console
    Access tokenFacebook page API tokenMeta Console

    Field notes

    • You can connect multiple Facebook Pages by entering comma-separated values in Page ID and Access token. Example: 1234567890, 9876543210. Values must be provided in the same order across related fields.

    • For outbound messages, Flametree uses connected accounts one by one in a fixed repeating sequence. After the first message, each recipient is handled by the same sender account.

  3. Click Save to create the integration

  4. Click Start in the upper-right corner to start the channel.

Step 2: Connect to AI Agent

  1. Open Agent Configuration

    • Navigate to your target AI agent's settings
    • Go to Communication channels section
  2. Assign Channel

    • Select your Facebook Messenger integration from the list
    • Save the agent configuration
  3. Restart the agent: click Stop agent then Start agent to apply the changes.

info

You can use the same Facebook page for multiple agents only for outbound communication. For inbound messaging, every connected agent would reply to the same customer message, causing duplicate or conflicting answers. Best practice is to use a dedicated Facebook page per agent for reliable message handling.

Configuration Options

Configure additional features through Set Environment Variables:

VariableDescriptionDefault
FB_REGISTRATION_DELAYDelay (seconds) between webhook registration attempts5.0
FB_REGISTRATION_COUNTNumber of webhook registration attempts20

Testing the Integration

  1. Send a Message

    • Go to your Facebook Page as a customer (not as an admin)
    • Send a message to the page
  2. Verify Response

    • The AI agent should respond automatically
  3. Test Carousel and Attachments

    • Send a message with a list or media to test advanced features

Security Considerations

  • Protect access tokens and rotate them regularly in Facebook console
  • Enable two-factor authentication on your Facebook account
  • Limit admin access to required team members only
  • Monitor API usage and webhook activity for suspicious behavior
  • Ensure message handling, data retention, and customer consent meet applicable privacy requirements

Was this article helpful?