v2.2.0

Telegram Handler

Sending logs to Telegram

Quick Start

Installing and setting up a Telegram bot

# Create a bot via @BotFather
# Get the token and chat_id

Usage on the server side

import { LogLevel, Logger, TelegramHandler } from '@bitrix24/b24jssdk'

// Create a logger with Telegram handler
const $logger = new Logger('my-app')

const telegramHandler = new TelegramHandler(LogLevel.ERROR, {
  botToken: 'YOUR_BOT_TOKEN',
  chatId: 'YOUR_CHAT_ID',
  parseMode: 'HTML', // or 'MarkdownV2'
  disableNotification: false, // Send notifications
  disableWebPagePreview: true // Disable link previews
});

$logger.pushHandler(telegramHandler)

// Now critical errors will be sent to Telegram
$logger.error('Payment service unavailable', {
  service: 'stripe',
  error: 'Connection timeout',
  duration: '30s'
})

Usage on the client side

On the client side it is not supported, for security reasons: the request carries the bot token in its URL, and client code is readable.That covers a worker too. A Web, Shared or Service Worker has no DOM, but its script is fetched over the network and as readable as the page's — so handle() sends nothing and warns, and testConnection() refuses rather than contacting Telegram. Before 3.0.0 a worker fell through to the unknown-environment fallback: no warning, and testConnection() would have made the request.Pass warnInBrowser: false to silence the console warning (the handler still sends nothing).Do not register this handler in code that ships to a browser or a worker at all. The refusal depends on the SDK recognising the runtime, and a runtime can be made to look like a server — a browser worker bundled with a process polyfill that reports a Node version is read as one, and the handler will send. Detection is a safety net, not a boundary; the token belongs on a server.