WhatsApp Message

Event representing a WhatsApp message to be processed by the AI agent

Event Channel:AWS SQS

Overview

The WhatsApp Message event represents a message that needs to be processed by the AI agent service. This event is published to the WhatsApp Message Queue and consumed by the Message Service.

Payload

The payload follows the WhatsAppMessagePayload schema:

WhatsApp Message Payload
{
"conversationId": "string",
"messageId": "string",
"sender": "string",
"content": "string",
"timestamp": "ISO 8601 date-time",
"tenantId": "string",
"channelId": "string"
}

Schema Definition

WhatsAppMessageSchema (Zod)
const WhatsAppMessageSchema = z.object({
conversationId: z.string(),
messageId: z.string(),
sender: z.string(),
content: z.string(),
timestamp: z.string().datetime(),
tenantId: z.string(),
channelId: z.string()
});

Producing the Event

Messages are typically published to the WhatsApp Message Queue by:

  1. Webhook Processor: When processing incoming WhatsApp webhooks
  2. Other Services: When they need to trigger message processing
Publishing to WhatsApp Message Queue
import { SQSClient } from './clients/sqs-client';
const sqsClient = new SQSClient();
await sqsClient.sendMessage({
conversationId: "conv-123",
messageId: "msg-456",
sender: "+5511999999999",
content: "Hello, I'd like to schedule an appointment",
timestamp: new Date().toISOString(),
tenantId: "tenant-789",
channelId: "channel-abc"
}, 'whatsapp_message');

Consuming the Event

The event is consumed by the whatsappMessageProcessor Lambda function:

Processing WhatsApp Message
export const handler = async (event: SQSEvent): Promise<SQSBatchResponse> => {
return sqsBatchProcessor<WhatsAppMessagePayload>({
schema: WhatsAppMessageSchema,
processPayload: async (payload, appContext) => {
const messageService = appContext.get(MessageService);
await messageService.processWhatsAppMessage(payload);
},
})(event);
};

Processing Flow

  1. Message is received from SQS queue
  2. Payload is validated against schema
  3. Message Service loads conversation context
  4. AI agent generates response
  5. Response is sent via WhatsApp API
  6. Follow-up may be scheduled if needed
Event-driven architecture documentation: Impulsell