event_hooks.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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)
  36. def on_message_received(order_id: int, user_id: int, message: str):
  37. """
  38. Hook triggered when a client sends a message to an order chat.
  39. Useful for alerting admins via external systems.
  40. """
  41. print(f"EVENT: New message for Order {order_id} from User {user_id}: {message[:20]}...")
  42. # TODO: Integration logic (Telegram, Slack, Email to shop owner, etc.)
  43. pass