feat(telegram): auto-migrate group config on supergroup migration

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.
This commit is contained in:
sleontenko
2026-01-14 22:20:26 +02:00
committed by Peter Steinberger
parent bcde09ae91
commit 9b7c4b3884

View File

@@ -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;