Developing a VK 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

Development of VK Bot with Bitrix24 Integration

A user writes in a VK group's messages — the bot responds, qualifies the lead, and creates a deal in Bitrix24. It's not just an autoresponder: the conversation is saved in CRM, the manager sees the correspondence in the lead card and can reply directly from B24.

Two Ways to Integrate VK with Bitrix24

Method 1: Bitrix24 Open Lines. B24 has a native connector for VK. It connects in open lines settings (imopenlines) and works "out of the box" — if your plan includes this feature. Limitations: text and attachments only, no programmable bot logic, no autoresponses with branching scenario.

Method 2: Custom integration via VK API. A bot on your server receives messages from VK via Callback API or Long Poll, processes them by its logic, communicates with B24 via REST API. Full control over behavior.

For complex scenarios (lead qualification, button menus, photo/voice processing) — you need a custom bot.

Architecture of Custom VK Bot

User → VK Group Messages
              ↓
      VK Callback API (POST to bot server)
              ↓
      Bot server (PHP/Node.js)
      ├── Conversation logic (FSM / scenario)
      ├── VK API: messages.send, messages.getHistory
      └── Bitrix24 REST API:
          ├── crm.lead.add / crm.deal.add
          ├── crm.activity.add (save dialog)
          └── im.message.add (notify manager)

Setting Up VK Callback API

In the VK group settings → "Work with API" → "Callback API" specify server address and subscribe to events message_new, message_reply, message_event (button click).

VK sends a POST request to the server with JSON:

{
  "type": "message_new",
  "object": {
    "message": {
      "from_id": 123456789,
      "text": "Hi, how to order?",
      "id": 987
    }
  },
  "group_id": 111222333
}

Server must respond with string ok within 5 seconds.

Bot Logic: FSM (Finite State Machine)

The conversation scenario is implemented by storing dialog state:

// Dialog state stored in Redis: vk_dialog:{vk_user_id}
$state = $redis->get("vk_dialog:{$userId}");

switch ($state) {
    case null:
        // New dialog
        sendVkMessage($userId, "Hello! Do you want to:\n1. Get a quote\n2. Submit request\n3. Contact manager");
        $redis->set("vk_dialog:{$userId}", 'menu', 3600);
        break;

    case 'menu':
        if ($text === '2' || stripos($text, 'request') !== false) {
            sendVkMessage($userId, "Enter your phone:");
            $redis->set("vk_dialog:{$userId}", 'await_phone', 3600);
        }
        break;

    case 'await_phone':
        if (isValidPhone($text)) {
            createLeadInB24($userId, $phone);
            sendVkMessage($userId, "Thank you! Manager will contact you within an hour.");
            $redis->del("vk_dialog:{$userId}");
        } else {
            sendVkMessage($userId, "Can't parse number. Enter in format +1XXXXXXXXXX");
        }
        break;
}

Creating Lead in Bitrix24

function createLeadInB24(int $vkUserId, string $phone): int {
    // Get VK user data
    $vkUser = callVkApi('users.get', ['user_ids' => $vkUserId, 'fields' => 'photo_200']);
    $name   = $vkUser[0]['first_name'] . ' ' . $vkUser[0]['last_name'];

    // Create lead in B24
    $result = $b24->callMethod('crm.lead.add', [
        'fields' => [
            'TITLE'        => "VK: {$name}",
            'NAME'         => $vkUser[0]['first_name'],
            'LAST_NAME'    => $vkUser[0]['last_name'],
            'PHONE'        => [['VALUE' => $phone, 'VALUE_TYPE' => 'WORK']],
            'SOURCE_ID'    => 'VK',
            'COMMENTS'     => "VK ID: {$vkUserId}\nhttps://vk.com/id{$vkUserId}",
            'ASSIGNED_BY_ID' => 5,
        ],
    ]);

    // Attach conversation history as activity
    $b24->callMethod('crm.activity.add', [
        'fields' => [
            'OWNER_TYPE_ID' => 1,
            'OWNER_ID'      => $result['result'],
            'TYPE_ID'       => 4,
            'SUBJECT'       => 'VK Conversation',
            'DESCRIPTION'   => $dialogHistory,
        ],
    ]);

    return $result['result'];
}

Buttons in VK Messages

VK supports keyboards with buttons:

$keyboard = [
    'one_time' => true,
    'buttons'  => [[
        ['action' => ['type' => 'text', 'label' => 'Submit request', 'payload' => '{"action":"lead"}'], 'color' => 'primary'],
        ['action' => ['type' => 'text', 'label' => 'Call us', 'payload' => '{"action":"call"}'], 'color' => 'secondary'],
    ]],
];

callVkApi('messages.send', [
    'user_id'   => $userId,
    'message'   => 'Choose action:',
    'keyboard'  => json_encode($keyboard),
    'random_id' => time(),
]);

Manager Notification in Bitrix24

After creating lead, send notification to responsible manager via im.notify:

$b24->callMethod('im.notify', [
    'to'      => $managerId,
    'message' => "New lead from VK: {$name}, phone {$phone}. [URL=https://b24.ru/crm/lead/{$leadId}/]Open[/URL]",
    'type'    => 'SYSTEM',
]);

Timeline

Stage Duration
VK Callback API setup, basic message reception 1–2 days
Bot scenario (FSM, state storage) 2–4 days
B24 integration: lead creation, activities 2–3 days
Buttons, menus, attachment handling 1–2 days
Manager notifications 1 day
Testing 1–2 days

Total: 1.5–2 weeks for standard lead qualification scenario.