Telegram bot in TypeScript: writing a smart assistant from scratch

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: why TypeScript and not Python?

\n\n

Hello, developer! If you are reading this on PythonLib, you are probably surprised: "Why an article about TypeScript on a Python resource?" The answer is simple: the IT world does not tolerate boundaries, and a good developer knows how to go beyond one language. TypeScript is not just a "superset of JavaScript", it is a powerful tool for creating reliable, scalable, and predictable applications. And Telegram bots are the perfect sandbox to try it out.

\n\n

Imagine: you write code, and the IDE highlights errors in advance, data types prevent runtime crashes, and autocompletion works like a Swiss watch. This is not magic — this is TypeScript. In this guide, we will create a full-fledged Telegram bot in TypeScript from scratch: from registration in BotFather to deployment. You will learn how to set up a project, handle commands, send messages, and work with external APIs.

\n\n

Ready? Then let's go!

\n\n

Why choose TypeScript for a Telegram bot

\n\n

Before diving into the code, let's figure out why you should write a bot in TypeScript rather than Python, which, it would seem, is the "native" language for such tasks.

\n\n
    \n
  • Typing is your friend. When a project has 10+ commands and 20 handlers, TypeScript will save you from silly mistakes like passing a string instead of a number.
  • \n
  • Modern syntax. Async/await, destructuring, modules — all this makes the code cleaner and more readable.
  • \n
  • Node.js ecosystem. A huge number of libraries, including Telegraf (the best framework for bots).
  • \n
  • Performance. Node.js is an event-driven platform, ideal for chat bots with high loads.
  • \n
\n\n

By the way, about Telegraf: it is a TypeScript-first framework that makes working with the Telegram Bot API as simple as possible. We will be using it.

\n\n

Preparation: what you need to install

\n\n

To get started, you will need:

\n\n
# Make sure Node.js (version 18+) and npm are installed\nnode -v\nnpm -v\n\n# Create a project folder\nmkdir telegram-bot-ts\ncd telegram-bot-ts\n\n# Initialize the project\nnpm init -y
\n\n

Now let's install the dependencies:

\n\n
npm install telegraf dotenv\nnpm install -D typescript @types/node ts-node nodemon
\n\n

Let's break down what we installed:

\n
    \n
  • telegraf — the main framework for the bot.
  • \n
  • dotenv — for storing the token in a .env file (security first).
  • \n
  • typescript, @types/node, ts-node — for compiling and running TS.
  • \n
  • nodemon — for automatic restart on changes.
  • \n
\n\n

Create a tsconfig.json file:

\n\n
{\n  "compilerOptions": {\n    "target": "ES2020",\n    "module": "commonjs",\n    "strict": true,\n    "esModuleInterop": true,\n    "skipLibCheck": true,\n    "forceConsistentCasingInFileNames": true,\n    "outDir": "./dist"\n  },\n  "include": ["src/**/*"],\n  "exclude": ["node_modules"]\n}
\n\n

And a .env file (don't forget to add it to .gitignore!):

\n\n
BOT_TOKEN=your_token_from_BotFather
\n\n

Bot registration: getting to know BotFather

\n\n

This is the easiest step. Open Telegram, find @BotFather and send the command /newbot. Follow the instructions:

\n
    \n
  1. Enter the bot's name (e.g., "My Smart Assistant").
  2. \n
  3. Enter a username (must end with bot, e.g., my_smart_helper_bot).
  4. \n
  5. Copy the received token and paste it into .env.
  6. \n
\n\n

Done! Now you have the key to the Telegram API.

\n\n

Project structure and first launch

\n\n

Create a src folder and inside it a file index.ts:

\n\n
// src/index.ts\nimport { Telegraf } from 'telegraf';\nimport { mess                

Blogs

Book Recommendations