Interface Client

A Telegram client that is bounded to a token/url.

interface Client {
    request<Method>(method, params): Promise<ClientResult<Method>>;
}

Methods

Methods

  • Make a request to the Telegram API.

    Type Parameters

    • Method extends keyof Methods

      the method to call. This should be inferred from usage. See example below.

    Parameters

    • method: Method

      The method to call

    • params: FormData | MethodParameters<Method>

      The parameters to pass to the method. If you want to call a Method that accepts a file, like sendDocument, you can pass a File object by sending a FormData object instead of a plain object. See buildFormDataFor for a type-safe way to build a FormData object for a specific Telegram API method.

    Returns Promise<ClientResult<Method>>

    The result of the method call

    Example

    const response = await client.request("sendMessage", { chat_id: 123, text: "Hello" });
    if (response.ok) {
    console.log(response.result.message_id);
    }

    Example

    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);
    }