Skip to content

Commit 7cc450a

Browse files
committed
fix(groups): move /actualizar_grupos to background task
This prevents Telegram webhook from timing out and endlessly retrying the command, which caused spam.
1 parent de72c29 commit 7cc450a

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

handlers/groups.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,31 @@ async def _update_groups(context: ContextTypes.DEFAULT_TYPE):
184184

185185
from handlers.admin import admin_ids
186186

187+
_update_in_progress = False
188+
189+
async def _background_update(update: Update, context: ContextTypes.DEFAULT_TYPE):
190+
global _update_in_progress
191+
try:
192+
await _update_groups(context)
193+
await context.bot.send_message(chat_id=update.effective_chat.id, text="¡Grupos actualizados!")
194+
except Exception as e:
195+
logger.error(f"Background update failed: {e}", exc_info=True)
196+
await context.bot.send_message(chat_id=update.effective_chat.id, text=f"Error actualizando grupos: {e}")
197+
finally:
198+
_update_in_progress = False
199+
187200
async def actualizar_grupos(update: Update, context: ContextTypes.DEFAULT_TYPE):
201+
global _update_in_progress
188202
user_id = update.effective_user.id
189203
if user_id not in admin_ids and str(user_id) not in admin_ids:
190204
logger.warning(f"Unauthorized user {user_id} tried to access /actualizar_grupos")
191205
return
192206

207+
if _update_in_progress:
208+
await update.message.reply_text("Ya hay una actualización de grupos en progreso. Por favor, esperá a que termine.")
209+
return
210+
211+
_update_in_progress = True
193212
logger.info(f"Manual update of groups triggered by {user_id}")
194-
await update.message.reply_text("Actualizando grupos (esto puede demorar varios minutos)...")
195-
await _update_groups(context)
196-
await update.message.reply_text("¡Grupos actualizados!")
213+
await update.message.reply_text("Actualizando grupos en segundo plano (esto puede demorar varios minutos)...")
214+
asyncio.create_task(_background_update(update, context))

0 commit comments

Comments
 (0)