import { ApiMethods } from '@grammyjs/types/methods'; import * as types from '@grammyjs/types'; export { types as Telegram }; declare const TgrmFileId: unique symbol; /** * A symbol that marks a File type */ type TgrmFileId = typeof TgrmFileId; /** * All the methods of the Telegram Bot API */ type Methods = ApiMethods; /** * A name of a method in the Telegram API * * @example "sendMessage" * @example "sendDocument" */ type Method = keyof Methods; /** * The parameters of a given method from {@link Methods} * * @example * ```typescript * const params: MethodParameters<"sendMessage"> = { chat_id: 123, text: "Hello" }; * ``` */ type MethodParameters = Parameters[0]; /** * The return type of a given method from {@link Methods} */ type MethodReturn = ReturnType; /** * The parameters of a given method from {@link Methods} with the File type replaced by a Blob * * @example Replace the file_id parameter with a File * ```ts * const file = new File(["hello"], "hello.txt"); * const params: FormDataParameters<"sendDocument"> = { chat_id: 123, document: file }; * ``` */ type FormDataParameters = { [key in keyof MethodParameters]: TgrmFileId extends MethodParameters[key] ? Exclude[key], TgrmFileId> | File : MethodParameters[key]; }; /** * A result of the `client.request` function. * * @example * ```typescript * const response = await client.request("sendMessage", { chat_id: 123, text: "Hello" }); * * if (response.ok) { * console.log(response.result.message_id); * } * ``` * * @group Helpers */ type ClientResult = { ok: false; } | { ok: true; result: MethodReturn; }; /** * Make a request to the Telegram API. * * @group Free functions */ declare function request({ baseUrl, fetch: providedFetch }: ResolvedTelegramClientOptions, method: Method, params: MethodParameters | FormData): Promise>; /** * A Telegram client that is bounded to a token/url. * * @group Client */ interface Client { /** * Make a request to the Telegram API. * * @param method The method to call * * @param params The parameters to pass to the method. If you want to call a {@link Method} that accepts a file, * like `sendDocument`, you can pass a `File` object by sending a {@link FormData} object instead of a plain object. * See {@link buildFormDataFor} for a type-safe way to build a {@link FormData} object for a specific Telegram API method. * * @returns The result of the method call * * @template Method the method to call. This should be inferred from usage. See example below. * * @example * ```typescript * const response = await client.request("sendMessage", { chat_id: 123, text: "Hello" }); * if (response.ok) { * console.log(response.result.message_id); * } * ``` * * @example * ```typescript * const response = await client.request( * "sendDocument", * buildFormDataFor<"sendDocument">({ * chat_id: 123, * document: new File(["hello"], "hello.txt") * }) * ); * * if (response.ok) { * console.log(response.result.message_id); * } * ``` */ request(method: Method, params: MethodParameters | FormData): Promise>; } type TokenOrBaseUrl = { token?: never; baseUrl: string | URL; } | { token: string; baseUrl?: never; }; /** * Options for the Telegram client. * Either a token or a base URL must be provided. */ type TelegramClientOptions = Omit & TokenOrBaseUrl; /** * Normalizes {@link TelegramClientOptions} to {@link ResolvedTelegramClientOptions} * so the client can be created. * * @internal */ declare function normalizeOptions(options: TelegramClientOptions): ResolvedTelegramClientOptions; /** * Resolved options for the Telegram client. * This is the "client options" after normalizing them with {@link normalizeOptions}. * * @internal */ interface ResolvedTelegramClientOptions { /** The base URL of the Telegram API. Contains the token. */ readonly baseUrl: string; /** A custom fetch function. Defaults to the global `fetch`. */ readonly fetch?: typeof fetch; } /** * Create a {@link Client}. * * @example * ```typescript * const client = createClient({ token: "xyz" }); * const clientFromBaseUrl = createClient({ baseUrl: "https://api.telegram.org/botxyz" }); * const clientWithCustomFetch = createClient({ token: "xyz", fetch: customFetchFunction }); * ``` * * @group Client */ declare const createClient: (userOptions: TelegramClientOptions) => Client; /** * Build a FormData object based on the given a {@link Method} * * This allows you to have autocomplete and type-checking * when using the `client.request` function, * while still being able to upload files to the Telegram API. * * The rules work as follows: * - `string` parameters will be appended to the FormData using their name. * - `File` parameters will be appended with their name and filename (`new File(...).name`) * - Other parameters will be `JSON.stringify`ed and appended with their name. * * @example * ```typescript * const params = buildFormDataFor<"sendDocument">({ * chat_id: 123, * document: new File(["hello"], "hello.txt") * }); * * const response = await client.request("sendDocument", params); * ``` * * @group Helpers */ declare function buildFormDataFor(params: FormDataParameters): FormData; type SendMessageParams = MethodParameters<"sendMessage">; type MessageEntity = NonNullable[number]; type PartialMessageEntity = { [key in MessageEntity["type"]]: Required>; }[MessageEntity["type"]]; /** * A tagged template literal function that builds a message with entities. * This allows you to avoid using `parse_mode` when sending messages, * and instead use this helper to create the `entities` array * in a composable manner. * * @example * ```ts * buildMessage`Hello ${entity("world", { type: "bold" })}!` * // => "world" will be bold in the message * ``` */ declare const buildMessage: (strings: string[] | TemplateStringsArray, ...values: (string | DecoratedText)[]) => DecoratedText; interface DecoratedText extends Pick { } /** * Wrap a text on an entire decorated message with an entity. * @see {@link buildMessage} */ declare const entity: (contents: string | DecoratedText, entity: PartialMessageEntity) => DecoratedText; export { type Client, type ClientResult, type FormDataParameters, type Method, type MethodParameters, type MethodReturn, type Methods, type PartialMessageEntity, type ResolvedTelegramClientOptions, type TelegramClientOptions, TgrmFileId, buildFormDataFor, buildMessage, createClient, entity, normalizeOptions, request };