360 Secure SkillHub
360 安全技能中心

telegram-bot-javascript

面向 Node.js 的 Telegram HTTP Bot API 开发技能,支持使用 grammY 或 Telegraf 构建或迁移机器人,覆盖中间件、会话、对话、回调、轮询与 Webhook、错误处理和部署配置,并提供可测试的起始代码与排障参考。

分类:开发
Aztech-1729
热度 0.00
最近更新 2026-11-30 11:37:03

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add aztech-1729/telegram-skills/telegram-bot-javascript
兼容性
通用兼容
联网要求
联网要求未确认
数据出网
可选出网
Metadata
name
telegram-bot-javascript
description
Build or migrate JavaScript and TypeScript Telegram HTTP Bot API bots with grammY or Telegraf, including middleware, sessions, conversations, callbacks, polling/webhooks and reliable async processing. Use for Node.js projects; inspect runtime support before adapting to Deno or edge environments.

JavaScript / TypeScript Telegram bots

Select and verify

Keep an existing project's framework. For a new project, grammY provides a documented plugin ecosystem and current Bot API types; Telegraf fits an existing Composer/Scenes application. These are HTTP Bot API frameworks, not user-account MTProto clients.

Checked 2026-10-03: grammY 1.46.0, Telegraf 4.16.3. Their Bot API coverage differs. Do not assume a recent Telegram feature has a Telegraf type or convenience method. Sources record official releases and topic references.

Read implementation guide for state, concurrency, media, webhooks, plugins, errors and deployment. Read only the plugin documentation needed by the task.

For multi-step grammY input, read dialogs and replay and its tested private-chat feedback factory for confirmation, external operations, cached authorization, cancellation and durable idempotency boundaries.

For a failing handler, missing API, session race or shutdown issue, use framework diagnostics before changing transport/framework. The focused audit there is dated separately from the release baseline.

Implementation workflow

  1. Establish bot token configuration (TELEGRAM_BOT_TOKEN in pack starters), runtime/module format, dependency lockfile, and one update transport.
  2. Register middleware in deliberate order; await API calls and next() when continuing the chain.
  3. Narrow update types before accessing fields; use framework filters and answer callback queries promptly.
  4. Choose session keys and persistence from the application's scope; serialize conflicting updates or use transactional state.
  5. Add bounded error handling, shutdown and replay handling suited to the deployment.

Starter and validation

The starter assets include separate grammY and Telegraf polling entrypoints and reusable bot factories. They provide /start, /help, a button callback and text echo. Real polling requires a token; tests use synthetic updates and a local fake HTTP server, with no Telegram calls.

cd telegram-bot-javascript/assets/starter
npm ci --ignore-scripts
npm test
node grammy.mjs
# Or choose the Telegraf entrypoint, not both for one token:
node telegraf.mjs

Install the chosen framework in the actual project, rather than carrying both dependencies by default. Adapt JavaScript starters to TypeScript with framework context types and strict checking; do not disable types to hide unsupported API fields.

Required integration decisions

  • A memory session is lost on restart and is not an order ledger. Concurrency must respect the same keys as state storage.
  • Conversation replay can repeat surrounding code: keep external side effects in the plugin's documented external-operation mechanism and use durable application idempotency.
  • Validate callback/action identity independently of a session. Text or callback data cannot grant administrative rights.
  • With webhooks, validate a dedicated secret and accept work before acknowledgment when durability matters. Avoid serverless long polling.
  • Bound retries and respect retry_after; transport failures can have uncertain send outcomes. Sanitize error logs.
  • Graceful shutdown must stop reception and drain accepted work, with a deployment-appropriate deadline.

Combine with keyboards, payments, Mini Apps, rich messaging or advanced operations when requested.

For product flows, load bot UX; for keyboard/focus or readable feedback, load accessibility. These decisions complement the framework handlers.

Upstream status

Before adopting a newer API or dependency, check automated source observations and compare them with the reviewed baseline.