queue to telegram retry

Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
Omar Sánchez Pizarro
2025-10-10 13:31:37 +02:00
parent 60db36ba99
commit b5178f415b
2 changed files with 14 additions and 29 deletions

View File

@@ -6,6 +6,7 @@ from managers.telegram_manager import TelegramManager
MESSAGE_DELAY = 3.0 # Tiempo de espera entre mensajes en segundos MESSAGE_DELAY = 3.0 # Tiempo de espera entre mensajes en segundos
NOTIFIED_ARTICLES_LIMIT = 300 # Límite de artículos notificados a mantener en memoria NOTIFIED_ARTICLES_LIMIT = 300 # Límite de artículos notificados a mantener en memoria
RETRY_TIMES = 3
class QueueManager: class QueueManager:
def __init__(self): def __init__(self):
@@ -19,14 +20,14 @@ class QueueManager:
self._processor_thread = threading.Thread(target=self._process_queue, daemon=True) self._processor_thread = threading.Thread(target=self._process_queue, daemon=True)
self._processor_thread.start() self._processor_thread.start()
def add_to_queue(self, article, search_name=None, thread_id=None): def add_to_queue(self, article, search_name=None, thread_id=None, retry_times=RETRY_TIMES):
# Verificar si el artículo ya ha sido enviado # Verificar si el artículo ya ha sido enviado
if article in self._notified_articles: if article in self._notified_articles:
return return
if search_name is None: if search_name is None:
search_name = "Unknown" search_name = "Unknown"
self._queue.put((search_name, article, thread_id)) self._queue.put((search_name, article, thread_id, retry_times))
self.logger.debug(f"Artículo añadido a la cola: {article.get_title()}") self.logger.debug(f"Artículo añadido a la cola: {article.get_title()}")
self.add_to_notified_articles(article) self.add_to_notified_articles(article)
@@ -45,16 +46,19 @@ class QueueManager:
try: try:
# Esperar hasta que haya un elemento en la cola (timeout de 1 segundo) # Esperar hasta que haya un elemento en la cola (timeout de 1 segundo)
try: try:
search_name, article, thread_id = self._queue.get(timeout=1.0) search_name, article, thread_id, retry_times = self._queue.get(timeout=1.0)
except queue.Empty: except queue.Empty:
continue continue
# Procesar el artículo # Procesar el artículo
try: try:
self._telegram_manager.send_telegram_article(search_name, article, thread_id) self._telegram_manager.send_telegram_article(search_name, article, thread_id)
except Exception as e: except Exception as e:
self.logger.error(f"Error al enviar artículo a Telegram: {e}") self.logger.error(f"Error al enviar artículo a Telegram: {e}")
if retry_times > 0:
self._queue.put((search_name, article, thread_id, retry_times - 1))
else:
self.logger.error(f"Artículo no enviado después de {RETRY_TIMES} intentos")
finally: finally:
self._queue.task_done() self._queue.task_done()

View File

@@ -83,29 +83,9 @@ class TelegramManager:
) )
images_url = article.get_images() images_url = article.get_images()
media = [] # Envía solo la primera imagen usando sendPhoto en vez de un álbum/media group
for idx, image_url in enumerate(images_url): first_image_url = images_url[0] if images_url else None
if idx == 0:
media.append(
telegram.InputMediaPhoto(
media=image_url,
caption=message,
parse_mode="HTML"
)
)
else:
media.append(
telegram.InputMediaPhoto(
media=image_url
)
)
# Enviar el media group
sent_messages = await self._bot.send_media_group(
chat_id=self._channel,
media=media,
message_thread_id=thread_id
)
# Crear botones inline para el primer mensaje del grupo # Crear botones inline para el primer mensaje del grupo
keyboard = [ keyboard = [
@@ -119,9 +99,10 @@ class TelegramManager:
# Enviar un mensaje adicional con los botones (reply al primer mensaje del grupo) # Enviar un mensaje adicional con los botones (reply al primer mensaje del grupo)
await self._bot.send_message( await self._bot.send_message(
chat_id=self._channel, chat_id=self._channel,
text="💾 Acciones", photo=first_image_url,
text=message,
parse_mode="HTML",
reply_markup=reply_markup, reply_markup=reply_markup,
reply_to_message_id=sent_messages[0].message_id,
message_thread_id=thread_id message_thread_id=thread_id
) )