Developing a Facebook Messenger bot with Bitrix24 integration

Our company is engaged in the development, support and maintenance of Bitrix and Bitrix24 solutions of any complexity. From simple one-page sites to complex online stores, CRM systems with 1C and telephony integration. The experience of developers is confirmed by certificates from the vendor.
Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1173
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    811
  • image_bitrix-bitrix-24-1c_development_of_an_online_appointment_booking_widget_for_a_medical_center_594_0.webp
    Development based on Bitrix, Bitrix24, 1C for the company Development of an Online Appointment Booking Widget for a Medical Center
    564
  • image_bitrix-bitrix-24-1c_mirsanbel_458_0.webp
    Development based on 1C Enterprise for MIRSANBEL
    745
  • image_crm_dolbimby_434_0.webp
    Website development on CRM Bitrix24 for DOLBIMBY
    655
  • image_crm_technotorgcomplex_453_0.webp
    Development based on Bitrix24 for the company TECHNOTORGKOMPLEKS
    976

Development of Facebook Messenger Bot with Bitrix24 Integration

Facebook Messenger is a working channel for attracting clients for business targeting international audiences or working with Facebook Ads. The bot receives inquiries in Messenger, qualifies them by scenario, and creates leads in Bitrix24. The manager responds to the client from the CRM card — no need to switch tabs.

Technical Basics: Messenger Platform API

Meta provides Messenger Platform API for automating page message work. Data flow:

Client → Facebook Page Messenger
              ↓
        Meta Webhook → POST to bot server
              ↓
        Bot server → Messenger Send API
                  → Bitrix24 REST API

You need:

  • Facebook Developer App with Messenger product.
  • Facebook Page linked to the app.
  • PAGE_ACCESS_TOKEN — long-lived page token.
  • Webhook verification via verify_token.

Webhook Verification and Reception

Meta sends a GET request before webhook activation to verify:

GET /webhook?hub.mode=subscribe&hub.verify_token=MY_TOKEN&hub.challenge=CHALLENGE_CODE

Server checks verify_token and returns hub.challenge.

Incoming messages arrive as POST:

{
  "entry": [{
    "messaging": [{
      "sender": { "id": "123456789" },
      "recipient": { "id": "PAGE_ID" },
      "message": {
        "mid": "m_abc123",
        "text": "Hi, interested in your product"
      }
    }]
  }]
}

Important: Meta expects 200 OK response within 5 seconds, otherwise it retries. Heavy processing (B24 requests) goes to queue.

Sending Messages

function sendMessengerMessage(string $psid, string $text, array $quickReplies = []): void {
    $payload = [
        'recipient' => ['id' => $psid],
        'message'   => ['text' => $text],
    ];

    if ($quickReplies) {
        $payload['message']['quick_replies'] = $quickReplies;
    }

    $ch = curl_init("https://graph.facebook.com/v18.0/me/messages?access_token=" . PAGE_ACCESS_TOKEN);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_exec($ch);
}

// Quick replies (button hints)
$quickReplies = [
    ['content_type' => 'text', 'title' => 'Yes, interested',  'payload' => 'INTERESTED'],
    ['content_type' => 'text', 'title' => 'Just browsing',    'payload' => 'BROWSING'],
];
sendMessengerMessage($psid, 'Need help choosing?', $quickReplies);

Generic Template for Product Cards

Messenger supports structured messages — carousels with images, buttons, links:

$template = [
    'recipient' => ['id' => $psid],
    'message'   => [
        'attachment' => [
            'type'    => 'template',
            'payload' => [
                'template_type' => 'generic',
                'elements'      => [[
                    'title'     => $productName,
                    'image_url' => $productImage,
                    'subtitle'  => $productDescription,
                    'buttons'   => [
                        ['type' => 'web_url',  'url' => $productUrl, 'title' => 'Learn more'],
                        ['type' => 'postback', 'title' => 'Order', 'payload' => "ORDER_{$productId}"],
                    ],
                ]],
            ],
        ],
    ],
];

Getting User Profile

Facebook allows requesting user name and avatar by PSID:

$profile = json_decode(file_get_contents(
    "https://graph.facebook.com/{$psid}?fields=first_name,last_name,profile_pic&access_token=" . PAGE_ACCESS_TOKEN
), true);

This data populates lead fields in Bitrix24: NAME, LAST_NAME, UF_FB_PSID.

Creating Lead in Bitrix24 and Saving Dialog

$leadId = $b24->callMethod('crm.lead.add', [
    'fields' => [
        'TITLE'          => "Messenger: {$firstName} {$lastName}",
        'NAME'           => $firstName,
        'LAST_NAME'      => $lastName,
        'SOURCE_ID'      => 'WEBFORM',
        'SOURCE_DESCRIPTION' => 'Facebook Messenger',
        'UF_CRM_FB_PSID' => $psid,
        'COMMENTS'       => "Facebook Profile ID: {$psid}",
    ],
])['result'];

// Save each message as activity
$b24->callMethod('crm.activity.add', [
    'fields' => [
        'OWNER_TYPE_ID' => 1,
        'OWNER_ID'      => $leadId,
        'TYPE_ID'       => 4,
        'SUBJECT'       => 'Facebook Messenger message',
        'DESCRIPTION'   => "{$firstName}: {$messageText}",
        'DEADLINE'      => date('Y-m-d\TH:i:s'),
    ],
]);

Handoff Protocol: Pass to Live Operator

When client wants to talk to a manager, bot transfers control via Handoff Protocol (pass_thread_control). With Inbox or LiveChat integration, operator takes over dialog. After completion, dialog returns to bot via take_thread_control.

Meta Restrictions and Requirements

  • Bot must reply to client within 24 hours of last message (standard window). Outside window — only approved message templates (Message Tags).
  • HTTPS required for webhook.
  • App undergoes Meta review before public launch (pages_messaging permission).
  • Privacy and data processing policies — mandatory Meta requirement for review.

Timeline

Stage Duration
Meta App setup, webhook, verification 1–2 days
Bot scenario, states, quick replies 3–4 days
B24 integration: leads, activities 2–3 days
Message templates, Generic Templates 1–2 days
Operator handoff 1–2 days
Testing + Meta app review 2–4 days

Total: 2–3 weeks including Meta review (takes up to 5 business days).