@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
158 lines (155 loc) • 5.15 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.0.3
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
import { LogLevel } from '../../types/logger.mjs';
import { AbstractHandler } from './abstract-handler.mjs';
import { getEnvironment, Environment } from '../../tools/environment.mjs';
import { TelegramFormatter } from '../formatter/telegram-formatter.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class TelegramHandler extends AbstractHandler {
static {
__name(this, "TelegramHandler");
}
botToken;
chatId;
parseMode;
disableNotification;
disableWebPagePreview;
environment;
warnInBrowser;
constructor(level = LogLevel.ERROR, options) {
super(level, options.bubble);
if (!options.botToken) {
throw new Error("botToken is required for TelegramHandler");
}
if (!options.chatId) {
throw new Error("chatId is required for TelegramHandler");
}
this.botToken = options.botToken;
this.chatId = options.chatId;
this.parseMode = options.parseMode || "HTML";
this.disableNotification = options.disableNotification || false;
this.disableWebPagePreview = options.disableWebPagePreview || true;
this.environment = getEnvironment();
this.warnInBrowser = options.warnInBrowser !== false;
this.setFormatter(new TelegramFormatter(this.parseMode === "HTML"));
}
/**
* @inheritDoc
*/
async handle(record) {
const formatter = this.getFormatter();
if (!formatter) {
console.error("TelegramHandler: No formatter set");
return false;
}
const message = formatter.format(record);
if (this.environment === Environment.BROWSE) {
return this._handleInBrowser(message, record);
} else if (this.environment === Environment.NODE) {
return this._handleInNode(message, record);
}
console.warn("TelegramHandler: Unknown environment, using fallback");
return this._handleFallback(message);
}
/**
* Processing in the browser
*/
async _handleInBrowser(_message, record) {
if (this.warnInBrowser) {
const warningMessage = `\u26A0\uFE0F TelegramHandler: Cannot send logs to Telegram from browser environment.
This would expose your bot token. Consider disabling this handler in browser.
Log message: ${record.message}
If you need to send logs from browser, use a proxy server.`;
console.warn(warningMessage);
const style = "color: #FF9800; background: #FFF3E0; padding: 8px; border: 1px solid #FFB74D; border-radius: 4px;";
console.log("%cTelegram Handler Warning", style, warningMessage);
}
return false;
}
/**
* Processing in Node.js
*/
async _handleInNode(message, _record) {
try {
const url = `https://api.telegram.org/bot${this.botToken}/sendMessage`;
const config = JSON.stringify({
chat_id: this.chatId,
text: message,
parse_mode: this.parseMode,
disable_notification: this.disableNotification,
disable_web_page_preview: this.disableWebPagePreview
});
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: config
});
const result = await response.json();
if (!result.ok) {
console.error("TelegramHandler: Failed to send message", result);
return false;
}
return true;
} catch (error) {
console.error("TelegramHandler: Error sending message", error);
return false;
}
}
/**
* Fallback processing for unknown environments
*/
async _handleFallback(message) {
console.log("TelegramHandler (fallback):", message);
return false;
}
updateSettings(options) {
if (options.botToken) this.botToken = options.botToken;
if (options.chatId) this.chatId = options.chatId;
if (options.parseMode) this.parseMode = options.parseMode;
if (options.disableNotification !== void 0) {
this.disableNotification = options.disableNotification;
}
if (options.disableWebPagePreview !== void 0) {
this.disableWebPagePreview = options.disableWebPagePreview;
}
if (options.warnInBrowser !== void 0) {
this.warnInBrowser = options.warnInBrowser;
}
return this;
}
/**
* Get current environment
*/
getEnvironment() {
return this.environment;
}
/**
* Check if the Telegram API is available
*/
async testConnection() {
if (this.environment === "browser") {
console.warn("TelegramHandler: Cannot test connection in browser environment");
return false;
}
try {
const url = `https://api.telegram.org/bot${this.botToken}/getMe`;
const response = await fetch(url);
const result = await response.json();
return result.ok === true;
} catch (error) {
console.error("TelegramHandler: Test connection failed", error);
return false;
}
}
}
export { TelegramHandler };
//# sourceMappingURL=telegram-handler.mjs.map