telegram-bot-template

Telegram bot template based on grammY

Stars
211
Committers
6

Bot starter template based on grammY bot framework.

Features

  • Scalable structure
  • Config loading and validation
  • Internationalization, language changing
  • Graceful shutdown
  • Logger (powered by pino)
  • Ultrafast and multi-runtime server (powered by hono)
  • Ready-to-use deployment setups:
  • Examples:

Usage

Follow these steps to set up and run your bot using this template:

  1. Create a New Repository

    Start by creating a new repository using this template. You can do this by clicking here.

  2. Environment Variables Setup

    Create an environment variables file by copying the provided example file:

    cp .env.example .env
    

    Open the newly created .env file and set the BOT_TOKEN environment variable.

  3. Launching the Bot

    You can run your bot in both development and production modes.

    Development Mode:

    Install the required dependencies:

    npm install
    

    Start the bot in watch mode (auto-reload when code changes):

    npm run dev
    

    Production Mode:

    Install only production dependencies:

    npm install --only=prod
    

    Set DEBUG environment variable to false in your .env file.

    Start the bot in production mode:

    npm run start:force # skip type checking and start
    # or
    npm start # with type checking (requires development dependencies)
    

List of Available Commands

  • npm run lint — Lint source code.
  • npm run format — Format source code.
  • npm run typecheck — Run type checking.
  • npm run dev — Start the bot in development mode.
  • npm run start — Start the bot.
  • npm run start:force — Starts the bot without type checking.

Directory Structure

project-root/
  ├── locales # Localization files
  └── src
      ├── bot # Code related to bot
      │   ├── callback-data # Callback data builders
      │   ├── features      # Bot features
      │   ├── filters       # Update filters
      │   ├── handlers      # Update handlers
      │   ├── helpers       # Helper functions
      │   ├── keyboards     # Keyboard builders
      │   ├── middlewares   # Bot middlewares
      │   ├── i18n.ts       # Internationalization setup
      │   ├── context.ts    # Context object definition
      │   └── index.ts      # Bot entry point
      ├── server # Code related to web server
      │   ├── middlewares   # Server middlewares
      │   ├── environment   # Server environment setup
      │   └── index.ts      # Server entry point
      ├── config.ts # Application config
      ├── logger.ts # Logging setup
      └── main.ts   # Application entry point

Deploy

Docker (docker.com)

Branch: deploy/docker-compose (open diff)

Use in your project:

  1. Add the template repository as a remote
git remote add template [email protected]:bot-base/telegram-bot-template.git
git remote update
  1. Merge deployment setup
git merge template/deploy/docker-compose -X theirs --squash --no-commit --allow-unrelated-histories
  1. Follow the usage instructions in the deploy/docker-compose branch.

Vercel (vercel.com)

Branch: deploy/vercel (open diff)

Use in your project:

  1. Add the template repository as a remote
git remote add template [email protected]:bot-base/telegram-bot-template.git
git remote update
  1. Merge deployment setup
git merge template/deploy/vercel -X theirs --squash --no-commit --allow-unrelated-histories
  1. Follow the usage instructions in the deploy/vercel branch.

Examples

grammY conversations (grammy.dev/plugins/conversations)

Branch: example/plugin-conversations (open diff)

Use in your project:

  1. Add the template repository as a remote
git remote add template [email protected]:bot-base/telegram-bot-template.git
git remote update
  1. Merge example
git merge template/example/plugin-conversations -X theirs --squash --no-commit --allow-unrelated-histories
  1. Install dependencies
npm i @grammyjs/conversations
  1. Follow the usage instructions in the example/plugin-conversations branch.

Prisma ORM (prisma.io)

Branch: example/orm-prisma (open diff)

Use in your project:

  1. Add the template repository as a remote
git remote add template [email protected]:bot-base/telegram-bot-template.git
git remote update
  1. Merge example
git merge template/example/orm-prisma -X theirs --squash --no-commit --allow-unrelated-histories
  1. Install dependencies
npm i -D prisma
npm i @prisma/client
  1. Follow the usage instructions in the example/orm-prisma branch.

Bun (bun.sh)

Branch: example/runtime-bun (open diff)

Use in your project:

  1. Add the template repository as a remote
git remote add template [email protected]:bot-base/telegram-bot-template.git
git remote update
  1. Merge example
git merge template/example/runtime-bun -X theirs --squash --no-commit --allow-unrelated-histories
  1. Install dependencies
# remove Node-related dependencies
npm r @types/node tsx tsc-watch

# install dependencies
bun i

# remove npm lockfile
rm package-lock.json

# install bun typings
bun add -d @types/bun
  1. Follow the usage instructions in the example/runtime-bun branch.

Environment Variables