gramio by gramiojs/documentation
npx skills add https://github.com/gramiojs/documentation --skill gramioGramIO 是一个现代化的、类型安全的 Telegram Bot API 框架,适用于 TypeScript。它运行在 Node.js、Bun 和 Deno 上,提供完整的 Bot API 覆盖、可组合的插件系统和一流的 TypeScript 支持。
@gramio/types 构建自定义 Bot API 包装器npm create gramio bot-name
cd bot-name
npm run dev
import { Bot } from "gramio";
const bot = new Bot(process.env.BOT_TOKEN as string)
.command("start", (context) => context.send("Hello!"))
.onStart(({ info }) => console.log(`@${info.username} started`))
.onError(({ context, kind, error }) => console.error(`[${kind}]`, error));
bot.start();
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
.command()、.on()、.extend() 等方法链式调用。顺序很重要。context.is("message") 进行类型收窄。new Plugin("name").derive(() => ({ ... })) 向上下文添加类型化的属性。通过 bot.extend(plugin) 注册。bot.api.method({ suppress: true }) 返回错误而不是抛出异常。await)在 bot.start() 时加载。使用 await 进行立即加载。.derive() 每次更新时运行(计算),.decorate() 一次性注入静态值。ctx 属性都是 camelCase 的获取器(ctx.from、ctx.firstName、ctx.chatId、ctx.messageId 等)。切勿访问 ctx.payload — 它是原始的 snake_case 格式的内部 Telegram 对象。请始终使用类型化的 camelCase 获取器。parse_mode — format 生成的是 MessageEntity 数组,而不是 HTML/Markdown 字符串。添加 parse_mode: "HTML" 或 "MarkdownV2" 会破坏消息。GramIO 会自动传递实体。.join() — 它会对每个 Formattable 调用 .toString(),从而静默地破坏所有样式。始终使用 join 辅助函数:join(items, (x) => bold(x), "\n")。| 插件 | 包 | 用途 |
|---|---|---|
| Session | @gramio/session | 持久化的每用户数据存储 |
| Scenes | @gramio/scenes | 多步骤对话流程 |
| I18n | @gramio/i18n | 国际化(TS 原生或 Fluent) |
| Autoload | @gramio/autoload | 基于文件的处理器加载 |
| Prompt | @gramio/prompt | 交互式单问题提示 |
| Views | @gramio/views | 可复用的消息模板(编程式 + JSON) |
| Auto Retry | @gramio/auto-retry | 在 429 速率限制时重试 |
| Media Cache | @gramio/media-cache | 缓存文件 ID |
| Media Group | @gramio/media-group | 处理相册消息 |
| Split | @gramio/split | 分割长消息 |
| Auto Answer CB | @gramio/auto-answer-callback-query | 自动回复回调查询 |
| PostHog | @gramio/posthog | 分析 + 功能标志 |
| OpenTelemetry | @gramio/opentelemetry | 分布式追踪和跨度 |
| Sentry | @gramio/sentry | 错误追踪 + 性能监控 |
GramIO 文档为 每一个 Telegram Bot API 方法和类型都提供了专门的参考页面:
https://gramio.dev/telegram/methods/{methodName} — 例如 sendMessage、createChatInviteLink、answerCallbackQueryhttps://gramio.dev/telegram/types/{typeName} — 例如 Message、ChatInviteLink、InlineKeyboard每个页面包含:GramIO TypeScript 示例、参数详情、带有原因和修复方法的错误表格、提示与注意事项以及相关链接。当用户询问特定的 Telegram API 方法或类型时,你可以获取或引用相应的页面以获取准确的 GramIO 特定用法。
给 LLM 的提示: 任何 GramIO 文档页面都可以通过向 URL 追加
.md来获取干净的 Markdown 格式:https://gramio.dev/telegram/methods/sendMessage.md— 这是干净的 Markdown 而非 HTML。这适用于文档的 所有 部分,不仅仅是 API 页面。
这些页面 默认不包含 在此技能中 — 当用户询问特定方法/类型时按需获取。
要快速查找存在哪些方法 — 请使用预构建的索引:telegram-api-index。它在一个文件中列出了所有 165+ 个 Bot API 方法及其简短描述。当你需要查找方法名称或在获取完整页面之前确认方法是否存在时,请加载它。
| 主题 | 描述 | 参考 |
|---|---|---|
| Bot 配置 | 构造函数、API 选项、代理、测试 DC、调试 | bot-configuration |
| Bot API | 调用方法、suppress、withRetries、类型辅助函数 | bot-api |
| 上下文与更新 | derive、decorate、中间件、start/stop、类型收窄 | context |
| 触发器 | command、hears、callbackQuery、inlineQuery、reaction | triggers |
| 钩子 | onStart、onStop、onError、preRequest、onResponse | hooks |
| 更新与生命周期 | start/stop 选项、优雅关闭(SIGINT/SIGTERM) | updates |
| 主题 | 描述 | 参考 |
|---|---|---|
| 键盘 | Keyboard、InlineKeyboard、布局辅助函数、样式 | keyboards |
| 格式化 | 实体辅助函数、join(切勿使用原生的 .join()!)、变量组合、不使用 parse_mode | formatting |
| 文件 | MediaUpload、MediaInput、下载、Bun.file() | files |
| CallbackData | 类型安全的回调数据模式 | callback-data |
| 存储 | 内存、Redis、Cloudflare 适配器 | storage |
| Telegram Stars | 支付、发票、订阅、内联发票、退款、测试模式 | telegram-stars |
| 类型 | @gramio/types、类型辅助函数、Proxy 包装器、声明合并 | types |
| 主题 | 描述 | 参考 |
|---|---|---|
| Webhook | 框架集成、隧道、自定义处理器 | webhook |
| 速率限制 | withRetries、广播、队列 | rate-limits |
| Docker | Dockerfile、多阶段构建、Docker Compose | docker |
| TMA | Mini Apps、mkcert HTTPS、@gramio/init-data 身份验证 | tma |
| 测试 | 事件驱动的机器人测试、用户角色、API 模拟 | testing |
当用户希望将现有机器人迁移到 GramIO 时加载。
| 来源 | 描述 | 参考 |
|---|---|---|
| puregram | 符号映射、API 比较、puregram → GramIO 重构清单 | migration-from-puregram |
| Telegraf | 符号映射、上下文类型推断、Scenes/WizardScene、webhook 差异、清单 | migration-from-telegraf |
| node-telegram-bot-api | 符号映射、中间件概念、键盘构建器、会话、清单 | migration-from-ntba |
| 插件 | 描述 | 参考 |
|---|---|---|
| Session | 每用户数据、Redis 支持 | session |
| Scenes | 多步骤流程、状态、导航 | scenes |
| I18n | TS 原生和 Fluent 国际化 | i18n |
| Autoload | 基于文件的处理器发现 | autoload |
| Prompt | 发送 + 等待响应 | prompt |
| Views | 可复用的消息模板、JSON 适配器、i18n | views |
| OpenTelemetry | 分布式追踪、跨度、插装 | opentelemetry |
| Sentry | 错误追踪、性能监控 | sentry |
| 其他 | auto-retry、media-cache、media-group、split、posthog | other |
| 插件开发 | 编写自定义插件、derive/decorate/error、惰性加载 | plugin-development |
| 示例 | 描述 | 文件 |
|---|---|---|
| 基础机器人 | 命令、钩子、错误处理 | basic.ts |
| 键盘 | 回复键盘、内联键盘、列、条件性 | keyboards.ts |
| CallbackData | 类型安全的回调模式 | callback-data.ts |
| 格式化 | 实体类型、join 辅助函数、变量组合、parse_mode 反模式 | formatting.ts |
| 文件上传 | 路径、URL、缓冲区、媒体组 | file-upload.ts |
| 错误处理 | 自定义错误、suppress、作用域 | error-handling.ts |
| Webhook | 框架集成 | webhook.ts |
| Session | 计数器、设置、Redis | session.ts |
| Scenes | 带步骤的注册流程 | scenes.ts |
| Telegram Stars | 支付、发票、退款 | telegram-stars.ts |
| TMA | Elysia 服务器、init-data 身份验证、webhook | tma.ts |
| Docker | 优雅关闭、webhook/轮询切换 | docker.ts |
| 测试 | 用户模拟、API 模拟、错误测试 | testing.ts |
每周安装量
99
代码库
GitHub 星标数
7
首次出现
2026年2月14日
安全审计
安装于
codex93
opencode93
gemini-cli92
amp92
github-copilot92
kimi-cli92
GramIO is a modern, type-safe Telegram Bot API framework for TypeScript. It runs on Node.js , Bun , and Deno with full Bot API coverage, a composable plugin system, and first-class TypeScript support.
@gramio/types for custom Bot API wrappersnpm create gramio bot-name
cd bot-name
npm run dev
import { Bot } from "gramio";
const bot = new Bot(process.env.BOT_TOKEN as string)
.command("start", (context) => context.send("Hello!"))
.onStart(({ info }) => console.log(`@${info.username} started`))
.onError(({ context, kind, error }) => console.error(`[${kind}]`, error));
bot.start();
.command(), .on(), .extend(), etc. Order matters.context.is("message") for type narrowing in generic handlers.new Plugin("name").derive(() => ({ ... })) adds typed properties to context. Register via bot.extend(plugin).bot.api.method({ suppress: true }) returns error instead of throwing.| Plugin | Package | Purpose |
|---|---|---|
| Session | @gramio/session | Persistent per-user data storage |
| Scenes | @gramio/scenes | Multi-step conversation flows |
| I18n | @gramio/i18n | Internationalization (TS-native or Fluent) |
| Autoload | @gramio/autoload | File-based handler loading |
| Prompt | @gramio/prompt |
GramIO docs include a dedicated reference page for every Telegram Bot API method and type:
https://gramio.dev/telegram/methods/{methodName} — e.g. sendMessage, createChatInviteLink, answerCallbackQueryhttps://gramio.dev/telegram/types/{typeName} — e.g. Message, ChatInviteLink, InlineKeyboardEach page contains: GramIO TypeScript examples, parameter details, error table with causes and fixes, tips & gotchas, and related links. When a user asks about a specific Telegram API method or type, you can fetch or reference the corresponding page for accurate GramIO-specific usage.
Tip for LLMs: Any GramIO docs page can be fetched as clean Markdown by appending
.mdto the URL:https://gramio.dev/telegram/methods/sendMessage.md— clean Markdown instead of HTML. This works for all sections of the docs, not just API pages.
These pages are not included in this skill by default — fetch them on demand when the user asks about a specific method/type.
To quickly find which methods exist — use the pre-built index: telegram-api-index. It lists all 165+ Bot API methods with short descriptions in one file. Load it when you need to discover a method name or confirm one exists before fetching a full page.
| Topic | Description | Reference |
|---|---|---|
| Bot Configuration | Constructor, API options, proxy, test DC, debugging | bot-configuration |
| Bot API | Calling methods, suppress, withRetries, type helpers | bot-api |
| Context & Updates | derive, decorate, middleware, start/stop, type narrowing | context |
| Triggers | command, hears, callbackQuery, inlineQuery, reaction | triggers |
| Hooks | onStart, onStop, onError, preRequest, onResponse | hooks |
| Updates & Lifecycle | start/stop options, graceful shutdown (SIGINT/SIGTERM) |
| Topic | Description | Reference |
|---|---|---|
| Keyboards | Keyboard, InlineKeyboard, layout helpers, styling | keyboards |
| Formatting | entity helpers, join (never native .join()!), variable composition, no parse_mode | formatting |
| Files | MediaUpload, MediaInput, download, Bun.file() | files |
| CallbackData | Type-safe callback data schemas | callback-data |
| Topic | Description | Reference |
|---|---|---|
| Webhook | Framework integration, tunneling, custom handlers | webhook |
| Rate Limits | withRetries, broadcasting, queues | rate-limits |
| Docker | Dockerfile, multi-stage build, Docker Compose | docker |
| TMA | Mini Apps, mkcert HTTPS, @gramio/init-data auth | tma |
| Testing | Event-driven bot testing, user actors, API mocking | testing |
Load when the user wants to migrate an existing bot to GramIO.
| From | Description | Reference |
|---|---|---|
| puregram | Symbol mapping, API comparisons, checklist for puregram → GramIO refactor | migration-from-puregram |
| Telegraf | Symbol mapping, context typing, Scenes/WizardScene, webhook differences, checklist | migration-from-telegraf |
| node-telegram-bot-api | Symbol mapping, middleware concepts, keyboard builders, session, checklist | migration-from-ntba |
| Plugin | Description | Reference |
|---|---|---|
| Session | Per-user data, Redis support | session |
| Scenes | Multi-step flows, state, navigation | scenes |
| I18n | TS-native and Fluent internationalization | i18n |
| Autoload | File-based handler discovery | autoload |
| Prompt | Send + wait for response | prompt |
| Views | Reusable message templates, JSON adapter, i18n | views |
| Example | Description | File |
|---|---|---|
| Basic bot | Commands, hooks, error handling | basic.ts |
| Keyboards | Reply, inline, columns, conditional | keyboards.ts |
| CallbackData | Type-safe callback schemas | callback-data.ts |
| Formatting | Entity types, join helper, variable composition, parse_mode anti-pattern | formatting.ts |
| File upload | Path, URL, buffer, media groups | file-upload.ts |
| Error handling | Custom errors, suppress, scoped |
Weekly Installs
99
Repository
GitHub Stars
7
First Seen
Feb 14, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
codex93
opencode93
gemini-cli92
amp92
github-copilot92
kimi-cli92
MCP服务器模式指南:构建AI助手工具与资源的最佳实践
1,100 周安装
PrivateInvestigator 道德人员查找工具 | 公开数据调查、反向搜索与背景研究
69 周安装
TorchTitan:PyTorch原生分布式大语言模型预训练平台,支持4D并行与H100 GPU加速
69 周安装
screenshot 截图技能:跨平台桌面截图工具,支持macOS/Linux权限管理与多模式捕获
69 周安装
tmux进程管理最佳实践:交互式Shell初始化、会话命名与生命周期管理
69 周安装
Git Rebase Sync:安全同步分支的Git变基工具,解决冲突与备份
69 周安装
LinkedIn自动化工具 - Claude Code专属,自然对话拓展人脉,避免垃圾信息
69 周安装
format\``` 中** — 将 Formattable 嵌入到普通的模板字面量(```${boldx}```)中会剥离所有实体。应使用 format${boldx`}``。awaitbot.start()await.derive() runs per-update (computed), .decorate() injects static values once.ctx properties are camelCase getters (ctx.from, ctx.firstName, ctx.chatId, ctx.messageId, etc.). Never accessctx.payload — it is the raw snake_case internal Telegram object. Use the typed camelCase getters for everything.parse_mode — format produces MessageEntity arrays, not HTML/Markdown strings. Adding parse_mode: "HTML" or "MarkdownV2" will break the message. GramIO passes entities automatically..join() on arrays of formatted values — it calls .toString() on each Formattable, silently destroying all styling. Always use the join helper: join(items, (x) => bold(x), "\n").format\```** when composing or reusing — embedding a Formattablein a plain template literal (```${boldx}```) strips all entities. Useformat${boldx`}`` instead.| Interactive single-question prompts |
| Views | @gramio/views | Reusable message templates (programmatic + JSON) |
| Auto Retry | @gramio/auto-retry | Retry on 429 rate limits |
| Media Cache | @gramio/media-cache | Cache file_ids |
| Media Group | @gramio/media-group | Handle album messages |
| Split | @gramio/split | Split long messages |
| Auto Answer CB | @gramio/auto-answer-callback-query | Auto-answer callbacks |
| PostHog | @gramio/posthog | Analytics + feature flags |
| OpenTelemetry | @gramio/opentelemetry | Distributed tracing and spans |
| Sentry | @gramio/sentry | Error tracking + performance monitoring |
| updates |
| Storage | In-memory, Redis, Cloudflare adapters | storage |
| Telegram Stars | Payments, invoices, subscriptions, inline invoices, refunds, test mode | telegram-stars |
| Types | @gramio/types, type helpers, Proxy wrapper, declaration merging | types |
| OpenTelemetry | Distributed tracing, spans, instrumentation | opentelemetry |
| Sentry | Error tracking, performance monitoring | sentry |
| Others | auto-retry, media-cache, media-group, split, posthog | other |
| Plugin Development | Writing custom plugins, derive/decorate/error, lazy loading | plugin-development |
| Webhook | Framework integration | webhook.ts |
| Session | Counters, settings, Redis | session.ts |
| Scenes | Registration flow with steps | scenes.ts |
| Telegram Stars | Payments, invoices, refunds | telegram-stars.ts |
| TMA | Elysia server, init-data auth, webhook | tma.ts |
| Docker | Graceful shutdown, webhook/polling toggle | docker.ts |
| Testing | User simulation, API mocking, error testing | testing.ts |