$ show yagop/node-telegram-bot-api
node-telegram-bot-api is a TypeScript client library for the Telegram Bot API, built as a runtime-agnostic core plus Node.js and Bun adapters. It exposes a Bot class with koa-style middleware, Context helpers, generated Api methods that map 1:1 to Telegram endpoints, and both long-polling and webhook update ingestion. The package targets Node 18+, Bun, Deno, Cloudflare Workers, and Vercel Edge by restricting src/core to Web-standard APIs and enforcing these boundaries with CI gates. It is a library, not a standalone app; consumers embed it to build Telegram bots. In production, the library receives updates either from getUpdates long polling (an AsyncGenerator with retry, backoff, and conflict handling) or from webhook callbacks (Web-standard Request to Response handlers plus adapters for Express, Next.js App Router, and Node HTTP). It includes a resilient transport layer with timeouts, retries, token-bucket rate limiting, and streaming multipart uploads, plus optional session middleware with pluggable stores (memory, file, SQLite, Redis) and a reply/callback tracking layer for structured conversations. The full Telegram type surface is auto-generated from live Bot API documentation by scripts/api-parser.ts, producing schemas.ts and api.ts, and the e2e suite exercises real Telegram endpoints. Architecturally, the most reusable patterns are generic middleware composition, an injectable fetch transport with backoff, a token-bucket rate limiter with per-key LRU state, and a streaming multipart encoder that avoids buffering whole uploads in memory. Session stores are deliberately minimal string-keyed KV contracts, with versioned envelopes allowing optional layers to extend state. The codebase is well tested, documented with 18 examples, and under active maintenance. This is not a data_research repository; the code generation pipeline is a build-time tool, not the product itself.
The LLM identified 5 files worth highlighting for reuse:
Koa-style middleware composer that folds an array of handlers into a single dispatch function while preserving registration order. It is runtime-agnostic and could be extracted into any TypeScript project needing composable request or context middleware.
The only module in the library that calls fetch. It composes a caller AbortSignal with a per-request timeout, retries transient failures such as 429 and 5xx with exponential backoff, honors retry_after, and re-streams multipart bodies on retries. Demonstrates robust HTTP client engineering and is adaptable to other web APIs.
A token-bucket rate limiter with a global bucket and per-chat or per-key sub-buckets, using LRU eviction to bound memory. The algorithm is generic and can be lifted into any in-process rate-limiting scenario.
Opt-in session middleware implementing a minimal string-keyed SessionStore contract, with per-key async locking, TTL expiry, and a versioned envelope so optional layers like reply tracking can extend session state without changing the store interface. The pattern is a strong blueprint for stateful middleware in event-driven systems.
Hand-rolled multipart/form-data encoder built on Web ReadableStream, supporting Blob, Uint8Array, ReadableStream, and replayable stream factories. It streams parts without buffering the entire body and is reusable for any Web-standard HTTP client needing memory-efficient file uploads.