Skip to content
SegmentStream/ Docs

Loading documentation…

↑ ↓ to navigate  ↵ to open  Esc to close
Website Console
Section menu

@segmentstream/ontology/whatsapp

29 public exports from @segmentstream/ontology/whatsapp.

Public exports

ChannelAnswerError

The provider's answer to a call that did not prove its message sent, kept whole for operators: its status and its body, such as Telegram's `description` or Meta's error. Whoever logs it redacts it.

class ChannelAnswerError extends Error {
    readonly status: number;
    readonly name = "ChannelAnswerError";
    readonly answer: unknown;
    constructor(message: string, status: number, reply: Pick<ChannelReply, "body">);
}
ChannelCall

One HTTP request through a connection: one of its APIs, a path relative to its base URL.

interface ChannelCall {
    readonly api: string;
    readonly method: "GET" | "POST" | "PUT";
    readonly path: string;
    readonly headers: ReadonlyArray<readonly [
        string,
        string
    ]>;
    readonly body: Uint8Array;
    readonly file?: ChannelCallFile;
}
ChannelDeliverFile

A file the ontology hands an agent's channel to deliver with a message: by a short-lived link to the workspace's file storage, granted just before the message is handed over, which the channel reads it by. `size` is the revision's the ontology checked: bytes of another size are not it.

interface ChannelDeliverFile {
    readonly filename: string;
    readonly content_type: string;
    readonly size: number;
    readonly url: string;
}
ChannelDeliveredFile

What became of one file of a delivery, by its index among the files the request named.

interface ChannelDeliveredFile {
    readonly index: number;
    readonly status: "sent" | "uncertain" | "failed";
    readonly message_id: string | null;
}
ChannelPart

What became of one visible message, or one step before any, a sender attempted: - `sent`: the provider's answer proves it; - `failed`: nothing of it reached the person; - `uncertain`: it may have.

interface ChannelPart {
    readonly files: ReadonlyArray<number>;
    readonly text: boolean;
    readonly status: "sent" | "failed" | "uncertain";
    readonly messageIds: ReadonlyArray<string>;
    readonly code?: string;
    readonly cause?: unknown;
}
ChannelReply

The provider's answer, as the connection relayed it.

interface ChannelReply {
    readonly status: number;
    readonly headers: ReadonlyArray<readonly [
        string,
        string
    ]>;
    readonly body: Uint8Array;
}
ChannelRequest

Sends one call. It rejects when the connection could not relay it; whether that proves the provider never saw it is for the caller's `definite` to say.

type ChannelRequest = (call: ChannelCall) => Promise<ChannelReply>;
DefiniteFailure

Whether a request the connection did not relay proves the provider never saw it.

type DefiniteFailure = (error: unknown) => boolean;
WHATSAPP_CAPTION_LIMIT

The most characters a WhatsApp caption has.

const WHATSAPP_CAPTION_LIMIT = 1024
WHATSAPP_DELIVER_FILES

The most files one message the ontology sends carries, each a WhatsApp message of its own.

const WHATSAPP_DELIVER_FILES = 10
WHATSAPP_FILE_BYTES

The most bytes WhatsApp fetches of a file of each type.

const WHATSAPP_FILE_BYTES: Readonly<Record<WhatsAppFileType, number>>
WHATSAPP_MESSAGE_LIMIT

The most characters WhatsApp accepts in a text message's `body`.

const WHATSAPP_MESSAGE_LIMIT = 4096
WHATSAPP_REFUSALS

Meta's refusals that code acts on: 131047, the person last wrote more than 24 hours ago, so only a template reaches them; 131026, the message cannot be delivered to the number; 131050, the person stopped marketing messages. Meta answers them when it refuses a message, or reports them later in a `failed` delivery status of a message it accepted.

const REFUSALS: Readonly<Record<number, Exclude<ChatRefusal, "buttons_unsupported">>>
WhatsAppDeliver

One message for the agent's WhatsApp channel to send (`POST` {@link whatsappDeliverRoute}).

interface WhatsAppDeliver {
    readonly operation_id: string;
    readonly connection: string;
    readonly to: string;
    readonly message?: WhatsAppDeliverMessage;
    readonly caption?: string;
    readonly files?: ReadonlyArray<ChannelDeliverFile>;
}
WhatsAppDeliverMessage

One message as WhatsApp's Cloud API takes it, `type` naming the field that holds it: a text (`{ body, preview_url }`), a text with its buttons (`interactive`: reply buttons or one link), or an approved template with its values (`{ name, language: { code }, components? }`).

type WhatsAppDeliverMessage = {
    readonly type: "text";
    readonly text: {
        readonly body: string;
        readonly preview_url: boolean;
    };
} | {
    readonly type: "interactive";
    readonly interactive: Readonly<Record<string, unknown>>;
} | {
    readonly type: "template";
    readonly template: Readonly<Record<string, unknown>>;
};
WhatsAppDeliverRefused

Why the agent sent nothing (`4xx`): `channel_unknown` when it has no WhatsApp channel of the name on the connection, so the ontology sends the message itself; `provider_refused` when Meta refused it; `connection_refused` when SegmentStream's connection refused the call; or the request's own fault, such as `invalid_request` or `file_too_large`.

interface WhatsAppDeliverRefused {
    readonly error: string;
    readonly refusal?: WhatsAppRefusal;
    readonly reference?: string;
}
WhatsAppDelivered

The agent's answer once it sent something: `sent` when Meta named every message, `uncertain` when some may not have reached the person, with what became of each file. A `4xx` with {@link WhatsAppDeliverRefused} sent nothing (`404` alone: an agent without the route); anything else may have sent it.

interface WhatsAppDelivered {
    readonly status: "sent" | "uncertain";
    readonly message_id: string | null;
    readonly files?: ReadonlyArray<ChannelDeliveredFile>;
    readonly reference?: string;
}
WhatsAppFileType

Meta's message types a file is sent as.

type WhatsAppFileType = "image" | "audio" | "video" | "document";
WhatsAppLinkedFile

A file WhatsApp fetches by its link. A link valid only briefly is better resolved just before its message: a function is called then, and when it throws, that file's message is not sent and its part fails with `unavailable`.

interface WhatsAppLinkedFile {
    readonly link: string | (() => Promise<string>);
    readonly filename: string;
    readonly contentType: string;
    readonly index?: number;
}
WhatsAppMessageType

Meta's message types: a file's, a text, a text with buttons, or an approved template.

type WhatsAppMessageType = WhatsAppFileType | "text" | "interactive" | "template";
WhatsAppRefusal

Meta's refusals of a message that code acts on, as `WHATSAPP_REFUSALS` names them by code.

type WhatsAppRefusal = "outside_reply_window" | "recipient_unreachable" | "recipient_opted_out";
WhatsAppSentPart

A message WhatsApp accepted, with Meta's type of it and its text or caption.

interface WhatsAppSentPart extends ChannelPart {
    readonly type: WhatsAppMessageType;
    readonly refusal?: WhatsAppRefusal;
}
literalWhatsAppText

Text that WhatsApp shows as it is: every formatting marker replaced by its look-alike.

function literalWhatsAppText(text: string): string;
renderWhatsApp

The member's text as written, then the separator and the attribution as literal text, the attribution in italics. Neither can open or close the member's formatting.

function renderWhatsApp(text: string, attribution: string, separator?: string): string;
sendWhatsAppFiles

Sends one message per file to `recipient`, `{ to }` by phone number or `{ recipient }` by business-scoped user id, `caption` with the first; `replyTo` answers a message with the first. A caption sent on its own is never put on a file, whatever became of it. Each message is a part: `sent` when Meta names it, `failed` when Meta refused it with an error code, otherwise `uncertain`. Nothing is retried, and a failed message does not stop the next.

function sendWhatsAppFiles(options: {
    readonly request: ChannelRequest;
    readonly recipient: Readonly<Record<string, string>>;
    readonly files: ReadonlyArray<WhatsAppLinkedFile>;
    readonly caption?: string;
    readonly replyTo?: string;
    readonly definite?: DefiniteFailure;
}): Promise<ReadonlyArray<WhatsAppSentPart>>;
sendWhatsAppMessage

One message to `recipient`, `{ to }` by phone number or `{ recipient }` by business-scoped user id: Meta's `type` and the object of that type, such as `text` and `{ body, preview_url }`, in one `POST /phone/messages` on the connection's `graph` API, never retried; `replyTo` answers a message. It is `sent` when Meta names it; `failed` when Meta refused it with an error code, or when the connection refused the call before relaying it, as `definite` says; and `uncertain` otherwise, as on a 5xx, a lost answer or a success without the message's id.

function sendWhatsAppMessage(options: {
    readonly request: ChannelRequest;
    readonly recipient: Readonly<Record<string, string>>;
    readonly type: WhatsAppMessageType;
    readonly message: Readonly<Record<string, unknown>>;
    readonly replyTo?: string;
    readonly definite?: DefiniteFailure;
    readonly part?: Pick<ChannelPart, "files" | "text">;
}): Promise<WhatsAppSentPart>;
whatsappDeliverRoute

Where the agent's WhatsApp channel `name` delivers a message the ontology sends: a draft a member approved, a reply its review let go, a member's or a service's post. The ontology decides and renders it, its exact text with its attribution, its buttons or its approved template with the values; the channel sends it from its number as given, its messages in order in one operation. Only the deployment's own functions may call it.

function whatsappDeliverRoute(name: string): string;
whatsappFileRefusal

Why WhatsApp cannot take this file, or undefined when it can.

function whatsappFileRefusal(file: {
    readonly contentType: string;
    readonly size: number;
}): {
    readonly code: "too_large" | "empty";
    readonly message: string;
} | undefined;
whatsappFileType

How WhatsApp sends a file of this content type.

function whatsappFileType(contentType: string): WhatsAppFileType;