feat: Introduce automated Telegram trade alert push and add target date option to /tradealert command.
This commit is contained in:
+28
-4
@@ -242,16 +242,36 @@ def start_bot():
|
|||||||
def tradealert_preview(message):
|
def tradealert_preview(message):
|
||||||
"""Preview the current trade-alert push payload for one city."""
|
"""Preview the current trade-alert push payload for one city."""
|
||||||
try:
|
try:
|
||||||
parts = message.text.split(maxsplit=1)
|
parts = message.text.split()
|
||||||
if len(parts) < 2:
|
if len(parts) < 2:
|
||||||
bot.reply_to(
|
bot.reply_to(
|
||||||
message,
|
message,
|
||||||
"用法: <code>/tradealert ankara</code>",
|
"用法: <code>/tradealert ankara</code> 或 <code>/tradealert ankara 2026-03-06</code>",
|
||||||
|
parse_mode="HTML",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
target_date = None
|
||||||
|
city_tokens = parts[1:]
|
||||||
|
if city_tokens:
|
||||||
|
last_token = city_tokens[-1].strip()
|
||||||
|
try:
|
||||||
|
from datetime import datetime as _dt
|
||||||
|
_dt.strptime(last_token, "%Y-%m-%d")
|
||||||
|
target_date = last_token
|
||||||
|
city_tokens = city_tokens[:-1]
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
city_input = " ".join(city_tokens).strip().lower()
|
||||||
|
if not city_input:
|
||||||
|
bot.reply_to(
|
||||||
|
message,
|
||||||
|
"用法: <code>/tradealert ankara</code> 或 <code>/tradealert ankara 2026-03-06</code>",
|
||||||
parse_mode="HTML",
|
parse_mode="HTML",
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
city_input = parts[1].strip().lower()
|
|
||||||
city_name = ALIASES.get(city_input)
|
city_name = ALIASES.get(city_input)
|
||||||
|
|
||||||
if not city_name and city_input in CITY_REGISTRY:
|
if not city_name and city_input in CITY_REGISTRY:
|
||||||
@@ -278,20 +298,24 @@ def start_bot():
|
|||||||
|
|
||||||
bot.send_message(
|
bot.send_message(
|
||||||
message.chat.id,
|
message.chat.id,
|
||||||
f"📡 正在生成 {city_name.title()} 的异动预警预览...",
|
f"📡 正在生成 {city_name.title()} 的异动预警预览"
|
||||||
|
f"{f' ({target_date})' if target_date else ''}...",
|
||||||
)
|
)
|
||||||
|
|
||||||
payload = build_trade_alert_for_city(
|
payload = build_trade_alert_for_city(
|
||||||
city_name,
|
city_name,
|
||||||
config,
|
config,
|
||||||
force_refresh=True,
|
force_refresh=True,
|
||||||
|
target_date=target_date,
|
||||||
)
|
)
|
||||||
|
|
||||||
preview = ((payload.get("telegram") or {}).get("zh") or "").strip()
|
preview = ((payload.get("telegram") or {}).get("zh") or "").strip()
|
||||||
trigger_count = int(payload.get("trigger_count") or 0)
|
trigger_count = int(payload.get("trigger_count") or 0)
|
||||||
severity = str(payload.get("severity") or "none")
|
severity = str(payload.get("severity") or "none")
|
||||||
|
resolved_date = payload.get("target_date")
|
||||||
header = (
|
header = (
|
||||||
f"[TEST PREVIEW] city={city_name} "
|
f"[TEST PREVIEW] city={city_name} "
|
||||||
|
f"date={resolved_date} "
|
||||||
f"severity={severity} triggers={trigger_count}"
|
f"severity={severity} triggers={trigger_count}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import json
|
|||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
from datetime import datetime
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
@@ -138,13 +139,16 @@ def build_trade_alert_for_city(
|
|||||||
city: str,
|
city: str,
|
||||||
config: Dict[str, Any],
|
config: Dict[str, Any],
|
||||||
force_refresh: bool = False,
|
force_refresh: bool = False,
|
||||||
|
target_date: Optional[str] = None,
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
from web.app import _analyze
|
from web.app import _analyze
|
||||||
from src.analysis.market_alert_engine import build_trading_alerts
|
from src.analysis.market_alert_engine import build_trading_alerts
|
||||||
from src.data_collection.polymarket_client import build_city_market_snapshot
|
from src.data_collection.polymarket_client import build_city_market_snapshot
|
||||||
|
|
||||||
city_weather = _analyze(city, force_refresh=force_refresh)
|
city_weather = _analyze(city, force_refresh=force_refresh)
|
||||||
target_date = city_weather.get("local_date")
|
resolved_target_date = target_date or city_weather.get("local_date")
|
||||||
|
if resolved_target_date:
|
||||||
|
datetime.strptime(resolved_target_date, "%Y-%m-%d")
|
||||||
|
|
||||||
proxy = (
|
proxy = (
|
||||||
(config.get("polymarket", {}) or {}).get("proxy")
|
(config.get("polymarket", {}) or {}).get("proxy")
|
||||||
@@ -152,7 +156,7 @@ def build_trade_alert_for_city(
|
|||||||
)
|
)
|
||||||
market_snapshot = build_city_market_snapshot(
|
market_snapshot = build_city_market_snapshot(
|
||||||
city=city,
|
city=city,
|
||||||
target_date=target_date,
|
target_date=resolved_target_date,
|
||||||
proxy=proxy,
|
proxy=proxy,
|
||||||
force_refresh=force_refresh,
|
force_refresh=force_refresh,
|
||||||
)
|
)
|
||||||
@@ -162,7 +166,7 @@ def build_trade_alert_for_city(
|
|||||||
market_snapshot=market_snapshot,
|
market_snapshot=market_snapshot,
|
||||||
map_url=map_url,
|
map_url=map_url,
|
||||||
)
|
)
|
||||||
alert_payload["target_date"] = target_date
|
alert_payload["target_date"] = resolved_target_date
|
||||||
return alert_payload
|
return alert_payload
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user