Make markets command generate digest asynchronously
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
|
||||
@@ -256,14 +257,28 @@ class BasicCommandHandler:
|
||||
)
|
||||
trace.set_status("blocked", f"unsupported_chat_type:{chat_type}")
|
||||
return
|
||||
from src.utils.telegram_push import build_market_monitor_digest
|
||||
self.bot.reply_to(message, "⏳ 正在生成当前市场概览,请稍候...")
|
||||
|
||||
summary = build_market_monitor_digest(
|
||||
self.config,
|
||||
slot_label="当前市场概览",
|
||||
force_refresh=False,
|
||||
)
|
||||
self.bot.reply_to(message, summary)
|
||||
trace.set_status("ok")
|
||||
chat_id = getattr(getattr(message, "chat", None), "id", None)
|
||||
|
||||
def _worker() -> None:
|
||||
try:
|
||||
from src.utils.telegram_push import build_market_monitor_digest
|
||||
|
||||
summary = build_market_monitor_digest(
|
||||
self.config,
|
||||
slot_label="当前市场概览",
|
||||
force_refresh=False,
|
||||
)
|
||||
self.bot.send_message(chat_id, summary)
|
||||
except Exception:
|
||||
self.bot.send_message(chat_id, "❌ 当前市场概览生成失败,请稍后重试。")
|
||||
|
||||
threading.Thread(
|
||||
target=_worker,
|
||||
name="telegram-markets-manual-query",
|
||||
daemon=True,
|
||||
).start()
|
||||
trace.set_status("accepted")
|
||||
finally:
|
||||
trace.emit()
|
||||
|
||||
@@ -7,12 +7,13 @@ from src.bot.runtime_coordinator import RuntimeStatus
|
||||
class DummyBot:
|
||||
def __init__(self):
|
||||
self.replies = []
|
||||
self.sent_messages = []
|
||||
|
||||
def reply_to(self, message, text, parse_mode=None):
|
||||
self.replies.append({"text": text, "parse_mode": parse_mode, "chat_id": message.chat.id})
|
||||
|
||||
def send_message(self, chat_id, text, parse_mode=None): # pragma: no cover
|
||||
pass
|
||||
self.sent_messages.append({"chat_id": chat_id, "text": text, "parse_mode": parse_mode})
|
||||
|
||||
def message_handler(self, *args, **kwargs): # pragma: no cover - decorator stub
|
||||
def _decorator(func):
|
||||
@@ -78,11 +79,17 @@ def test_basic_handler_markets_returns_summary(monkeypatch):
|
||||
"src.utils.telegram_push.build_market_monitor_digest",
|
||||
lambda config, slot_label="当前概览", top_n=None, force_refresh=False: "MARKET DIGEST",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"src.bot.handlers.basic.threading.Thread",
|
||||
lambda target, name=None, daemon=None: SimpleNamespace(start=target),
|
||||
)
|
||||
|
||||
handler.handle_markets(_message("/markets"))
|
||||
|
||||
assert len(bot.replies) == 1
|
||||
assert bot.replies[0]["text"] == "MARKET DIGEST"
|
||||
assert "正在生成当前市场概览" in bot.replies[0]["text"]
|
||||
assert len(bot.sent_messages) == 1
|
||||
assert bot.sent_messages[0]["text"] == "MARKET DIGEST"
|
||||
|
||||
|
||||
def test_basic_handler_markets_rejects_channel_chat():
|
||||
|
||||
Reference in New Issue
Block a user