@effect-ak/tg-bot-client

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

Telegram Bot API 的安全 HTTP 客户端

中文使用文档
文档目录6 节
  1. 安装
  2. 快速启动
  3. API 表面
  4. 实例
  5. 文档
  6. 许可证

@ affect-ak/tg-bot-客户端

NPM Version NPM Downloads

Telegram Bot API的Type-safe HTTP客户端. 一个 execute 方法覆盖每个 API 方法,文件被编码到 FormData 自动输入错误。 土著 fetch,零运行时间依赖性——运行在Node.js 18+, Bun, Deno, Cloudflare Workers,浏览器上.

类型来自 @effect-ak/tg-bot-api; 机器人框架 @effect-ak/tg-bot 以此客户端为基础.

安装

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

Bot API 类型在子路径下被再导出,所以这是您唯一需要的软件包 :

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

快速启动

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 表面

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"; ... }
  • 方法和参数名称 匹配 https://core.telegram.org/bots/api (英语) 内 snake_case (单位:千美元)send_message, (中文(简体) ), edit_message_text, (中文(简体) ), answer_callback_query) (中文(简体) ). 两者都是自动完成的;返回类型是推断的.
  • 文件: 通过 { file_content: Uint8Array, file_name: string } (或 a 个) Blob 编号 : File在API接受的情况下 InputFile ——客户端自动切换到多段. 对象内的文件( album 项在 send_media_group贴纸, set_my_profile_photo) 工作方式相同:客户端上传各自作为自己的部分并发送 attach://… 参考文献。 或者通过一个 file_id / URL 字符串 .
  • 信件效果数字 : message_effect_id 接受 emoji 键 MESSAGE_EFFECTS (单位:千美元)"🔥" | "👍" | "👎" | "❤️" | "🎉" | "💩"并被映射到真实的id.
  • 错误数字 : execute 掷出 TgBotClientError (单位:千美元)error.reason 上面贴有标签的结合; executeSafe 返回结合 —— 选择一种风格并坚持它.
  • 选项数字 : { timeout } 每个呼叫; base_url 用于自动托管的 Bot API 服务器.

实例

// 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

文档

许可证

麻省理工学院

版本与发布记录

@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 是什么?

Telegram Bot API 的安全 HTTP 客户端

如何安装 @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 和提交记录判断。