From b22381061236a3a9568a5a906375f73dcbe16f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Fri, 8 May 2026 18:08:09 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=BF=D1=83=D1=81?= =?UTF-8?q?=D0=BA=20Telegram=20bridge=20=D0=B1=D0=B5=D0=B7=20PM2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/telegram.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/backend/src/telegram.js b/backend/src/telegram.js index 886cd98..8b6f734 100644 --- a/backend/src/telegram.js +++ b/backend/src/telegram.js @@ -54,7 +54,29 @@ export function getTelegramBridgeStatus() { }; } +export async function stopTelegramBridge(reason = 'restart') { + if (bot) { + try { + await bot.stop(reason); + } catch { + // Telegraf can throw if polling was never started. Safe to ignore on restart. + } + } + + bot = null; + botStatus = { + ...botStatus, + running: false, + error: '', + startedAt: null + }; + + return getTelegramBridgeStatus(); +} + export async function startTelegramBridge({ logger } = {}) { + await stopTelegramBridge('restart'); + const settings = getTelegramSettings({ revealSecrets: true }); const token = settings.botToken; @@ -136,7 +158,7 @@ export async function startTelegramBridge({ logger } = {}) { }); const me = await bot.telegram.getMe(); - await bot.launch(); + await bot.launch({ dropPendingUpdates: true }); botStatus = { enabled: true, @@ -149,9 +171,6 @@ export async function startTelegramBridge({ logger } = {}) { logger?.info?.({ username: botStatus.username, proxyEnabled: botStatus.proxyEnabled }, 'Telegram bridge started'); - process.once('SIGINT', () => bot?.stop('SIGINT')); - process.once('SIGTERM', () => bot?.stop('SIGTERM')); - return botStatus; } catch (error) { botStatus = { @@ -164,6 +183,11 @@ export async function startTelegramBridge({ logger } = {}) { } } +export async function restartTelegramBridge({ logger } = {}) { + await stopTelegramBridge('manual restart'); + return startTelegramBridge({ logger }); +} + export async function notifyOperatorsAboutVisitorMessage({ site, visitor, conversation, message, logger } = {}) { if (!botStatus.running || !bot) return { ok: false, sent: 0, error: 'telegram bridge is not running' };