telebot-ts API Documentation - v1.0.0
    Preparing search index...

    Class BaseBotClientAbstract

    Core HTTP dispatcher and credential manager for all Telegram Bot API operations.

    Implements automated rate-limit backoff (HTTP 429 retry_after), exponential backoff for server errors (HTTP 5xx), payload serialization (JSON or multipart FormData), and uniform error unwrapping into TelegramApiError.

    Index
    • Constructs a new BaseBotClient.

      Parameters

      • token: string

        Telegram bot token received from BotFather. Must follow format <bot_id>:<secret_token>.

      • options: BotOptions = {}

        Configuration options for the client including custom fetch or local API server URL.

      Returns BaseBotClient

      When the token is empty or invalid.

    _fetch: {
        (input: URL | RequestInfo, init?: RequestInit): Promise<Response>;
        (input: string | URL | Request, init?: RequestInit): Promise<Response>;
    }

    Internal fetch adapter instance used for HTTP dispatch.

    Type Declaration

      • (input: URL | RequestInfo, init?: RequestInit): Promise<Response>
      • Parameters

        • input: URL | RequestInfo
        • Optionalinit: RequestInit

        Returns Promise<Response>

      • (input: string | URL | Request, init?: RequestInit): Promise<Response>
      • Parameters

        • input: string | URL | Request
        • Optionalinit: RequestInit

        Returns Promise<Response>

    apiRoot: string

    Base endpoint URL of the Telegram Bot API server.

    baseDelayMs?: number

    Optional base delay in milliseconds used for retry calculations.

    options: BotOptions

    Client configuration options.

    requestTimeoutMs: number

    Default per-request abort timeout in milliseconds.

    token: string

    Secret Telegram Bot API token received from BotFather.

    • Dynamically configures or updates the auto-retry and flood control settings.

      Parameters

      • options: Partial<RetryOptions>

        Partial retry options to update.

      Returns this

      This client instance for chaining.

      bot.configureRetry({
      maxRetryAttempts: 5,
      minDelaySeconds: 2,
      });
    • Registers an observer invoked whenever a Bot API request finally fails.

      Hooks run in registration order right before the TelegramApiError is thrown, after retries are exhausted. Observers cannot alter the error; exceptions inside a hook are swallowed and logged. Useful for alerting and metrics plugins.

      Parameters

      Returns this

      This client instance for chaining.

      bot.onApiError((method, error) => {
      console.error(`${method} failed: ${error.description}`);
      });
    • Removes every request transform, response transform, and API error hook registered with the given owner tag.

      Parameters

      • tag: string

        Owner tag previously passed when registering hooks.

      Returns number

      The number of hooks removed.

      bot.removeHooksByTag("my-plugin");
      
    • Core request dispatcher to execute any raw Telegram Bot API endpoint.

      Automatically serializes JSON objects or multipart FormData when file buffers / InputFile are present. Handles HTTP 429 rate-limiting (honoring retry_after) and HTTP 5xx retries with exponential backoff, sharing a single retry budget across both so that repeated 429s cannot retry forever or starve genuine 5xx retries.

      Type Parameters

      • T

        The expected return payload type from Telegram.

      Parameters

      • method: string

        The Bot API method name (e.g. "sendMessage", "getMe", "setWebhook").

      • payload: Record<string, unknown> = {}

        Key-value parameters corresponding to the API method fields.

      Returns Promise<T>

      Resolves with the unwrapped result field returned by Telegram.

      TelegramApiError When Telegram returns ok: false, HTTP 4xx, or after retry exhaustion on 429/5xx/network errors.

      const user = await bot.request<User>("getMe");
      console.log(`Bot username: @${user.username}`);
    • Helper utility to pause asynchronous execution for a given number of seconds.

      Used internally during retry delays and rate-limit backoff.

      Parameters

      • seconds: number

        Duration in seconds to pause execution.

      Returns Promise<void>

      Promise that resolves once the specified duration elapses.

      await bot.sleep(2); // Pauses execution for 2 seconds
      
    • Registers a hook invoked before every outgoing Bot API request.

      Hooks run in registration order and may mutate the payload. Intended for plugins that need to observe or decorate all API traffic (logging, metrics, throttling signals, test mocks).

      Parameters

      Returns this

      This client instance for chaining.

      bot.transformRequest((method, payload) => {
      console.log(`Calling ${method}`);
      payload["protect_content"] = true;
      });
    • Registers a hook invoked with every successful Bot API response.

      Hooks run in registration order. Returning a value replaces the result for subsequent hooks and the caller; returning undefined keeps it unchanged. Useful for response logging, caching, or normalization plugins.

      Parameters

      Returns this

      This client instance for chaining.

      bot.transformResponse((method, result) => {
      console.log(`${method} succeeded`);
      });