Skip to content
SegmentStream/ Docs

Loading documentation…

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

@segmentstream/ontology/telegram

59 public exports from @segmentstream/ontology/telegram.

Public exports

CHANNEL_FILE_FRAME_BYTES

The most bytes a call sends before, and the most after, the stored file it carries.

const CHANNEL_FILE_FRAME_BYTES: number
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;
}
ChannelCallFile

The body of a call that carries a stored file: `prefix`, the file's bytes from `start` to `end` inclusive, read by the connection by the file's signed read, then `suffix`, such as a multipart form's fields and its closing boundary around the file's part. Each of `prefix` and `suffix` is at most {@link CHANNEL_FILE_FRAME_BYTES}.

interface ChannelCallFile extends ChannelFileRead {
    readonly start: number;
    readonly end: number;
    readonly prefix?: Uint8Array;
    readonly suffix?: Uint8Array;
}
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;
}
ChannelFile

A file read from storage, which the channel uploads.

interface ChannelFile {
    readonly filename: string;
    readonly contentType: string;
    readonly bytes: Uint8Array;
}
ChannelFileRead

A signed read of a file in the workspace's file storage, valid for about a minute.

interface ChannelFileRead {
    readonly url: string;
    readonly headers: ReadonlyArray<readonly [
        string,
        string
    ]>;
}
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>;
ChannelStoredFile

A file the channel sends without its bytes passing through the sender: they stay in the workspace's file storage, which the provider's request reads by a signed read asked for just before it. A file too large for a request's own body goes this way.

interface ChannelStoredFile {
    readonly filename: string;
    readonly contentType: string;
    readonly size: number;
    readonly read: () => Promise<ChannelFileRead>;
}
DefiniteFailure

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

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

The most characters a Telegram caption has.

const TELEGRAM_CAPTION_LIMIT = 1024
TELEGRAM_DELIVER_PARTS

The most text messages one delivery carries.

const TELEGRAM_DELIVER_PARTS = 20
TELEGRAM_DOCUMENT_BYTES

The most bytes Telegram takes of a document a bot uploads.

const TELEGRAM_DOCUMENT_BYTES: number
TELEGRAM_FILES_PER_MESSAGE

The most files one Telegram album carries.

const TELEGRAM_FILES_PER_MESSAGE = 10
TELEGRAM_INLINE_FILE_BYTES

The most bytes of a file sent in a request's own body, which leaves room in the connection's request bound for the form around it. Files this small can go together as an album; a larger one goes by its signed read, alone in its message.

const TELEGRAM_INLINE_FILE_BYTES: number
TELEGRAM_MESSAGE_LIMIT

The most characters Telegram takes in one message's text.

const TELEGRAM_MESSAGE_LIMIT = 4096
TELEGRAM_PHOTO_BYTES

The most bytes Telegram takes of a photo.

const TELEGRAM_PHOTO_BYTES: number
TelegramDeliver

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

interface TelegramDeliver {
    readonly operation_id: string;
    readonly connection: string;
    readonly chat_id: string;
    readonly thread_id: string | null;
    readonly reply_to: string | null;
    readonly parts: ReadonlyArray<TelegramDeliverPart>;
    readonly rich?: TelegramDeliverRich;
    readonly reply_markup?: Readonly<Record<string, unknown>>;
    readonly files?: ReadonlyArray<ChannelDeliverFile>;
}
TelegramDeliverPart

One text message of a delivery: as Telegram formats it, and the same text without formatting, which is sent once in its place only when Telegram refuses the formatting.

interface TelegramDeliverPart {
    readonly formatted: TelegramDeliverText;
    readonly plain: TelegramDeliverText;
}
TelegramDeliverRefused

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

interface TelegramDeliverRefused {
    readonly error: string;
    readonly refusal?: "recipient_unreachable" | "buttons_unsupported";
    readonly reference?: string;
}
TelegramDeliverRich

One Telegram rich message as `sendRichMessage` takes it: its Markdown.

interface TelegramDeliverRich {
    readonly markdown: string;
}
TelegramDeliverText

One text as `sendMessage` takes it: the text, its format, and its link preview.

interface TelegramDeliverText {
    readonly text: string;
    readonly parse_mode?: "MarkdownV2" | "HTML";
    readonly link_preview_options?: Readonly<Record<string, unknown>>;
    readonly disable_web_page_preview?: boolean;
}
TelegramDelivered

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

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

A formatting entity of Telegram's, in UTF-16 code units.

interface TelegramEntity {
    readonly type: string;
    readonly offset: number;
    readonly length: number;
    readonly url?: string;
    readonly language?: string;
    readonly unix_time?: number;
    readonly date_time_format?: string;
}
TelegramFilesTarget

Where the files go: the chat, its forum topic, and the message they answer.

interface TelegramFilesTarget {
    readonly chatId: string;
    readonly messageThreadId?: string;
    readonly replyTo?: string | number;
}
TelegramFormattedText

Visible text and the entities that format it.

interface TelegramFormattedText {
    readonly text: string;
    readonly entities: ReadonlyArray<TelegramEntity>;
    readonly parseMode?: "MarkdownV2" | "HTML";
}
TelegramLinkEntity
interface TelegramLinkEntity {
    readonly type: string;
    readonly offset: number;
    readonly length: number;
    readonly url?: string;
}
TelegramLinkPreviews
type TelegramLinkPreviews = (typeof TELEGRAM_LINK_PREVIEWS)[number];
TelegramPreviewContext
interface TelegramPreviewContext {
    readonly settings?: TelegramPreviewSettings;
    readonly ownOrigins?: readonly string[];
}
TelegramPreviewSettings

Settings belong to the destination connection, independently of the source channel.

interface TelegramPreviewSettings {
    readonly evaluation?: {
        readonly receiverBotId: string;
        readonly senderBotId: string;
        readonly groups: readonly string[];
        readonly privateParticipantSlot?: string;
        readonly decisions?: "simulated";
    };
    readonly connection: string;
    readonly linkPreviews?: TelegramLinkPreviews;
    readonly additionalOwnOrigins?: readonly string[];
    readonly appLinks?: TelegramAppLinks;
}
httpOrigin

Normalize HTTP(S) origins only; credential-bearing and non-web destinations are ineligible.

function httpOrigin(value: string): string | undefined;
isAppContentPage

Whether a link may open the app's page at `pathname`: a local path, also once decoded, that is not an API, an asset, or a page where the member decides something.

function isAppContentPage(pathname: string): boolean;
isHttpOrigin
function isHttpOrigin(value: unknown): value is string;
isTelegramBotUsername

A username Telegram accepts in a `t.me` link; any other leaves every link as it is.

function isTelegramBotUsername(username: string): boolean;
sendTelegramFiles

Sends `files` to `target` in their order, `caption` on the first message Telegram takes. Each album is one request and one part: `sent` when Telegram names its messages, `failed` when it refused them (`too_large` for 413, `forbidden` for 403, `send_failed` otherwise) or a request could not be built (`unavailable` for a stored file whose read could not be had), `uncertain` when its answer proves neither. A stored file goes alone, through a `request` that reads stored files. A refusal of the caption's formatting alone sends the album again without it. Nothing is retried otherwise.

function sendTelegramFiles(options: {
    readonly request: ChannelRequest;
    readonly target: TelegramFilesTarget;
    readonly files: ReadonlyArray<(ChannelFile | ChannelStoredFile) & {
        readonly index?: number;
    }>;
    readonly caption?: TelegramFormattedText;
    readonly plainCaption?: TelegramFormattedText;
    readonly definite?: DefiniteFailure;
}): Promise<ReadonlyArray<ChannelPart>>;
telegramAccountMessageIds

Private chats and basic groups number messages per account. Supergroup and channel message numbers are shared. Their Bot API peer IDs start below -1000000000000. https://core.telegram.org/api/bots/ids

function telegramAccountMessageIds(chatId: string): boolean;
telegramButtonsRefused

A definite Bot API rejection of the card's keyboard, safe to replace with simpler buttons.

function telegramButtonsRefused(status: number, body: unknown): boolean;
telegramDeliverRoute

Where the agent's Telegram 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, links and previews; the channel sends it through its bot, its parts in order, or the rich message that takes their place, in one operation. Only the deployment's own functions may call it.

function telegramDeliverRoute(name: string): string;
telegramEvaluationPeer

Validate the provider peer independently from the operator's permission to run the step.

function telegramEvaluationPeer(target: NonNullable<TelegramPreviewSettings["evaluation"]>, chatId: string, kind: unknown): boolean;
telegramFileBytes

The most bytes of one file a single connection request can carry to Telegram in its body.

function telegramFileBytes(contentType: string): number;
telegramFileRefusal

Why Telegram cannot take this file through one connection request, or undefined when it can: `too_large` past Telegram's bound for its type or, unless the file may go by its signed read (`stored`), the connection's request bound; `empty` for no bytes.

function telegramFileRefusal(file: {
    readonly contentType: string;
    readonly size: number;
}, options?: {
    readonly stored?: boolean;
}): {
    readonly code: "too_large" | "empty";
    readonly message: string;
} | undefined;
telegramFileType

How Telegram sends a file of this content type: as a photo or as a document.

function telegramFileType(contentType: string): "photo" | "document";
telegramFormattingRefused

Whether Telegram refused a message only for its formatting entities, which it then takes without them. Only this definite refusal permits a second request; an unknown failure never does.

function telegramFormattingRefused(status: number, body: unknown): boolean;
telegramPreviewEnvironment

Public, non-secret deployment metadata; no request headers or cross-tenant suffix matching.

function telegramPreviewEnvironment(environment: Readonly<Record<string, string | undefined>>): {
    readonly channels?: {
        readonly telegram?: TelegramPreviewSettings;
    };
    readonly appOrigins: string[];
};
telegramPreviewOptions

Explicit modern > explicit legacy > channel mode > external default. Never merge caller options.

function telegramPreviewOptions(caller: Readonly<Record<string, unknown>>, links: readonly string[], context?: TelegramPreviewContext): Record<string, unknown>;
telegramStartParam

The `startapp` value of a page's path and query, or `undefined` when it does not fit.

function telegramStartParam(page: string): string | undefined;
validateTelegramPreviews

Also validates programmatic standalone service configuration.

function validateTelegramPreviews(settings: TelegramPreviewSettings): void;
withCrlfLineBreaks

The same text with every line break written CRLF, and its entities moved to match. A multipart form sends a text field's line breaks as CRLF, and Telegram counts the CR in entity offsets, so a caption sent in one keeps its links and formatting only once its line breaks are CRLF already.

function withCrlfLineBreaks<Text extends TelegramFormattedText>(message: Text): Text;