@@ -92,7 +92,7 @@ async def agregar(update: Update, context: ContextTypes.DEFAULT_TYPE, grouptype,
9292 chat_id = update .message .chat .id )
9393 name = update .message .chat .title
9494 chat_id = str (update .message .chat .id )
95- except :
95+ except Exception :
9696 await update .message .reply_text (
9797 text = f"Mirá, no puedo hacerle un link a este grupo, proba haciendome admin" )
9898 return
@@ -134,17 +134,25 @@ async def agregareci(update: Update, context: ContextTypes.DEFAULT_TYPE):
134134 await agregar (update , context , ECI , "eci" )
135135
136136from telegram .error import Forbidden , BadRequest , RetryAfter , ChatMigrated
137+ from dataclasses import dataclass
138+
139+ @dataclass
140+ class GroupUrlResult :
141+ chat_id : str | None
142+ url : str | None
143+ title : str | None
144+ validated : bool | None
137145
138- async def update_group_url (context : ContextTypes .DEFAULT_TYPE , chat_id : str , max_retries : int = 3 ) -> tuple [ str , str , str , bool ] :
146+ async def update_group_url (context : ContextTypes .DEFAULT_TYPE , chat_id : str , max_retries : int = 3 ) -> GroupUrlResult :
139147 base_delay = 1
140148 for attempt in range (max_retries ):
141149 try :
142150 url = await context .bot .export_chat_invite_link (chat_id = chat_id )
143151 chat = await context .bot .get_chat (chat_id = chat_id )
144- return chat_id , url , chat .title , True
152+ return GroupUrlResult ( chat_id = chat_id , url = url , title = chat .title , validated = True )
145153 except (Forbidden , BadRequest ) as e :
146154 logger .error (f"Bot is no longer allowed to create invite link for { chat_id } : { e } " )
147- return None , None , None , False
155+ return GroupUrlResult ( chat_id = None , url = None , title = None , validated = False )
148156 except ChatMigrated as e :
149157 logger .info (f"Group { chat_id } migrated to { e .new_chat_id } . Retrying with new chat id." )
150158 return await update_group_url (context , str (e .new_chat_id ), max_retries )
@@ -158,7 +166,7 @@ async def update_group_url(context: ContextTypes.DEFAULT_TYPE, chat_id: str, max
158166 await asyncio .sleep (wait_time )
159167
160168 logger .error (f"Max retries reached for { chat_id } . Failing gracefully." )
161- return None , None , None , None
169+ return GroupUrlResult ( chat_id = None , url = None , title = None , validated = None )
162170
163171async def _update_groups (context : ContextTypes .DEFAULT_TYPE ):
164172 logger .info ("Starting update_groups job" )
@@ -175,11 +183,11 @@ async def _update_groups(context: ContextTypes.DEFAULT_TYPE):
175183
176184 for chat_chat_id , db_entries in chats_by_id .items ():
177185 await asyncio .sleep (1 )
178- chat_id , url , title , validated = await update_group_url (context , chat_chat_id )
186+ result = await update_group_url (context , chat_chat_id )
179187
180188 primary_name = db_entries [0 ][1 ]
181189
182- if validated is False :
190+ if result . validated is False :
183191 logger .warning (f"Failed to update URL for group '{ primary_name } '. De-validating." )
184192 with get_session () as session :
185193 for db_id , _ in db_entries :
@@ -190,19 +198,19 @@ async def _update_groups(context: ContextTypes.DEFAULT_TYPE):
190198 await context .bot .send_message (chat_id = DC_GROUP_CHATID , text = f"El grupo { primary_name } murió 💀" )
191199 except Exception as e :
192200 logger .error (f"Failed to send death message for { primary_name } : { e } " )
193- elif validated is True :
201+ elif result . validated is True :
194202 logger .info (f"Updating URL for group '{ primary_name } '" )
195203 with get_session () as session :
196204 for db_id , _ in db_entries :
197205 c = session .query (Listable ).filter_by (id = db_id ).first ()
198206 if c :
199- c .url = url
200- if title and c .name != title :
201- logger .info (f"Updating name for group '{ primary_name } ' to '{ title } '" )
202- c .name = title
203- if str (c .chat_id ) != str (chat_id ):
204- logger .info (f"Updating chat_id for group '{ primary_name } ' from { c .chat_id } to { chat_id } " )
205- c .chat_id = str (chat_id )
207+ c .url = result . url
208+ if result . title and c .name != result . title :
209+ logger .info (f"Updating name for group '{ primary_name } ' to '{ result . title } '" )
210+ c .name = result . title
211+ if str (c .chat_id ) != str (result . chat_id ):
212+ logger .info (f"Updating chat_id for group '{ primary_name } ' from { c .chat_id } to { result . chat_id } " )
213+ c .chat_id = str (result . chat_id )
206214 logger .info ("Finished update_groups job" )
207215
208216from handlers .admin import admin_ids
0 commit comments