Setting up order status notifications in Viber 1C-Bitrix

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
    1175
  • 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
    747
  • 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

Setting Up Order Status Notifications in Viber for 1C-Bitrix

Viber provides a public Bot API with a structure similar to Telegram's. Unlike WhatsApp, sending notifications via Viber does not require business verification through Meta — it is enough to create a bot through the Viber Admin Panel. Limitation: the user must subscribe to the bot or message it first before the bot can send them messages.

Creating a Viber Bot

  1. Register an account at developers.viber.com
  2. Create a bot at my.viber.com/~admin/bot/new
  3. Obtain an auth token in the form 47b...==
  4. Set the webhook: POST https://chatapi.viber.com/pa/set_webhook
// Register webhook
$ch = curl_init('https://chatapi.viber.com/pa/set_webhook');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => json_encode([
        'url'           => 'https://your-site.com/viber/webhook/',
        'event_types'   => ['subscribed', 'unsubscribed', 'message'],
        'send_name'     => true,
    ]),
    CURLOPT_HTTPHEADER     => [
        'X-Viber-Auth-Token: ' . VIBER_BOT_TOKEN,
        'Content-Type: application/json',
    ],
    CURLOPT_RETURNTRANSFER => true,
]);
curl_exec($ch);
curl_close($ch);

Storing the Viber user_id

When a user subscribes to the bot, Viber sends a webhook with a subscribed event. At this point we link the Viber user_id to the 1C-Bitrix user:

// Viber webhook handler
$update = json_decode(file_get_contents('php://input'), true);
$eventType = $update['event'] ?? '';

if ($eventType === 'subscribed') {
    $viberUserId = $update['user']['id'];
    $context = $update['user']['context'] ?? '';  // we pass uid= in the deep link

    if (preg_match('/uid=(\d+)/', $context, $m)) {
        $bitrixUserId = (int)$m[1];
        \Bitrix\Main\UserTable::update($bitrixUserId, [
            'UF_VIBER_USER_ID' => $viberUserId,
        ]);
    }
}

The subscription deep link is generated in the user account area:

$deepLink = 'viber://pa?chatURI=' . VIBER_BOT_URI . '&context=uid=' . $USER->GetID();

Sending Notifications on Status Change

\Bitrix\Main\EventManager::getInstance()->addEventHandler(
    'sale',
    'OnSaleOrderStatusChange',
    function (\Bitrix\Main\Event $event) {
        $order = $event->getParameter('ENTITY');
        $statusId = $order->getField('STATUS_ID');
        $userId = $order->getUserId();

        $user = \Bitrix\Main\UserTable::getById($userId)->fetch();
        $viberUserId = $user['UF_VIBER_USER_ID'] ?? null;
        if (!$viberUserId) {
            return;
        }

        $texts = [
            'N' => 'Order #%d has been placed and is awaiting processing.',
            'P' => 'Order #%d has been dispatched for delivery.',
            'F' => 'Order #%d is complete. Thank you for your purchase!',
            'X' => 'Order #%d has been cancelled.',
        ];

        if (!isset($texts[$statusId])) {
            return;
        }

        $message = sprintf($texts[$statusId], $order->getId());

        // Send via Viber API
        $payload = [
            'receiver' => $viberUserId,
            'type'     => 'text',
            'text'     => $message,
            'sender'   => ['name' => 'MyShop'],
        ];

        $ch = curl_init('https://chatapi.viber.com/pa/send_message');
        curl_setopt_array($ch, [
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => json_encode($payload),
            CURLOPT_HTTPHEADER     => [
                'X-Viber-Auth-Token: ' . VIBER_BOT_TOKEN,
                'Content-Type: application/json',
            ],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT        => 5,
        ]);
        $response = json_decode(curl_exec($ch), true);
        curl_close($ch);

        // status_message = 'ok' on success
    }
);

Viber Bot API Limitations

  • You cannot message a user first if they have never interacted with the bot
  • Free tier: no message limits for registered bots
  • Viber is unavailable in some countries (China, parts of the CIS)
  • Messages from a bot that the user has not contacted in over 1 year may not be delivered

Setup Timeline

Bot creation, webhook, UF_VIBER_USER_ID user field, subscription page in the user account area, event handler — 4–8 hours.