Developing a Telegram channel for notifications about 1C-Bitrix orders

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
    1177
  • 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

Developing a Telegram Order Notification Channel for 1C-Bitrix

A Telegram order notification channel is not the same as a bot with private messages. The channel is designed for the team: managers, warehouse operators, and couriers all receive notifications about new orders, status changes, and critical events in a shared stream. This is faster than email and does not require each employee to connect a personal bot. Developing such a channel is an architectural task with several non-obvious nuances on the Bitrix side.

Telegram Channel vs Telegram Bot: Implementation Differences

Personal bot — messages go to the chat_id of a specific user. Opt-in is required, and the chat_id of each employee must be stored.

Channel — messages are published to the channel, all subscribers see them simultaneously. The bot must be added to the channel as an administrator with publish rights. The channel's chat_id is static and never changes.

Group/supergroup — a hybrid: employees can reply, discuss orders, and the bot publishes events. For a team, this is preferable to a channel.

For order notifications, a supergroup is more commonly chosen: a manager can reply to a message with "accepted" or add a comment directly in the chat.

How It Works

[Event in Bitrix] → [Event Handler] → [Message Formatter]
       ↓
[Telegram Bot API: sendMessage / sendPhoto]
       ↓
[Team Channel / Group]

The bot is a technical account with a token. The channel/group is the destination. The bot is added to the channel/group via @telegram → Add Member → @your_bot.

Formatting Order Messages

Telegram supports HTML and Markdown. For orders — a structured HTML message:

class OrderNotificationFormatter
{
    public static function newOrder(\Bitrix\Sale\Order $order): string
    {
        $basket = $order->getBasket();
        $props = $order->getPropertyCollection();

        $name  = $props->getPayerName()?->getValue() ?? 'Not specified';
        $phone = $props->getPhone()?->getValue() ?? '—';
        $email = $props->getUserEmail()?->getValue() ?? '—';

        $itemLines = [];
        foreach ($basket as $item) {
            $itemLines[] = sprintf(
                '  • %s × %d = %s',
                htmlspecialchars($item->getField('NAME')),
                (int)$item->getQuantity(),
                number_format($item->getFinalPrice(), 0, '.', ' ')
            );
        }

        $deliveryName = $order->getDeliverySystemName() ?? 'Not selected';
        $paySystemName = $order->getPaySystemName() ?? 'Not selected';

        return sprintf(
            "🛒 <b>New Order #%d</b>\n\n" .
            "👤 %s\n" .
            "📞 %s\n" .
            "✉️ %s\n\n" .
            "<b>Items:</b>\n%s\n\n" .
            "💰 <b>Total: %s</b>\n" .
            "🚚 %s\n" .
            "💳 %s\n\n" .
            "<a href=\"https://%s/bitrix/admin/sale_order_detail.php?ID=%d\">Open in admin panel</a>",
            $order->getId(),
            htmlspecialchars($name),
            htmlspecialchars($phone),
            htmlspecialchars($email),
            implode("\n", $itemLines),
            number_format($order->getPrice(), 0, '.', ' '),
            htmlspecialchars($deliveryName),
            htmlspecialchars($paySystemName),
            $_SERVER['HTTP_HOST'],
            $order->getId()
        );
    }

    public static function statusChange(\Bitrix\Sale\Order $order, string $oldStatus): string
    {
        $statusList = \CSaleStatus::GetListArray();
        $oldName = $statusList[$oldStatus]['NAME'] ?? $oldStatus;
        $newName = $statusList[$order->getField('STATUS_ID')]['NAME'] ?? $order->getField('STATUS_ID');

        return sprintf(
            "🔄 <b>Order #%d</b>: %s → <b>%s</b>",
            $order->getId(),
            htmlspecialchars($oldName),
            htmlspecialchars($newName)
        );
    }

    public static function lowStock(int $productId, string $productName, float $quantity): string
    {
        return sprintf(
            "⚠️ <b>Low stock:</b> %s\nRemaining: %s pcs.\nID: %d",
            htmlspecialchars($productName),
            number_format($quantity, 0),
            $productId
        );
    }
}

Event Handler with Channel Posting

// /local/php_interface/init.php
use Local\Telegram\ChannelBot;
use Local\Telegram\OrderNotificationFormatter;

$em = \Bitrix\Main\EventManager::getInstance();

// New order
$em->addEventHandler('sale', 'OnSaleOrderSaved', function (\Bitrix\Main\Event $event) {
    $order = $event->getParameter('ENTITY');
    if (!$order->isNew()) {
        return;
    }

    $message = OrderNotificationFormatter::newOrder($order);
    ChannelBot::post($message);
});

// Status change
$em->addEventHandler('sale', 'OnSaleOrderStatusChange', function (\Bitrix\Main\Event $event) {
    $order = $event->getParameter('ENTITY');
    $oldStatus = $event->getParameter('OLD_STATUS');

    $message = OrderNotificationFormatter::statusChange($order, $oldStatus);
    ChannelBot::post($message);
});

Channel Posting Class

// /local/lib/Telegram/ChannelBot.php
namespace Local\Telegram;

use Bitrix\Main\Config\Configuration;

class ChannelBot
{
    private static function getConfig(): array
    {
        return Configuration::getValue('telegram_channel') ?? [];
    }

    public static function post(string $text, array $options = []): ?array
    {
        $config = self::getConfig();
        $token  = $config['bot_token'] ?? '';
        $chatId = $config['channel_id'] ?? ''; // e.g.: -1001234567890

        if (!$token || !$chatId) {
            return null;
        }

        $payload = array_merge([
            'chat_id'                  => $chatId,
            'text'                     => $text,
            'parse_mode'               => 'HTML',
            'disable_web_page_preview' => true,
        ], $options);

        $ch = curl_init('https://api.telegram.org/bot' . $token . '/sendMessage');
        curl_setopt_array($ch, [
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => json_encode($payload),
            CURLOPT_HTTPHEADER     => ['Content-Type: application/json'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT        => 5,
        ]);

        $raw      = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpCode !== 200) {
            \Bitrix\Main\Diag\Debug::writeToFile(
                "Telegram channel error {$httpCode}: {$raw}",
                '', '/local/logs/telegram.log'
            );
            return null;
        }

        return json_decode($raw, true);
    }

    public static function postPhoto(string $photoUrl, string $caption = ''): ?array
    {
        $config = self::getConfig();
        return self::callApi('sendPhoto', [
            'chat_id'   => $config['channel_id'],
            'photo'     => $photoUrl,
            'caption'   => $caption,
            'parse_mode' => 'HTML',
        ]);
    }

    private static function callApi(string $method, array $payload): ?array
    {
        $config = self::getConfig();
        $ch = curl_init('https://api.telegram.org/bot' . $config['bot_token'] . '/' . $method);
        curl_setopt_array($ch, [
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => json_encode($payload),
            CURLOPT_HTTPHEADER     => ['Content-Type: application/json'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT        => 5,
        ]);
        $raw = curl_exec($ch);
        curl_close($ch);
        return json_decode($raw, true);
    }
}

Configuration in /bitrix/.settings.php:

'telegram_channel' => [
    'value' => [
        'bot_token'  => '7654321:AAbcdef...',
        'channel_id' => '-1001234567890',
    ],
],

The channel_id for a channel starts with -100. You can find it by forwarding a message from the channel to @userinfobot.

Additional Events for the Operations Team Channel

Beyond orders, the following are useful to broadcast to the operations channel:

  • Low stock — triggered via a Bitrix agent that checks b_catalog_store_product WHERE AMOUNT < THRESHOLD every hour
  • Export errors to 1Cb_event_log with type SALE_EXCHANGE_ERROR
  • Large order — if b_sale_order.PRICE > N, a separate message with a flag
  • Abandoned cart for more than 3 hours — via a query to b_sale_basket filtered by date

Development Timeline

Stage Contents Duration
Bot and channel setup BotFather, permissions, channel_id 1–2 hours
Message formatter HTML templates for all events 4–6 hours
Sales event handlers New order, statuses, payment 4–6 hours
Additional events Stock levels, errors, large orders 4–8 hours
Testing and debugging All scenarios with real orders 2–4 hours