Designing a unified inbox across four messaging providers
Every provider thinks it invented messaging. WhatsApp gives you phone-number identities and 24-hour session windows; Instagram hides the username behind a scoped ID; Telegram happily lets bots edit messages; Messenger fires webhooks for things you never asked for.
The conversation model
I normalised everything into three tables: contacts (one human, many channel identities), conversations (a thread bound to a channel identity) and messages (append-only, provider-agnostic).
messages
id uuid
conversation uuid
direction 'in' | 'out'
provider enum
provider_ref text -- external id, unique per provider
body jsonb -- normalised blocks
sent_at timestamptz
state 'queued' | 'sent' | 'delivered' | 'read' | 'failed'
Idempotency is the whole game
Webhooks retry. Users double-send. A unique index on (provider, provider_ref) plus an upsert means replays are free, and the realtime layer never has to reason about duplicates.
Where AI belongs
Not in the write path. AI triage subscribes to new inbound messages, writes a suggestion row, and a human (or an automation rule) decides. That keeps a bad model response from ever reaching a customer unflagged.
The moment a product claims “one inbox”, the hard part is not the UI. It is agreeing on what a message is.