notifier.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from typing import Dict, Callable, Optional
  2. from core_types import QueryResult, BookResult
  3. from toolkit.cloud_api import CloudApi
  4. from toolkit.task_executor import submit_background
  5. def notify_date_changes(
  6. query_result: QueryResult,
  7. cache: Optional[Dict[str, str]] = None,
  8. logger: Optional[Callable[[str], None]] = None
  9. ) -> bool:
  10. """
  11. Slot availability change WeChat notification.
  12. If a cache dict is provided (e.g. {routing_key: earliest_date}), only pushes and updates cache when earliest date changes.
  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. CloudApi.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. submit_background(_push_to_wx)
  38. return True
  39. def notify_book_result(
  40. book_result: BookResult,
  41. order_id: Optional[str] = None,
  42. logger: Optional[Callable[[str], None]] = None
  43. ) -> bool:
  44. """
  45. Booking success WeChat notification.
  46. Only sends push notification when book_result.success is 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. CloudApi.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. submit_background(_push_to_wx)
  72. return True