Overview
TheConversationService handles all conversation and message management operations. It creates and tracks user conversations, stores messages with metadata, and provides conversation history and statistics.
Class Structure
Namespace:App\Services
Dependencies:
Database- Database connection for persistence
Constructor
Database instance for all conversation operations
Public Methods
getOrCreateConversation
Retrieves an existing conversation or creates a new one for a phone number.User’s WhatsApp phone number (unique identifier)
Display name for the contact
id- Conversation IDphone_number- User’s phonecontact_name- Contact display namestatus- Conversation status (active,pending_human, etc.)ai_enabled- Whether AI responses are enabled (1/0)created_at- Creation timestamplast_message_at- Last activity timestamp
- If conversation exists, updates
last_message_atand returns it - If new, creates with status
activeandai_enabled = 1
addMessage
Stores a new message in the conversation history.ID of the conversation
Either
user or botThe message content
WhatsApp message ID (for deduplication)
RAG context used to generate the response
AI confidence score (0.0-1.0)
Path to audio file if message is audio
Message type:
text, audio, image, etc.getConversationHistory
Retrieves recent messages from a conversation.ID of the conversation
Maximum number of messages to retrieve
created_at DESC (newest first)
getAllConversations
Retrieves all conversations with their last message.Filter by status:
active, pending_human, archived, etc.Maximum conversations to retrieve
last_message_at DESC
updateConversationStatus
Changes the status of a conversation.ID of the conversation
New status:
active, pending_human, archived, etc.getConversationStats
Retrieves aggregate statistics about conversations and messages.Use this method for dashboard statistics and monitoring conversation volume.
Database Schema
conversations Table
| Column | Type | Description |
|---|---|---|
id | INT | Primary key |
phone_number | VARCHAR | WhatsApp phone number |
contact_name | VARCHAR | User’s display name |
status | VARCHAR | active, pending_human, archived |
ai_enabled | TINYINT | Whether AI is enabled (1/0) |
created_at | TIMESTAMP | Creation time |
last_message_at | TIMESTAMP | Last activity |
last_bot_message_at | TIMESTAMP | Last bot response time |
messages Table
| Column | Type | Description |
|---|---|---|
id | INT | Primary key |
conversation_id | INT | Foreign key to conversations |
message_id | VARCHAR | WhatsApp message ID |
sender_type | ENUM | user or bot |
message_text | TEXT | Message content |
audio_url | VARCHAR | Audio file path |
media_type | VARCHAR | text, audio, etc. |
context_used | TEXT | RAG context used |
confidence_score | DECIMAL | AI confidence (0.0-1.0) |
created_at | TIMESTAMP | Message time |
Usage Examples
Complete Conversation Flow
Best Practices
Always use
getOrCreateConversation before adding messages to ensure the conversation exists and is properly tracked.Related Services
- RAG Service - Uses conversation history for context
- WhatsApp Service - Sends messages and triggers conversation creation
- Calendar Flow Handler - References conversation IDs in flow state
Source Code
Location:src/Services/ConversationService.php:1-124