Add whatsapp feature
This commit is contained in:
@@ -11,6 +11,7 @@ from datetime import datetime
|
||||
|
||||
from shared.messaging.rabbitmq import RabbitMQClient
|
||||
from app.services.email_service import EmailService
|
||||
from app.services.whatsapp_service import WhatsAppService
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
@@ -18,10 +19,12 @@ logger = structlog.get_logger()
|
||||
class POEventConsumer:
|
||||
"""
|
||||
Consumes purchase order events from RabbitMQ and sends notifications
|
||||
Sends both email and WhatsApp notifications to suppliers
|
||||
"""
|
||||
|
||||
def __init__(self, email_service: EmailService):
|
||||
def __init__(self, email_service: EmailService, whatsapp_service: WhatsAppService = None):
|
||||
self.email_service = email_service
|
||||
self.whatsapp_service = whatsapp_service
|
||||
|
||||
# Setup Jinja2 template environment
|
||||
template_dir = Path(__file__).parent.parent / 'templates'
|
||||
@@ -50,17 +53,24 @@ class POEventConsumer:
|
||||
)
|
||||
|
||||
# Send notification email
|
||||
success = await self.send_po_approved_email(event_data)
|
||||
email_success = await self.send_po_approved_email(event_data)
|
||||
|
||||
if success:
|
||||
# Send WhatsApp notification if service is available
|
||||
whatsapp_success = False
|
||||
if self.whatsapp_service:
|
||||
whatsapp_success = await self.send_po_approved_whatsapp(event_data)
|
||||
|
||||
if email_success:
|
||||
logger.info(
|
||||
"PO approved email sent successfully",
|
||||
po_id=event_data.get('data', {}).get('po_id')
|
||||
po_id=event_data.get('data', {}).get('po_id'),
|
||||
whatsapp_sent=whatsapp_success
|
||||
)
|
||||
else:
|
||||
logger.error(
|
||||
"Failed to send PO approved email",
|
||||
po_id=event_data.get('data', {}).get('po_id')
|
||||
po_id=event_data.get('data', {}).get('po_id'),
|
||||
whatsapp_sent=whatsapp_success
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
@@ -276,3 +286,76 @@ This is an automated email from your Bakery Management System.
|
||||
return dt.strftime('%B %d, %Y')
|
||||
except Exception:
|
||||
return iso_date
|
||||
|
||||
async def send_po_approved_whatsapp(self, event_data: Dict[str, Any]) -> bool:
|
||||
"""
|
||||
Send PO approved WhatsApp notification to supplier
|
||||
|
||||
This sends a WhatsApp Business template message notifying the supplier
|
||||
of a new purchase order. The template must be pre-approved in Meta Business Suite.
|
||||
|
||||
Args:
|
||||
event_data: Full event payload from RabbitMQ
|
||||
|
||||
Returns:
|
||||
bool: True if WhatsApp message sent successfully
|
||||
"""
|
||||
try:
|
||||
# Extract data from event
|
||||
data = event_data.get('data', {})
|
||||
|
||||
# Check for supplier phone number
|
||||
supplier_phone = data.get('supplier_phone')
|
||||
if not supplier_phone:
|
||||
logger.debug(
|
||||
"No supplier phone in event, skipping WhatsApp notification",
|
||||
po_id=data.get('po_id')
|
||||
)
|
||||
return False
|
||||
|
||||
# Extract tenant ID for tracking
|
||||
tenant_id = data.get('tenant_id')
|
||||
|
||||
# Prepare template parameters
|
||||
# Template: "Hola {{1}}, has recibido una nueva orden de compra {{2}} por un total de {{3}}."
|
||||
# Parameters: supplier_name, po_number, total_amount
|
||||
template_params = [
|
||||
data.get('supplier_name', 'Estimado proveedor'),
|
||||
data.get('po_number', 'N/A'),
|
||||
f"€{data.get('total_amount', 0):.2f}"
|
||||
]
|
||||
|
||||
# Send WhatsApp template message
|
||||
# The template must be named 'po_notification' and approved in Meta Business Suite
|
||||
success = await self.whatsapp_service.send_message(
|
||||
to_phone=supplier_phone,
|
||||
message="", # Not used for template messages
|
||||
template_name="po_notification", # Must match template name in Meta
|
||||
template_params=template_params,
|
||||
tenant_id=tenant_id
|
||||
)
|
||||
|
||||
if success:
|
||||
logger.info(
|
||||
"PO approved WhatsApp sent successfully",
|
||||
po_id=data.get('po_id'),
|
||||
supplier_phone=supplier_phone,
|
||||
template="po_notification"
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
"Failed to send PO approved WhatsApp",
|
||||
po_id=data.get('po_id'),
|
||||
supplier_phone=supplier_phone
|
||||
)
|
||||
|
||||
return success
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error sending PO approved WhatsApp",
|
||||
error=str(e),
|
||||
po_id=data.get('po_id'),
|
||||
exc_info=True
|
||||
)
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user