import type { JsonObject } from './common'; import type { Contact, ContactChatMetadata, ContactName, ContactProfile, ContactTag } from './audience'; import type { ContactFilterEntry } from './contact-filter'; /** * Delivery state, shared by every channel kind. Providers report more than these * -- email bounces, carrier rejections -- and each maps onto one of the five, so * the inbox, the campaign reports and the billing logs all read one field. The * provider's own wording survives in `error`. */ export type MessageStatus = 'received' | 'sent' | 'delivered' | 'read' | 'failed'; /** `received` is the only inbound state, so direction is derived, not stored. */ export type MessageDirection = 'inbound' | 'outbound'; /** Who put the message on the wire, derived from the `sent_by_*` flags. */ export type MessageSource = 'contact' | 'agent' | 'bot' | 'campaign' | 'api' | 'business_app'; /** * The message body, as the channel describes it. * * Open by design -- a WhatsApp interactive reply, an email part tree and an RCS * card share no structure worth forcing into one schema. Only `type` is promised, * and by convention the body for that kind sits under a key of the same name: * `{ type: 'image', image: { id, link } }`. * * Narrow it yourself where you know the channel: `Message`. */ export interface MessageContent extends JsonObject { type: string; } /** Per-status unix seconds. `0` means the message never reached that state. */ export interface MessageReceipts { sent: number; read: number; failed: number; replied: number; received: number; delivered: number; } export interface Message { id: string; contact: string; /** Channel it travelled over. Lives in the channels service, so an id only. */ channel: string; content: C; /** Provider context: the replied-to id, forwarding flags, the CTWA referral. */ context: X; /** The contact's identity on this channel -- phone number, handle, address. */ identifier: string; /** * The provider's id (a `wamid` for WhatsApp), unique across the account. An * outbound message carries its queue id here until the provider answers. */ message_id: string; status: MessageStatus; /** Unix seconds as the provider reported it. Thread order is by this. */ timestamp: number; direction: MessageDirection; source: MessageSource; sent_by_api: boolean; sent_by_biz_app: boolean; sent_by_campaign: boolean; sent_by_bot: boolean; agent: string | null; receipts: MessageReceipts; meta: { bookmarked: boolean; hide: boolean; }; campaign: { id: string | null; cmid: number; }; bot: { id: string | null; node: string; }; error: string; createdAt: string; updatedAt: string; } /** Media kinds whose bytes the platform fetches from the provider and re-hosts. */ export type MediaContentType = 'image' | 'video' | 'audio' | 'document' | 'voice' | 'sticker'; export interface ListMessagesPayload { /** Contact whose thread to read. */ id: string; /** Default 20, max 100. */ limit?: number; /** `id` of the oldest message already held; older ones follow. */ last_message_id?: string; } export interface FetchMessagePayload { /** The provider id, not the document id. */ message_id: string; } export interface SendMessagePayload { channel: string; contact: { id: string; identifier: string; }; content: MessageContent; context?: JsonObject; /** Attribute to another member. Needs `live_chat.send.AS_OTHER`. */ agent?: string; } export interface DeleteMessagePayload { /** Contact whose thread the message belongs to. */ id: string; /** Either the document id or the provider id. */ message_id: string; } export interface RetryDownloadPayload { /** The document id -- a retry targets a row the platform already stored. */ message_id: string; } export interface MessageDeleteResult { success: boolean; id: string; } /** * The inbox's view of a contact: the chat-relevant projection, not the whole * contact. Every chat event answers with this, so one shape updates the list row * and the conversation header together. */ export interface Chat { id: string; name: ContactName; profile: Record; chat_metadata: ContactChatMetadata; /** Unix seconds of the last inbound message. The list is ordered by this. */ last_seen: number; /** Flow state. Open-ended on the stored schema, carried through as-is. */ bot: JsonObject; /** Last-message preview, so a list of chats needs no thread reads. */ last_interaction_props: JsonObject; agent: string | null; team: string | null; tags: ContactTag[]; /** Present on list rows; the chat header renders them. */ custom_field_hash?: JsonObject; } /** * The inbox's own list filter. Every key is independent and omitting one means * "don't care", so an empty filter is every live chat. */ export interface ChatListFilter { /** `all` every chat, `me` the caller's own, `none` the unassigned queue. */ assignment?: 'all' | 'me' | 'none'; /** `unread` has unseen messages or a by-hand flag; `replied` is caught up. */ unread?: 'unread' | 'replied'; new?: 'new' | 'old'; /** `closed` covers closed and marked-done alike; `open` is neither. */ closed?: 'closed' | 'open'; archived?: 'archived' | 'unarchived'; /** `active_no_bot` is running with no flow attached. */ automation?: 'active' | 'paused' | 'active_no_bot'; block?: 'ic' | 'og' | 'api' | 'bot' | 'unblocked'; /** `active` means seen inside the last 24 hours -- the provider reply window. */ session?: 'active' | 'expired'; } export interface ListChatsPayload { filter?: ChatListFilter; /** Conditions in the same DSL the contact list and segments use. */ conditions?: ContactFilterEntry[]; search?: string; /** `last_seen` of the oldest row already held; omit for the first page. */ nextPage?: number; /** Default 20, max 100. */ limit?: number; department?: string; team_member?: string; } export interface ChatList { contacts: Chat[]; /** * The filter after permission narrowing. A team member without the unassigned * permission asking for `all` gets `me` back, so the UI can show what it got. */ filter: ChatListFilter; /** Send as `nextPage` for the following page; `null` at the end. */ nextPage: number | null; } /** Every action `chat.action` accepts. */ export type ChatActionName = 'delete' | 'transfer' | 'bulk_transfer' | 'archive' | 'unarchive' | 'block' | 'mark_as_read' | 'mark_as_unread' | 'pause_automation' | 'resume_automation' | 'close' | 'done' | 'pause_timer' | 'update_tags'; /** The blocked flags; send only the ones to change. */ export interface ChatBlockUpdate { incoming?: boolean; outgoing?: boolean; bot?: boolean; api?: boolean; } /** `chat` names the target. `id` is accepted as an alias on every action. */ interface ChatTarget { chat?: string; id?: string; } export type ChatActionPayload = (ChatTarget & { action: 'delete'; }) | (ChatTarget & { action: 'transfer'; member: string; }) | (ChatTarget & { action: 'archive'; }) | (ChatTarget & { action: 'unarchive'; }) | (ChatTarget & { action: 'block'; update: ChatBlockUpdate; }) | (ChatTarget & { action: 'mark_as_read'; }) | (ChatTarget & { action: 'mark_as_unread'; }) | (ChatTarget & { action: 'pause_automation'; }) | (ChatTarget & { action: 'resume_automation'; }) | (ChatTarget & { action: 'close'; }) | (ChatTarget & { action: 'done'; isEnded?: boolean; }) | (ChatTarget & { action: 'pause_timer'; duration: number; }) | (ChatTarget & { action: 'update_tags'; tags: string[]; }) | { action: 'bulk_transfer'; /** Member to take chats from, or `UNASSIGN` for the unassigned queue. */ source: string | 'UNASSIGN'; /** Member or team to hand them to, or `UNASSIGN` to clear it. */ target: string | 'UNASSIGN'; target_type?: 'member' | 'department'; }; /** The envelope every chat action answers and broadcasts with. */ export interface ChatActionResult { action: ChatActionName; id: string; /** `null` for `delete`: the row has left the list, so there is nothing to draw. */ chat?: Chat | null; } export interface BulkTransferResult { action: 'bulk_transfer'; count: number; chats: ChatActionResult[]; } export interface UpdateChatPayload { id: string; action: 'done' | 'archive'; } /** `contact.update` answers with the metadata alone. */ export interface UpdateChatResult { id: string; action: 'done' | 'archive'; chat_metadata?: ContactChatMetadata; } export interface AssignBotPayload { chat: string; channel: string; bot: string; node?: string; /** * `assign` attaches the flow and resumes automation. `trigger` hands the chat * to the bot server, which decides where in the flow it belongs. */ action?: 'assign' | 'trigger'; } /** A click-to-WhatsApp referral: the ad payload behind a conversation. */ /** * A delivery state moved on a message already on screen. * * Its own push rather than another `message.received`, because a client updates * the ticks in place and does not want to re-render the bubble. The row is the * stored document as the sender service wrote it, so `_id` is a string and * `receipts` carries the unix second each state was reached. */ export interface MessageStatusUpdate extends JsonObject { /** The provider's message id -- what an echoed bubble's queue id is replaced by. */ message_id: string; status: MessageStatus; /** Owner the row belongs to. */ user: string; contact?: string; receipts?: Partial; /** Provider text when `status` is `failed`. */ error?: string; } /** Media kinds a chat message can carry. */ export type MediaKind = 'image' | 'video' | 'audio' | 'voice' | 'sticker' | 'document'; /** * Per-kind ceilings, in bytes. WhatsApp's limits, enforced when the upload is * requested rather than when the message is sent. */ export declare const MEDIA_LIMITS: Record; /** What `POST /v1/audience/inbox/media/upload-url` takes. Metadata, not bytes. */ export interface CreateMediaUploadPayload { kind: MediaKind; /** Only its extension and display name survive; the stored key is a uuid. */ filename: string; content_type: string; /** Exact byte length. Signed into the ticket, so the upload must match. */ size: number; } /** A presigned upload. `PUT` the bytes to `upload_url` with exactly `headers`. */ export interface MediaUpload { upload_url: string; /** Every header the signature covers. Anything else fails the upload. */ headers: Record; /** * Where the object is readable once the upload succeeds. Permanent -- only * `upload_url` expires, and only if unused. */ link: string; key: string; kind: MediaKind; filename: string; content_type: string; size: number; /** Unix seconds after which `upload_url` stops working. */ expires_at: number; expires_in: number; } /** * The bytes `sendMedia` can take. * * A browser `File` or `Blob`, or a description of bytes for Node, where neither * exists. Structurally the same thing `crm.inboxMedia.upload()` accepts, declared * here so the realtime entrypoint does not have to import the REST one. */ export type MediaFile = Blob | { body: unknown; filename: string; contentType: string; size: number; }; /** * Uploads a file and answers with its permanent link. * * Satisfied by `crm.inboxMedia.upload`. Declared as a function so the socket * client takes a capability rather than a dependency on the REST client: an app * that only ever sends a `link` pulls none of it in. */ export type MediaUploader = (file: MediaFile, options: { kind: MediaKind; filename?: string; }) => Promise; /** * What `sendMedia` takes: a chat, a kind, and either a link or a file. * * `link` and `file` are alternatives -- a link is sent as it is, a file is * uploaded first and needs the `Inbox` to have been given an uploader. */ export interface SendMediaPayload { channel: string; contact: { id: string; identifier: string; }; kind: MediaKind; /** Already uploaded. Takes precedence over `file`. */ link?: string; /** Uploaded before sending. Requires `uploadMedia` on the Inbox. */ file?: MediaFile; /** Shown under the media. Ignored for a sticker, which takes none. */ caption?: string; /** What the recipient sees for a document. Also names the uploaded object. */ filename?: string; context?: JsonObject; /** Attribute to another member. Needs `live_chat.send.AS_OTHER`. */ agent?: string; } export interface CtwaReferral { id: string; contact: string; channel: string | null; /** The provider's referral object. `payload.source_id` is the ad id. */ payload: JsonObject; createdAt: string; updatedAt: string; } /** * When a contact was last seen on one channel. Providers only allow unprompted * replies inside 24 hours of the last inbound message, and a contact reachable on * several channels has one such window per channel. */ export interface ChatSession { id: string; channel: string; last_seen: number; } /** Re-exported so a consumer importing `Chat` need not also reach for `Contact`. */ export type { Contact };