Cloudflare Workers Telegram Bot

Cloudflare Workers Serverless Architecture KV Storage TypeScript Telegram Bot API WebSockets REST API

Cloudflare Workers Telegram Bot

A production-grade serverless Telegram bot platform demonstrating cloud-based architecture with dynamic menu systems and state persistence.

Overview

Built on Cloudflare Workers with KV Storage for persistent state management, featuring:

  • Dynamic nested reply keyboard system
  • Admin management API with authentication
  • Media broadcasting capabilities
  • Webhook-based real-time responses

Architecture

Cloudflare Worker
  ├─ /webhook (Telegram updates)
  ├─ /api/auth (admin authentication)
  ├─ /api/buttons (CRUD nested menus)
  ├─ /api/broadcast (push to all users)
  ├─ Static assets (served via ASSETS)
  └─ KV Storage (state persistence)

Key Features

  • Dynamic hierarchical menus: Multi-level reply keyboards with CRUD operations
  • Serverless architecture: Zero cold-start latency with edge deployment
  • KV persistence: User state, button configurations, and metadata
  • Media support: Photo, document, sticker attachments with captions
  • Admin controls: Chat ID-based authentication and broadcast capabilities
  • Webhook-based: Responsive, no-polling architecture

Technical Highlights

  • Markdown-like authoring converted to Telegram-safe HTML
  • Automatic keyboard refresh on configuration changes
  • Per-user data commands (/save, /get, /update, /delete)
  • Vitest test setup for reliability

API Endpoints

EndpointMethodPurpose
/api/authPOSTValidate admin credentials
/api/buttonsGET/POSTList / create menu buttons
/api/buttons/:idGET/PUT/DELETEManage individual buttons
/api/broadcastPOSTSend messages to all users
/webhookPOSTHandle Telegram updates

Source Code

GitHub Repository