GitHub

telegram_bot_api

API reference for telegram_bot_api.

The minimal Bot API client the Telegram adapter is built on (Effect's @effect/chat-telegram/TelegramBotApi). It is public, so a consumer can call a method the adapter has no op for, with the same error mapping, the same rule for writes and the same care for the token.

The bot token is part of every request's path, and HttpClient records a request's path in its span and its errors. So the client's own span is off for every request made here, each call gets a span TelegramBotApi.{method} whose url.path is /bot<redacted>/{method}, and every failure is a ChatError whose cause holds no request, response or URL. Redirects are never followed: the next hop would see the token.

Re-exports

Values

AttributeTypeDescription
DEFAULT_API_URLLiteral['https://api.telegram.org'] = 'https://api.telegram.org'

The cloud Bot API server.

Types

TypeDefinitionDescription
Environmenttype Environment = Literal['production', 'test']

Which Telegram environment a bot lives in. The test environment has its own accounts and bots, and its methods sit under /bot<token>/test/.

Classes

Functions

make

make(*, token, environment='production', api_url=DEFAULT_API_URL, max_retry_after=None) -> Effect[TelegramBotApiService, ChatError, HttpClient]

Builds the client from an HttpClient and a bot token. Fails Unauthenticated with detail invalid_token when the token has no numeric bot id in front of a colon. api_url is for a local Bot API server and is privileged configuration. max_retry_after caps the wait a retry takes; see TelegramBotApiService.

ParameterTypeDescription
tokenRedacted[str]
environmentEnvironment = 'production'
api_urlstr = DEFAULT_API_URL
max_retry_afterDurationInput | None = None

Returns Effect[TelegramBotApiService, ChatError, HttpClient]

def make(
    *,
    token: Redacted[str],
    environment: Environment = "production",
    api_url: str = DEFAULT_API_URL,
    max_retry_after: DurationInput | None = None,
) -> Effect[TelegramBotApiService, ChatError, HttpClient]:
    """Builds the client from an ``HttpClient`` and a bot token. Fails
    ``Unauthenticated`` with detail ``invalid_token`` when the token has no
    numeric bot id in front of a colon. ``api_url`` is for a local Bot API
    server and is privileged configuration. ``max_retry_after`` caps the
    wait a retry takes; see ``TelegramBotApiService``."""
    ceiling = None if max_retry_after is None else from_input_unsafe(max_retry_after)

    @fx.gen
    def body() -> Generator[Effect[Any, ChatError, HttpClient], Any, TelegramBotApiService]:
        http = yield from HttpClient()
        match = _TOKEN.fullmatch(redacted_value(token))
        if match is None:
            return (yield from fx.fail(_errors.chat_error("layer", _errors.UNAUTHENTICATED_TOKEN)))
        bot_id: str = match[1]
        return TelegramBotApiService(
            http=http,
            token=token,
            bot_id=bot_id,
            environment=environment,
            api_url=api_url.rstrip("/"),
            max_retry_after=ceiling,
        )

    return body

layer

layer(*, token, environment='production', api_url=DEFAULT_API_URL, max_retry_after=None) -> Layer[TelegramBotApi, ChatError, HttpClient]

The client as a layer.

ParameterTypeDescription
tokenRedacted[str]
environmentEnvironment = 'production'
api_urlstr = DEFAULT_API_URL
max_retry_afterDurationInput | None = None

Returns Layer[TelegramBotApi, ChatError, HttpClient]

def layer(
    *,
    token: Redacted[str],
    environment: Environment = "production",
    api_url: str = DEFAULT_API_URL,
    max_retry_after: DurationInput | None = None,
) -> Layer[TelegramBotApi, ChatError, HttpClient]:
    """The client as a layer."""
    return _layer_effect(
        TelegramBotApi,
        make(
            token=token,
            environment=environment,
            api_url=api_url,
            max_retry_after=max_retry_after,
        ),
    )