/** * Telegram Channel Layer — отправка ответов через MAX Messenger API. * * Бот: nik_bot (user_id: 140245519). * API endpoint: https://bot.qw4.ru/bot/nik_bot/message * * FIX: SNI issue с Node.js — используем https модуль с правильными опциями. */ import * as https from 'node:https'; export interface TelegramConfig { botUrl: string; userId: string; } export interface MessageResult { messageId: string; sentAt: number; } /** * Telegram Messenger — отправка сообщений через MAX API. * Поддержка SNI для HTTPS запросов. */ export class TelegramMessenger { private botUrl: string; private userId: string; constructor(config: TelegramConfig) { this.botUrl = config.botUrl; this.userId = config.userId; } /** Отправить сообщение через https module (SNI fix). */ async sendMessage(text: string): Promise { // Парсим URL const url = new URL(this.botUrl); const path = `/message`; const postData = JSON.stringify({ user_id: this.userId, text, }); const options = { hostname: url.hostname, port: 443, path: path, method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData), 'SNI': url.hostname, // Explicit SNI }, // SNI fix для Node.js servername: url.hostname, }; return new Promise((resolve, reject) => { const req = https.request(options, (res) => { let body = ''; res.on('data', (chunk: Buffer) => body += chunk); res.on('end', () => { if (res.statusCode !== 200) { reject(new Error(`Telegram send error: ${res.statusCode}`)); return; } try { const data: any = JSON.parse(body); resolve({ messageId: data.message_id ?? `msg-${Date.now()}`, sentAt: Date.now(), }); } catch (e) { reject(e); } }); }); req.on('error', reject); req.write(postData); req.end(); }); } }