Introduction: why TypeScript and not Python?
\n\nHello, 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\nImagine: 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\nReady? Then let's go!
\n\nWhy choose TypeScript for a Telegram bot
\n\nBefore 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
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\nPreparation: what you need to install
\n\nTo 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\nNow let's install the dependencies:
\n\nnpm install telegraf dotenv\nnpm install -D typescript @types/node ts-node nodemon\n\nLet'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
Create a tsconfig.json file:
{\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\nAnd a .env file (don't forget to add it to .gitignore!):
BOT_TOKEN=your_token_from_BotFather\n\nBot registration: getting to know BotFather
\n\nThis is the easiest step. Open Telegram, find @BotFather and send the command /newbot. Follow the instructions:
- \n
- Enter the bot's name (e.g., "My Smart Assistant"). \n
- Enter a username (must end with
bot, e.g.,my_smart_helper_bot). \n - Copy the received token and paste it into
.env. \n
Done! Now you have the key to the Telegram API.
\n\nProject structure and first launch
\n\nCreate a src folder and inside it a file index.ts:
// src/index.ts\nimport { Telegraf } from 'telegraf';\nimport { mess