From 87bf7c357508deec5b9933ec2baa022b42074d9b Mon Sep 17 00:00:00 2001 From: "2569718930@qq.com" <2569718930@qq.com> Date: Thu, 11 Jun 2026 15:58:13 +0800 Subject: [PATCH] fallback missing Telegram city topics --- src/utils/telegram_push.py | 20 ++++++++++++++++ tests/test_telegram_hashtags.py | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/utils/telegram_push.py b/src/utils/telegram_push.py index 5b987cf7..9a6c87d2 100644 --- a/src/utils/telegram_push.py +++ b/src/utils/telegram_push.py @@ -1733,6 +1733,7 @@ def _process_airport_city( # Send to all target chats sent = False for chat_id in chat_ids: + thread_id = 0 try: kwargs = {} thread_id = _resolve_thread_id(chat_id, city) @@ -1741,6 +1742,25 @@ def _process_airport_city( _rate_limited_send(bot, chat_id, message, **kwargs) sent = True except Exception as exc: + if thread_id and "message thread not found" in str(exc).lower(): + logger.warning( + "airport push thread missing; retrying main chat city={} chat_id={} thread_id={}", + city, + chat_id, + thread_id, + ) + try: + _rate_limited_send(bot, chat_id, message) + sent = True + continue + except Exception as fallback_exc: + logger.warning( + "airport push main chat fallback failed city={} chat_id={}: {}", + city, + chat_id, + fallback_exc, + ) + continue logger.warning("airport push failed city={} chat_id={}: {}", city, chat_id, exc) if sent: diff --git a/tests/test_telegram_hashtags.py b/tests/test_telegram_hashtags.py index e2cc456e..bcbadcc7 100644 --- a/tests/test_telegram_hashtags.py +++ b/tests/test_telegram_hashtags.py @@ -405,6 +405,48 @@ def test_airport_push_rejects_observation_older_than_last_push(monkeypatch): assert result is None +def test_airport_push_retries_main_chat_when_forum_thread_is_missing(monkeypatch): + import src.utils.telegram_push as telegram_push + + calls = [] + + monkeypatch.setattr( + telegram_push, + "_load_airport_city_weather_for_push", + lambda _city: { + "local_time": "15:22", + "current": {"temp": 30.0}, + "deb": {"prediction": 31.0}, + "airport_primary": { + "temp": 30.0, + "obs_time": "2026-06-11T07:52:00+00:00", + }, + }, + ) + monkeypatch.setattr(telegram_push, "_resolve_thread_id", lambda _chat, _city: 99) + + def fake_send(_bot, chat_id, _message, **kwargs): + calls.append((chat_id, kwargs)) + if kwargs.get("message_thread_id"): + raise RuntimeError("Bad Request: message thread not found") + + monkeypatch.setattr(telegram_push, "_rate_limited_send", fake_send) + + result = telegram_push._process_airport_city( + "shenzhen", + now_ts=1781164400, + last_city={}, + chat_ids=["chat-1"], + bot=object(), + ) + + assert result is not None + assert calls == [ + ("chat-1", {"message_thread_id": 99}), + ("chat-1", {}), + ] + + def test_high_freq_airport_push_workers_default_to_one_for_shared_cpu(monkeypatch): source = Path("src/utils/telegram_push.py").read_text(encoding="utf-8") assert 'TELEGRAM_AIRPORT_PUSH_MAX_WORKERS", 1' in source