From e9e1dc439faa7ea0ffef61043d5d90c28a31ca7a Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Fri, 8 Nov 2024 14:53:37 +0000 Subject: [PATCH] feat: add handling of supergroup chat creation --- src/index.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/index.ts b/src/index.ts index 6e759b7..bbfdba1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -141,6 +141,34 @@ async function processUpdate(update: Update): Promise { if (message == null) { return; } + + // Handle migration to supergroup + if (message.migrate_to_chat_id) { + const oldChatId = message.chat.id; + const newChatId = message.migrate_to_chat_id; + + // Find any existing webhook for the old chat ID + const oldChat = JSON.stringify({ + chatId: oldChatId, + messageThreadId: message.message_thread_id, + }); + const webhookId = await TG_GROUPS.get(`chat-webhook:${oldChat}`); + + if (webhookId) { + // Update the stored chat info with new chat ID + const newChat = JSON.stringify({ + chatId: newChatId, + messageThreadId: message.message_thread_id, + }); + await TG_GROUPS.put(`chat-webhook:${newChat}`, webhookId); + await TG_GROUPS.put(`webhook-chat:${webhookId}`, newChat); + + // Delete old mapping + await TG_GROUPS.delete(`chat-webhook:${oldChat}`); + } + return; + } + if (message.text === "/webhook" || message.text === "/webhook@telerts_bot") { const chatId = message.chat.id; const chat = {