notifier.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from typing import Dict, Callable, Optional
  2. from vs_types import VSQueryResult, VSBookResult
  3. from toolkit.vs_cloud_api import VSCloudApi
  4. from toolkit.thread_pool import ThreadPool
  5. def notify_date_changes(
  6. query_result: VSQueryResult,
  7. cache: Optional[Dict[str, str]] = None,
  8. logger: Optional[Callable[[str], None]] = None
  9. ) -> bool:
  10. """
  11. 档期变化微信通知。
  12. 若提供了 cache 字典(如 {routing_key: earliest_date}),仅在最早档期发生变化时推送并更新 cache。
  13. """
  14. current_earliest_date = query_result.earliest_date
  15. apt_type = query_result.apt_type
  16. if not current_earliest_date or not apt_type:
  17. return False
  18. if cache is not None:
  19. last_date = cache.get(apt_type.routing_key)
  20. if last_date == current_earliest_date:
  21. return False
  22. cache[apt_type.routing_key] = current_earliest_date
  23. def _push_to_wx():
  24. try:
  25. push_content = (
  26. f"📢【档期变化通知】\n"
  27. f"最早日期: {query_result.earliest_date}\n"
  28. f"目标国家: {apt_type.country}\n"
  29. f"递交城市: {apt_type.city}\n"
  30. f"签证类型: {apt_type.visa_type}\n"
  31. f"Routing: {apt_type.routing_key}"
  32. )
  33. VSCloudApi.Instance().push_weixin_text(push_content)
  34. except Exception as e:
  35. if logger:
  36. logger(f"Failed to notify date change to cloud: {e}")
  37. ThreadPool.getInstance().enqueue(_push_to_wx)
  38. return True
  39. def notify_book_result(
  40. book_result: VSBookResult,
  41. order_id: Optional[str] = None,
  42. logger: Optional[Callable[[str], None]] = None
  43. ) -> bool:
  44. """
  45. 预定成功微信通知。
  46. 仅在 book_result.success 为 True 时进行推送。
  47. """
  48. if not book_result.success:
  49. return False
  50. def _push_to_wx():
  51. try:
  52. lines = [
  53. "🎉 【预定成功通知】",
  54. "━━━━━━━━━━━━━━━",
  55. ]
  56. if order_id:
  57. lines.append(f"订单编号: {order_id}")
  58. lines.extend([
  59. f"预约账号: {book_result.account}",
  60. f"预约日期: {book_result.book_date}",
  61. f"预约时间: {book_result.book_time}",
  62. f"预约编号: {book_result.urn}",
  63. f"支付链接: {book_result.payment_link if book_result.payment_link else '无需支付/暂无'}",
  64. "━━━━━━━━━━━━━━━",
  65. ])
  66. push_content = "\n".join(lines)
  67. VSCloudApi.Instance().push_weixin_text(push_content)
  68. except Exception as e:
  69. if logger:
  70. logger(f"Failed to notify book result to cloud: {e}")
  71. ThreadPool.getInstance().enqueue(_push_to_wx)
  72. return True