Complete guide to AI Munshi — a production WhatsApp legal agent built in PHP with persona routing, STT, TTS, and intent classification.
HMAC-SHA256 signature verification, the 20-second response requirement, deduplication by wa_msg_id, and webhook verification challenge handling.
Resolving a phone number to user+role, persona classes (Lawyer/Client/Staff/Unknown) with role-aware system prompt context, and fallback for unregistered caller…
Two-layer classification: Layer 1 keyword matching for common intents, Layer 2 LLM classification for ambiguous messages, confidence thresholds, and routing to …
Downloading voice notes from Meta's media endpoint, local Whisper transcription via HTTP microservice, language hint injection, and graceful failure handling.
The WAV-not-MP3 trap, the UTF-8 /u flag corruption bug in prepareText(), audio type classification, and keeping the model warm with a health-check cron.
TTS → WAV → OGG/OPUS via FFmpeg → Meta upload → send media_id → monitor delivery status. The silent failure trap: API returns 200 but delivery may still fail.
Per-user session schema, storing last N turns, context injection into LLM prompts, session expiry, and multi-device handling.
Script-based language detection (Devanagari/Gurmukhi Unicode blocks), storing preferred_lang per contact, language-specific error messages in all three language…
Storing wa_message_id + transcript on created workspace items, WebhookContext globals for cross-cutting request state, and the media URL expiry trap.
Client asks the WhatsApp AI agent → bot forwards to lawyer → lawyer replies via WhatsApp → bot forwards to client. Relay detection, forwarding, expiry, and the …