| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- from typing import Dict, Callable, Optional
- from core_types import QueryResult, BookResult
- from toolkit.cloud_api import CloudApi
- from toolkit.thread_pool import ThreadPool
- def notify_date_changes(
- query_result: QueryResult,
- cache: Optional[Dict[str, str]] = None,
- logger: Optional[Callable[[str], None]] = None
- ) -> bool:
- """
- 档期变化微信通知。
- 若提供了 cache 字典(如 {routing_key: earliest_date}),仅在最早档期发生变化时推送并更新 cache。
- """
- current_earliest_date = query_result.earliest_date
- apt_type = query_result.apt_type
- if not current_earliest_date or not apt_type:
- return False
- if cache is not None:
- last_date = cache.get(apt_type.routing_key)
- if last_date == current_earliest_date:
- return False
- cache[apt_type.routing_key] = current_earliest_date
- def _push_to_wx():
- try:
- push_content = (
- f"📢【档期变化通知】\n"
- f"最早日期: {query_result.earliest_date}\n"
- f"目标国家: {apt_type.country}\n"
- f"递交城市: {apt_type.city}\n"
- f"签证类型: {apt_type.visa_type}\n"
- f"Routing: {apt_type.routing_key}"
- )
- CloudApi.Instance().push_weixin_text(push_content)
- except Exception as e:
- if logger:
- logger(f"Failed to notify date change to cloud: {e}")
- ThreadPool.getInstance().enqueue(_push_to_wx)
- return True
- def notify_book_result(
- book_result: BookResult,
- order_id: Optional[str] = None,
- logger: Optional[Callable[[str], None]] = None
- ) -> bool:
- """
- 预定成功微信通知。
- 仅在 book_result.success 为 True 时进行推送。
- """
- if not book_result.success:
- return False
- def _push_to_wx():
- try:
- lines = [
- "🎉 【预定成功通知】",
- "━━━━━━━━━━━━━━━",
- ]
- if order_id:
- lines.append(f"订单编号: {order_id}")
- lines.extend([
- f"预约账号: {book_result.account}",
- f"预约日期: {book_result.book_date}",
- f"预约时间: {book_result.book_time}",
- f"预约编号: {book_result.urn}",
- f"支付链接: {book_result.payment_link if book_result.payment_link else '无需支付/暂无'}",
- "━━━━━━━━━━━━━━━",
- ])
- push_content = "\n".join(lines)
- CloudApi.Instance().push_weixin_text(push_content)
- except Exception as e:
- if logger:
- logger(f"Failed to notify book result to cloud: {e}")
- ThreadPool.getInstance().enqueue(_push_to_wx)
- return True
|