.
This commit is contained in:
parent
91338ca40f
commit
04cf58dd1f
34
main.py
34
main.py
@ -390,18 +390,30 @@ class TelegramClient:
|
||||
if not NOTIFY_ON_WEEKENDS and msk_now().weekday() >= 5:
|
||||
log_info(f"[notify] Выходной день, отправка пропущена chat_id={chat_id}")
|
||||
return
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=20) as client:
|
||||
response = await client.post(
|
||||
f"{self.base_url}/sendMessage",
|
||||
json={"chat_id": chat_id, "text": text},
|
||||
delays = (0, 2, 5)
|
||||
last_exc: Exception | None = None
|
||||
for attempt_idx, delay_s in enumerate(delays, start=1):
|
||||
if delay_s > 0:
|
||||
await asyncio.sleep(delay_s)
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=45) as client:
|
||||
response = await client.post(
|
||||
f"{self.base_url}/sendMessage",
|
||||
json={"chat_id": chat_id, "text": text},
|
||||
)
|
||||
response.raise_for_status()
|
||||
log_info(f"[notify] Сообщение отправлено chat_id={chat_id} attempt={attempt_idx}")
|
||||
return
|
||||
except Exception as exc:
|
||||
last_exc = exc
|
||||
log_warn(
|
||||
f"[notify] Ошибка отправки chat_id={chat_id} attempt={attempt_idx}/{len(delays)}: "
|
||||
f"{type(exc).__name__}"
|
||||
)
|
||||
response.raise_for_status()
|
||||
log_info(f"[notify] Сообщение отправлено chat_id={chat_id}")
|
||||
except Exception as exc:
|
||||
# Не роняем обработку webhook, если Telegram API временно недоступен
|
||||
# или указан неверный токен/чат.
|
||||
log_error("telegram send_message", exc)
|
||||
# Не роняем обработку webhook, если Telegram API временно недоступен
|
||||
# или указан неверный токен/чат.
|
||||
if last_exc is not None:
|
||||
log_error("telegram send_message", last_exc)
|
||||
|
||||
|
||||
class GiteaClient:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user