The owner deferred the WhatsApp e2e (add-whatsapp-inbox task 6.3) until the business account can be linked; the task stays open with a note. Specs are synced and the new capabilities have their Purpose written. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
212 lines
11 KiB
Markdown
212 lines
11 KiB
Markdown
# whatsapp-inbox Specification
|
|
|
|
## Purpose
|
|
|
|
A two-way WhatsApp inbox inside Co-Lite on each company's business number. Sales staff follow up leads from the back office, every message is recorded, and any colleague with access can pick up a chat.
|
|
|
|
## Requirements
|
|
|
|
### Requirement: Company WhatsApp settings
|
|
|
|
The system SHALL read each company's WhatsApp number id, business account id, access token and app secret from the environment, and SHALL never store or log them. A company without complete settings SHALL have WhatsApp switched off. Its inbox SHALL say "WhatsApp is not configured", and the rest of the system SHALL keep working.
|
|
|
|
#### Scenario: Company not configured
|
|
|
|
- **GIVEN** CV has no WhatsApp settings
|
|
- **WHEN** a CV admin with `BO_WHATSAPP` opens the inbox
|
|
- **THEN** the page says "WhatsApp is not configured" and offers no composer
|
|
|
|
### Requirement: Webhook handshake
|
|
|
|
The public webhook SHALL answer Meta's subscription check with the given challenge only when the verify token matches the configured one. Otherwise it SHALL answer 403.
|
|
|
|
#### Scenario: Wrong verify token
|
|
|
|
- **WHEN** a subscription check arrives with a verify token that does not match
|
|
- **THEN** the webhook answers 403 and does not echo the challenge
|
|
|
|
### Requirement: Webhook signature
|
|
|
|
The webhook SHALL accept an event only when its `X-Hub-Signature-256` header is a valid HMAC-SHA256 of the raw body under the app secret of the company the event's number belongs to. A missing or invalid signature SHALL get 401, and nothing SHALL be stored.
|
|
|
|
#### Scenario: Forged event
|
|
|
|
- **WHEN** an event for INS's number arrives without a valid signature
|
|
- **THEN** the webhook answers 401 and no message is stored
|
|
|
|
#### Scenario: Signed with the other company's secret
|
|
|
|
- **GIVEN** INS and CV have different app secrets
|
|
- **WHEN** an event for INS's number is signed with CV's app secret
|
|
- **THEN** the event is rejected and no message is stored
|
|
|
|
### Requirement: Inbound messages
|
|
|
|
An accepted inbound message SHALL be stored once, in the conversation of the company that owns the receiving number, for the sender's WhatsApp number. Meta redelivering the same message id SHALL NOT create a second message. An event for a number no company owns SHALL be dropped. An event the inbox cannot take yet (the service is down, or the company of a configured number is not known yet) SHALL be refused, so that Meta redelivers it. Receiving a message SHALL open the 24-hour reply window.
|
|
|
|
#### Scenario: Lead writes to INS
|
|
|
|
- **GIVEN** INS lead Aisyah has mobile `+60123456701`
|
|
- **WHEN** she sends "Hi, about the expo" to INS's number
|
|
- **THEN** an INS conversation for `60123456701` holds the message, linked to Aisyah's lead
|
|
- **AND** no CV user can see it
|
|
|
|
#### Scenario: Redelivery
|
|
|
|
- **WHEN** Meta delivers the same message id twice
|
|
- **THEN** the conversation holds the message once
|
|
|
|
#### Scenario: Inbox not ready
|
|
|
|
- **GIVEN** the WhatsApp service has just started and cannot reach the system service yet
|
|
- **WHEN** Meta delivers a message to INS's number
|
|
- **THEN** the webhook answers 503 and stores nothing
|
|
- **AND** when Meta redelivers after the service has resolved INS, the message is stored once
|
|
|
|
### Requirement: Delivery status
|
|
|
|
The status of a sent message SHALL move only forward, from pending to sent to delivered to read, except that failed SHALL be recorded at any point, together with Meta's error code and title. The thread SHALL show the status as ticks, or as a failure with its reason.
|
|
|
|
#### Scenario: Late delivered after read
|
|
|
|
- **WHEN** a "read" status arrives and then a "delivered" status arrives for the same message
|
|
- **THEN** the message stays "read"
|
|
|
|
#### Scenario: Send failed
|
|
|
|
- **WHEN** Meta rejects a sent message
|
|
- **THEN** the thread shows the message as failed with Meta's error title
|
|
|
|
### Requirement: Automatic ownership
|
|
|
|
A conversation without a manual owner SHALL be owned by the lead's latest salesperson (the salesperson of its most recently captured event lead that has one) when that user is active and holds `BO_WHATSAPP`. Otherwise the current owner SHALL be kept while still active with `BO_WHATSAPP`, and the conversation SHALL be unassigned when not. Ownership SHALL be recomputed when the conversation is created and when an event lead of that lead is captured or changes salesperson.
|
|
|
|
#### Scenario: Chat follows the latest salesperson
|
|
|
|
- **GIVEN** Aisyah's latest event lead has salesperson `sales1`, who holds `BO_WHATSAPP`
|
|
- **WHEN** Aisyah writes for the first time
|
|
- **THEN** the conversation is owned by `sales1`
|
|
|
|
#### Scenario: Latest event has no salesperson
|
|
|
|
- **GIVEN** a conversation owned by `sales1`
|
|
- **WHEN** Aisyah is captured at a new event without a salesperson
|
|
- **THEN** the conversation stays with `sales1`
|
|
|
|
#### Scenario: Salesperson without WhatsApp access
|
|
|
|
- **GIVEN** Aisyah's latest salesperson `sales2` does not hold `BO_WHATSAPP` and the conversation has no owner
|
|
- **WHEN** ownership is recomputed
|
|
- **THEN** the conversation is unassigned
|
|
|
|
#### Scenario: Unknown sender
|
|
|
|
- **WHEN** a number with no lead in INS writes to INS
|
|
- **THEN** an unassigned conversation without a lead link is created
|
|
|
|
### Requirement: Manual assignment
|
|
|
|
A WhatsApp Admin SHALL be able to assign a conversation of their company to any active user of that company who holds `BO_WHATSAPP`, including themselves. A manual owner SHALL NOT change when event leads change. "Return to automatic" SHALL clear the manual owner and recompute it.
|
|
|
|
#### Scenario: Manual owner sticks
|
|
|
|
- **GIVEN** a WhatsApp Admin assigned Aisyah's conversation to `sales3`
|
|
- **WHEN** Aisyah is captured at a new event with salesperson `sales1`
|
|
- **THEN** the conversation stays with `sales3`
|
|
|
|
#### Scenario: Owner outside WhatsApp
|
|
|
|
- **WHEN** a WhatsApp Admin tries to assign a conversation to a user without `BO_WHATSAPP`
|
|
- **THEN** the request is refused and the owner does not change
|
|
|
|
### Requirement: Owner loses access
|
|
|
|
When a user is disabled, or their roles no longer grant `BO_WHATSAPP`, the system SHALL make their conversations unassigned and clear any manual assignment. A user locked out by failed logins SHALL keep their conversations.
|
|
|
|
#### Scenario: Owner disabled
|
|
|
|
- **GIVEN** `sales1` owns three conversations
|
|
- **WHEN** an admin disables `sales1`
|
|
- **THEN** the three conversations are unassigned
|
|
|
|
### Requirement: Inbox access
|
|
|
|
A user with `BO_WHATSAPP` SHALL see, read and reply only in conversations they own. A user with `BO_WHATSAPP_ADMIN` SHALL see every conversation of their company, including unassigned ones, and SHALL be able to read and reply in any of them. A super admin SHALL see the conversations of every company. Every request SHALL be limited to the caller's company, and SHALL fail closed when the caller's company cannot be determined.
|
|
|
|
#### Scenario: Staff opens someone else's chat
|
|
|
|
- **WHEN** `sales1` requests a conversation owned by `sales3`
|
|
- **THEN** the request answers 404
|
|
|
|
#### Scenario: WhatsApp Admin sees the queue
|
|
|
|
- **WHEN** an INS WhatsApp Admin opens the Unassigned tab
|
|
- **THEN** it lists INS's unassigned conversations and none of CV's
|
|
|
|
### Requirement: Replies inside the 24-hour window
|
|
|
|
A user SHALL be able to send a text message only while less than 24 hours have passed since the contact's last inbound message. Outside the window a text send SHALL be refused with "The 24-hour window has closed. Send a template." The composer SHALL show the window's state and offer only templates when the window is closed.
|
|
|
|
#### Scenario: Window closed
|
|
|
|
- **GIVEN** Aisyah last wrote 25 hours ago
|
|
- **WHEN** `sales1` sends a text reply
|
|
- **THEN** it is refused and nothing is sent to Meta
|
|
|
|
### Requirement: Templates
|
|
|
|
A WhatsApp Admin SHALL be able to sync a company's approved templates from that company's business account, and the system SHALL also sync them nightly. Staff SHALL be able to send a synced template with every body parameter filled, inside or outside the window. Templates whose header or buttons need parameters SHALL be listed as not supported and SHALL NOT be sendable.
|
|
|
|
#### Scenario: Missing parameter
|
|
|
|
- **WHEN** a user sends template `expo_followup` with one of its two parameters empty
|
|
- **THEN** it is refused and nothing is sent to Meta
|
|
|
|
### Requirement: Starting a chat from a lead
|
|
|
|
A user with `BO_WHATSAPP` SHALL be able to start a chat from a lead or event lead that has a mobile number, in that lead's company. If the conversation has no owner it SHALL become the starter's as a manual assignment. If another user owns it, the starter SHALL be told who owns it. Until the contact replies, only templates SHALL be offered.
|
|
|
|
#### Scenario: Start an unowned chat
|
|
|
|
- **GIVEN** Aisyah has no conversation yet and no salesperson with WhatsApp
|
|
- **WHEN** `sales1` presses WhatsApp on her event lead
|
|
- **THEN** a conversation owned by `sales1` opens with the template composer
|
|
|
|
### Requirement: Incoming media
|
|
|
|
Incoming images, documents, audio, video, stickers and locations SHALL be shown in the thread. Media SHALL be fetched from Meta only when a user with access opens it. Media that Meta no longer holds SHALL show "Media expired".
|
|
|
|
#### Scenario: Image opened by someone without access
|
|
|
|
- **WHEN** a user who cannot see the conversation requests one of its images
|
|
- **THEN** the request answers 404
|
|
|
|
### Requirement: Unread counts
|
|
|
|
The system SHALL track, per user, the last message they read in each conversation they can see. It SHALL show unread conversations in the list and the number of unread conversations as a badge on the sidebar's inbox item.
|
|
|
|
#### Scenario: Reading clears the badge
|
|
|
|
- **GIVEN** `sales1` has one unread conversation
|
|
- **WHEN** `sales1` opens it
|
|
- **THEN** the sidebar badge clears
|
|
|
|
### Requirement: WhatsApp audit and privacy
|
|
|
|
Sending a message, reassigning a conversation and syncing templates SHALL be recorded in the audit log with the conversation and template names but not the message text. Message text, phone numbers, media, tokens and signatures SHALL NOT be written to application logs.
|
|
|
|
#### Scenario: Template send audited
|
|
|
|
- **WHEN** `sales1` sends template `expo_followup`
|
|
- **THEN** the audit log records the send by `sales1` with the conversation and template name
|
|
- **AND** not the filled-in text
|
|
|
|
### Requirement: Local Meta stand-in
|
|
|
|
The local stack SHALL include a test-only stand-in for Meta's API. It SHALL accept sends, serve templates and media, and post signed inbound events to the webhook on request, so the inbox can be developed and tested without a Meta account. The stand-in SHALL NOT be part of any release.
|
|
|
|
#### Scenario: Simulated inbound message
|
|
|
|
- **WHEN** a developer asks the stand-in to deliver "Hello" from `+60123456701` to INS's number
|
|
- **THEN** the INS inbox shows the message within one polling interval
|