From 9b7c4b3884b2edb68f84d61dd6efc5f3d17abd21 Mon Sep 17 00:00:00 2001 From: sleontenko Date: Wed, 14 Jan 2026 22:20:26 +0200 Subject: [PATCH] feat(telegram): auto-migrate group config on supergroup migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a Telegram group is upgraded to a supergroup, the chat ID changes (e.g., -123456 → -100123456). This causes the bot to lose its group configuration since it's keyed by chat ID. This change: - Adds handler for `message:migrate_to_chat_id` event - Logs the migration (old_id → new_id) for visibility - If the old chat ID has config in channels.telegram.groups, automatically: - Copies the config to the new chat ID - Removes the old chat ID entry - Saves the updated config file This eliminates the need to manually update clawdbot.json when groups migrate to supergroups. --- src/telegram/bot-handlers.ts | 56 +++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/telegram/bot-handlers.ts b/src/telegram/bot-handlers.ts index cf9659f54..ada96d4fd 100644 --- a/src/telegram/bot-handlers.ts +++ b/src/telegram/bot-handlers.ts @@ -1,5 +1,7 @@ // @ts-nocheck -import { danger, logVerbose } from "../globals.js"; +import { loadConfig } from "../config/config.js"; +import { writeConfigFile } from "../config/io.js"; +import { danger, logVerbose, warn } from "../globals.js"; import { resolveMedia } from "./bot/delivery.js"; import { resolveTelegramForumThreadId } from "./bot/helpers.js"; import type { TelegramMessage } from "./bot/types.js"; @@ -75,6 +77,58 @@ export const registerTelegramHandlers = ({ } }); + // Handle group migration to supergroup (chat ID changes) + bot.on("message:migrate_to_chat_id", async (ctx) => { + try { + const msg = ctx.message; + if (!msg?.migrate_to_chat_id) return; + + const oldChatId = String(msg.chat.id); + const newChatId = String(msg.migrate_to_chat_id); + const chatTitle = (msg.chat as { title?: string }).title ?? "Unknown"; + + runtime.log?.( + warn( + `[telegram] Group migrated: "${chatTitle}" ${oldChatId} → ${newChatId}`, + ), + ); + + // Check if old chat ID has config and migrate it + const currentConfig = await loadConfig(); + const telegramGroups = currentConfig.channels?.telegram?.groups; + + if (telegramGroups && telegramGroups[oldChatId]) { + const groupConfig = telegramGroups[oldChatId]; + runtime.log?.( + warn( + `[telegram] Migrating group config from ${oldChatId} to ${newChatId}`, + ), + ); + + // Copy config to new ID + telegramGroups[newChatId] = groupConfig; + // Remove old ID + delete telegramGroups[oldChatId]; + + // Save updated config + await writeConfigFile(currentConfig); + runtime.log?.( + warn(`[telegram] Group config migrated and saved successfully`), + ); + } else { + runtime.log?.( + warn( + `[telegram] No config found for old group ID ${oldChatId}, migration logged only`, + ), + ); + } + } catch (err) { + runtime.error?.( + danger(`[telegram] Group migration handler failed: ${String(err)}`), + ); + } + }); + bot.on("message", async (ctx) => { try { const msg = ctx.message;