Skip to content

GarageInc/telegram-bot-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

94 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Telegram Clicker Game - Full-Stack Template

Telegram Bot TypeScript Bun Docker Fly.io

A production-ready, full-stack Telegram Mini App template featuring real-time updates, leaderboards, and referral systems. Perfect for building viral Telegram games and interactive bots.

๐Ÿš€ Quick Start โ€ข โœจ Features โ€ข ๐Ÿ—๏ธ Architecture โ€ข ๐Ÿ“ฆ Deploy โ€ข ๐Ÿ“– Documentation


๐ŸŽฏ What is This?

A complete, battle-tested template for building Telegram Mini Apps with real-time gameplay, live leaderboards, viral referral mechanics, and a comment service for social engagement. Built with modern technologies and designed for scale.

Perfect For:

  • ๐ŸŽฎ Game Developers - Build viral clicker games, idle games, or competitive experiences
  • ๐Ÿ’ฌ Community Builders - Create discussion platforms with nested comments
  • ๐Ÿš€ Startup Founders - Launch your MVP in hours, not weeks
  • ๐Ÿ’ผ Agencies - White-label template for client projects
  • ๐ŸŽ“ Learners - Study a production-grade Telegram bot architecture

Why Use This Template?

โœ… Production-Ready - Already deployed and tested at scale
โœ… Real-Time Updates - Live leaderboards and statistics using Redis pub/sub
โœ… Viral Growth - Built-in referral system with encrypted codes
โœ… Comment Service - Tree-structured comments with unlimited nesting (MongoDB)
โœ… One-Click Deploy - Docker and Fly.io configs included
โœ… Type-Safe - Full TypeScript across frontend, backend, and bot
โœ… Modern Stack - Built with Bun, React, and the latest tools


โœจ Features

๐Ÿค– Telegram Bot

  • Interactive Menus - Beautiful inline keyboard navigation
  • Posts & Comments - Create posts directly from the bot
  • Admin Panel - User management, broadcasting, and analytics
  • Webhook Support - Production-ready webhook handling with Fly.io
  • Error Handling - Graceful error recovery with Slack notifications
  • Session Management - Stateful conversations and user context

๐ŸŽฎ Mini App (Web)

  • Telegram Web App API - Seamless integration with Telegram UI
  • Tab Navigation - Switch between Clicker game and Posts/Comments
  • Real-Time Updates - Live click counts and leaderboard updates
  • Comment Threads - Tree-structured discussions with unlimited nesting
  • Responsive Design - Works perfectly on mobile and desktop
  • Rate Limiting - Client-side and server-side anti-cheat measures
  • Combo System - Engaging gameplay mechanics

๐Ÿ”ง Backend API

  • High Performance - Built with Express and Bun for speed
  • Redis Caching - Lightning-fast data access
  • Batch Processing - Efficient database writes
  • Security - Telegram data validation and request signing
  • CORS Ready - Configured for cross-origin requests

๐Ÿ“Š Real-Time Features

  • Live Leaderboards - Top players updated in real-time
  • Global Statistics - Total clicks, active players, rankings
  • Broadcaster Service - Push updates to all active users
  • Session Tracking - Know who's online and where

๐ŸŽ Growth & Social Features

  • Referral System - Encrypted referral codes with tracking
  • Display Names - Let users customize their identity
  • Posts & Comments - Discussion system similar to Disqus
  • Nested Replies - Unlimited comment threading
  • Achievements - Track milestones and engagement
  • Notifications - Keep users engaged with updates

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Telegram User  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”
    โ”‚   Bot   โ”‚ โ—„โ”€โ”€โ”€ grammY Framework
    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜      โ€ข /start, /posts commands
         โ”‚           โ€ข Create posts
    โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Mini App     โ”‚ โ—„โ”€โ”€โ”€ React + Vite
    โ”‚  (Frontend)   โ”‚      โ€ข Clicker game
    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ€ข Posts & Comments
         โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Backend API  โ”‚ โ—„โ”€โ”€โ”€ Express + Bun
    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ€ข REST API
         โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Redis  โ”‚ Postgres โ”‚   MongoDB   โ”‚
    โ”‚ (Cache) โ”‚ (Users)  โ”‚(Posts/Cmts) โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Tech Stack

Component Technology Purpose
Bot grammY, TypeScript Telegram bot framework
Frontend React, Vite, TypeScript Mini app interface
Backend Express, Bun REST API server
Database PostgreSQL, Drizzle ORM User data & clicks
Posts/Comments MongoDB, Mongoose Discussion threads
Cache Redis, ioredis Real-time updates & caching
Runtime Bun Fast JavaScript runtime
Deploy Docker, Fly.io Production deployment

๐Ÿš€ Quick Start

Prerequisites

1. Clone & Install

# Clone the repository
git clone <your-repo-url>
cd telegram-clicker-game

# Install dependencies
bun install
cd bot && bun install && cd ..
cd backend && bun install && cd ..
cd web && bun install && cd ..
cd shared && bun install && cd ..

2. Configure Environment

Create .env files in each directory:

# Root .env or bot/.env
BOT_TOKEN=your_telegram_bot_token
DATABASE_URL=postgresql://admin:password@localhost:5432/databasev1
REDIS_URL=redis://localhost:6379
QUEUE_REDIS_URL=redis://localhost:6379
MONGO_URL=mongodb://localhost:27017/komi
MINI_APP_URL=http://localhost:5173
REFERRAL_CRYPTO_KEY=generate_with_openssl_rand_hex_16
REFERRAL_CRYPTO_IV=generate_with_openssl_rand_hex_8
ENABLE_WEBHOOKS=false

# backend/.env
BACKEND_PORT=4000
BOT_TOKEN=your_telegram_bot_token
DATABASE_URL=postgresql://admin:password@localhost:5432/databasev1
REDIS_URL=redis://localhost:6379
MONGO_URL=mongodb://localhost:27017/komi

Generate crypto keys:

openssl rand -hex 16  # REFERRAL_CRYPTO_KEY
openssl rand -hex 8   # REFERRAL_CRYPTO_IV

MongoDB Setup:

# Option 1: Local MongoDB with Docker
docker run -d --name mongodb -p 27017:27017 mongo:7

# Option 2: MongoDB Atlas (Free tier)
# Sign up at https://www.mongodb.com/cloud/atlas
# Create free M0 cluster and get connection string
MONGO_URL=mongodb+srv://username:[email protected]/komi

3. Start Services

Option A: Using Docker (Recommended)

cd bot
docker-compose up -d

This starts:

  • PostgreSQL (port 5432)
  • Redis (port 6379)
  • MongoDB (port 27017)
  • Backend API (port 4000)
  • Bot

Option B: Manual Start

# Terminal 1: Start bot
cd bot
bun run dev

# Terminal 2: Start backend
cd backend
bun run dev

# Terminal 3: Start web app
cd web
bun run dev

4. Open Your Bot

  1. Message your bot on Telegram
  2. Send /start command
  3. Play the clicker game: Click "๐ŸŽฎ Play Game" button
  4. Use comments feature: Click "๐Ÿ’ฌ Posts & Comments" button
  5. Start clicking and posting! ๐ŸŽ‰

๐Ÿ“ฆ Deployment

Deploy to Fly.io (Production)

# 1. Install Fly CLI
curl -L https://fly.io/install.sh | sh

# 2. Login
fly auth login

# 3. Run setup script
./fly-setup.sh

# 4. Deploy
./fly-deploy-all.sh

See FLY_DEPLOYMENT.md for detailed instructions.

Docker Build Commands

# Build bot
docker build -f bot/Dockerfile -t clicker-bot .

# Build backend
docker build -f backend/Dockerfile -t clicker-backend .

# Run with docker-compose
cd bot
docker-compose up --build

๐Ÿ“– Documentation

Project Structure

telegram-clicker-game/
โ”œโ”€โ”€ bot/                    # Telegram bot
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ commands/       # Bot commands (/start, /posts, /admin)
โ”‚   โ”‚   โ”œโ”€โ”€ menus/          # Interactive inline keyboards
โ”‚   โ”‚   โ”œโ”€โ”€ messages/       # Centralized text/labels
โ”‚   โ”‚   โ”œโ”€โ”€ services/       # Business logic
โ”‚   โ”‚   โ”œโ”€โ”€ plugins/        # grammY plugins
โ”‚   โ”‚   โ””โ”€โ”€ main.ts         # Bot entry point
โ”‚   โ””โ”€โ”€ Dockerfile
โ”‚
โ”œโ”€โ”€ backend/                # REST API
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ controllers/    # Route handlers (clicker, posts)
โ”‚   โ”‚   โ”œโ”€โ”€ routes/         # Route definitions with DI
โ”‚   โ”‚   โ”œโ”€โ”€ middleware/     # Express middleware (Telegram auth)
โ”‚   โ”‚   โ””โ”€โ”€ index.ts        # API entry point
โ”‚   โ””โ”€โ”€ Dockerfile
โ”‚
โ”œโ”€โ”€ web/                    # React Mini App
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ components/     # UI components (Clicker, Posts, Comments)
โ”‚       โ”œโ”€โ”€ hooks/          # React hooks (useClicker, usePosts)
โ”‚       โ””โ”€โ”€ App.tsx         # Main app with tab navigation
โ”‚
โ””โ”€โ”€ shared/                 # Shared code
    โ”œโ”€โ”€ database/           # Schemas (PostgreSQL + MongoDB)
    โ”œโ”€โ”€ infra/database/     # DB clients (Postgres, Mongo, Redis)
    โ”œโ”€โ”€ repositories/       # Data access layer
    โ””โ”€โ”€ services/           # Business logic

Key Components

Bot Menus

  • ExistingUserStart - Main menu with stats & leaderboard
  • NewUserStart - Onboarding for new users
  • PostsMenu - Browse and create posts
  • CreatePostMenu - Post creation with validation
  • AdminPanel - Admin controls
  • Referrals - Referral system management
  • ChangeDisplayName - User customization

Backend Endpoints

Clicker:

  • POST /api/clicker/click - Record user click
  • GET /api/clicker/stats - Get user statistics

Posts & Comments:

  • POST /api/posts - Create new post
  • GET /api/posts - List all posts
  • GET /api/posts/:id - Get single post
  • POST /api/posts/:id/comments - Add comment
  • GET /api/posts/:id/comments?tree=true - Get comment tree

Services

  • UserService - User management
  • ClickerService - Click tracking & validation
  • LeaderboardService - Rankings & statistics
  • PostService - Posts & comments with tree building
  • BroadcasterService - Real-time updates
  • NotificationService - Push notifications

Development Guides


๐ŸŽจ Customization

Change Game Mechanics

Edit click values and combo logic:

// shared/services/clicker.service.ts
const CLICK_VALUE = 1;
const COMBO_THRESHOLD = 5;

Customize UI

Modify the web app appearance:

// web/src/App.css
// web/src/components/ClickButton.css

Add Admin Commands

Create new admin features:

// bot/src/command/admin.command.ts
// bot/src/menus/AdminPanel.ts

Configure Webhooks

For production, enable webhooks:

ENABLE_WEBHOOKS=true
BOT_WEBHOOK_HOST=https://your-domain.fly.dev
WEBHOOK_SECRET_TOKEN=your_secret_token

๐Ÿ”’ Security Features

  • โœ… Telegram Data Validation - Verify all requests from Telegram
  • โœ… Rate Limiting - Prevent click spam and abuse
  • โœ… CORS Protection - Secure API endpoints
  • โœ… Encrypted Referrals - Tamper-proof referral codes
  • โœ… Environment Variables - No hardcoded secrets
  • โœ… Input Validation - Sanitize all user inputs

๐Ÿ“Š Performance

  • โšก < 50ms API response time
  • โšก 1000+ concurrent users supported
  • โšก Redis caching for sub-millisecond reads
  • โšก Batch writes to minimize database load
  • โšก Connection pooling for optimal resource usage

๐Ÿค Contributing

We love contributions! Check out our Contributing Guide to get started.

Ways to Contribute:

  • ๐Ÿ› Report bugs
  • ๐Ÿ’ก Suggest features
  • ๐Ÿ“ Improve documentation
  • ๐Ÿ”ง Submit pull requests

๐Ÿ“„ License

This project is open source and available under the MIT License.


๐ŸŒŸ Star History

If this template helped you, please give it a โญ on GitHub!


๐Ÿ’ฌ Community & Support


๐ŸŽฏ Roadmap

Core Features

  • Clicker game with combo system
  • Real-time leaderboards
  • Referral system
  • Posts & Comments (tree structure)
  • Tab navigation in Mini App

Upcoming

  • Edit/delete posts and comments
  • Like/reaction system
  • User mentions (@username)
  • Rich text formatting (Markdown)
  • Image attachments
  • Multi-language support
  • Achievement system
  • Daily rewards
  • Team/clan features
  • Analytics dashboard

๐Ÿ™ Acknowledgments

Built with:


Made with โค๏ธ for the Telegram community
Star this repo if you found it helpful!

About

Grammy + Menu + Updatable messages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published