event_hooks.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import json
  2. import db
  3. import config
  4. def on_order_created(order_id: int):
  5. """
  6. Hook triggered asynchronously when a new order is placed.
  7. Users can add any notification logic here (e.g. email or telegram).
  8. """
  9. print(f"EVENT: Order {order_id} created.")
  10. # Fetch order data if needed
  11. order = db.execute_query("SELECT * FROM orders WHERE id = %s", (order_id,))
  12. if order:
  13. order_data = order[0]
  14. # TODO: Add your notification logic here
  15. pass
  16. def on_order_status_changed(order_id: int, status: str, order_data: dict, send_notification: bool):
  17. """
  18. Hook triggered asynchronously when the admin changes the order status.
  19. Uses the send_notification flag explicitly.
  20. """
  21. print(f"EVENT: Order {order_id} status changed to {status}. Notify user: {send_notification}")
  22. if send_notification:
  23. # TODO: Add your notification logic here (Email, Telegram, SMS, etc.)
  24. # The order_data dictionary contains all the details of the order.
  25. user_email = order_data.get('email')
  26. first_name = order_data.get('first_name')
  27. user_id = order_data.get('user_id')
  28. lang = "en"
  29. if user_id:
  30. user_info = db.execute_query("SELECT preferred_language FROM users WHERE id = %s", (user_id,))
  31. if user_info and user_info[0].get('preferred_language'):
  32. lang = user_info[0]['preferred_language']
  33. print(f"--> Preparing notification to {user_email} (User: {first_name}, Lang: {lang})...")
  34. import notifications
  35. notifications.notify_status_change(user_email, order_id, status, first_name, lang)