Skip to content

Commit de72c29

Browse files
committed
fix(groups): handle ChatMigrated exceptions and update chat_id in db
- Catch ChatMigrated exception when exporting chat invite link. - Retry with the new supergroup chat_id provided by the exception. - Update the stored chat_id in the database when a group is migrated.
1 parent 7090d22 commit de72c29

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

handlers/groups.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ async def agregarotros(update: Update, context: ContextTypes.DEFAULT_TYPE):
133133
async def agregareci(update: Update, context: ContextTypes.DEFAULT_TYPE):
134134
await agregar(update, context, ECI, "eci")
135135

136-
from telegram.error import Forbidden, BadRequest, RetryAfter
136+
from telegram.error import Forbidden, BadRequest, RetryAfter, ChatMigrated
137137

138138
async def update_group_url(context: ContextTypes.DEFAULT_TYPE, chat_id: str) -> tuple[str, str, bool]:
139139
try:
@@ -142,6 +142,9 @@ async def update_group_url(context: ContextTypes.DEFAULT_TYPE, chat_id: str) ->
142142
except (Forbidden, BadRequest) as e:
143143
logger.error(f"Bot is no longer allowed to create invite link for {chat_id}: {e}")
144144
return None, None, False
145+
except ChatMigrated as e:
146+
logger.info(f"Group {chat_id} migrated to {e.new_chat_id}. Retrying with new chat id.")
147+
return await update_group_url(context, str(e.new_chat_id))
145148
except RetryAfter as e:
146149
logger.warning(f"Rate limited while creating invite link for {chat_id}. Retry after {e.retry_after} seconds.")
147150
return None, None, None
@@ -174,6 +177,9 @@ async def _update_groups(context: ContextTypes.DEFAULT_TYPE):
174177
c = session.query(Listable).filter_by(id=chat_db_id).first()
175178
if c:
176179
c.url = url
180+
if str(c.chat_id) != str(chat_id):
181+
logger.info(f"Updating chat_id for group '{chat_name}' from {c.chat_id} to {chat_id}")
182+
c.chat_id = str(chat_id)
177183
logger.info("Finished update_groups job")
178184

179185
from handlers.admin import admin_ids

0 commit comments

Comments
 (0)