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
- Register an account at
developers.viber.com - Create a bot at
my.viber.com/~admin/bot/new - Obtain an auth token in the form
47b...== - 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.







