@effect-ak/tg-bot-client

中文文档
1.13.0发布于 2026/11/13MITTypeScriptESM + CJS构建来源已验证近 30 天下载 2995

Type-safe HTTP client for Telegram Bot API

原始使用文档
文档目录6 节
  1. Installation
  2. Quick Start
  3. API Surface
  4. Examples
  5. Documentation
  6. License

@effect-ak/tg-bot-client

NPM Version NPM Downloads

Type-safe HTTP client for the Telegram Bot API. One execute method covers every API method, files are encoded to FormData automatically, errors are typed. Native fetch, zero runtime dependencies — runs on Node.js 18+, Bun, Deno, Cloudflare Workers, browsers.

Types come from @effect-ak/tg-bot-api; the bot framework @effect-ak/tg-bot is built on this client.

Installation

npm install @effect-ak/tg-bot-client

The Bot API types are re-exported under a subpath, so this is the only package you need:

import { makeTgBotClient } from "@effect-ak/tg-bot-client"
import type { Message, Update } from "@effect-ak/tg-bot-client/api"

Quick Start

import { makeTgBotClient } from "@effect-ak/tg-bot-client"

const client = makeTgBotClient({ bot_token: "YOUR_BOT_TOKEN" })

// Method names are snake_case, exactly as in the official docs
const message = await client.execute("send_message", {
  chat_id: 123456789,
  text: "Hello!"
})

API Surface

makeTgBotClient({ bot_token, base_url?, timeout? }): TgBotClient

interface TgBotClient {
  execute(method, params, options?): Promise<Result>            // throws TgBotClientError
  executeSafe(method, params, options?): Promise<ClientResult>  // never throws
  getFile({ fileId }): Promise<TgFile>                          // download by file_id
  getFileSafe({ fileId }): Promise<ClientResult<TgFile>>
}

type ClientResult<T> = { ok: true; data: T } | { ok: false; error: ClientErrorReason }
type ClientErrorReason =
  | { _tag: "NotOkResponse"; errorCode?: number; details?: string }  // Telegram said ok:false
  | { _tag: "RequestTimeout"; timeoutMs: number }
  | { _tag: "NotJsonResponse" | "UnexpectedResponse" | "ClientInternalError" | "UnableToGetFile"; ... }
  • Method and parameter names match https://core.telegram.org/bots/api in snake_case (send_message, edit_message_text, answer_callback_query). Both are autocompleted; return types are inferred.
  • Files: pass { file_content: Uint8Array, file_name: string } (or a Blob / File) wherever the API accepts InputFile — the client switches to multipart automatically. Files inside objects (album items in send_media_group, stickers, set_my_profile_photo) work the same way: the client uploads each as its own part and sends the attach://… reference. Or pass a file_id / URL string.
  • Message effects: message_effect_id accepts an emoji key of MESSAGE_EFFECTS ("🔥" | "👍" | "👎" | "❤️" | "🎉" | "💩") and is mapped to the real id.
  • Errors: execute throws TgBotClientError (error.reason is the tagged union above); executeSafe returns the union — pick one style and stick to it.
  • Options: { timeout } per call; base_url for a self-hosted Bot API server.

Examples

// Safe variant
const result = await client.executeSafe("get_chat", { chat_id: 1 })
if (!result.ok) console.error(result.error._tag)

// Upload a file
await client.execute("send_document", {
  chat_id,
  document: { file_content: new TextEncoder().encode("hi"), file_name: "hi.txt" },
  caption: "Generated on the fly"
})

// Download a file
const file = await client.getFile({ fileId })
file.content // ArrayBuffer

Documentation

License

MIT

版本与发布记录

@effect-ak/tg-bot-client 共发布 48 个版本,首次发布于 2024/12/10;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.13.0latest2026/11/13今天
1.12.02026/09/052 个月前
1.11.02026/08/262 个月前
1.10.02026/08/152 个月前
1.9.02026/07/173 个月前
1.8.02026/07/084 个月前

关于 @effect-ak/tg-bot-client 的常见问题

@effect-ak/tg-bot-client 是什么?

Type-safe HTTP client for Telegram Bot API

如何安装 @effect-ak/tg-bot-client?

在项目目录运行 npm install @effect-ak/tg-bot-client,或使用 pnpm add / yarn add / bun add。

@effect-ak/tg-bot-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@effect-ak/tg-bot-client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@effect-ak/tg-bot-client 可以商用吗?

@effect-ak/tg-bot-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@effect-ak/tg-bot-client 的下载量有多少?

根据 npm 官方下载统计,@effect-ak/tg-bot-client 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,995 次,日均约 100 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@effect-ak/tg-bot-client 还在维护吗?

最新版本 1.13.0 发布于 2026/11/13(今天);至今共发布 48 个版本,其中过去 12 个月发布了 20 个;首次发布于 2024/12/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。