Telegram Bot in JavaScript: Where to Start and How Not to Get Confused
\n\nHave you ever wondered how those smart bots in Telegram work, the ones that send weather updates, remind you of deadlines, or just tell funny jokes? What if I told you that you can write your own Telegram bot in JavaScript in just one evening? Yes, even if you are a beginner. In this guide, we will break down the process of creating a bot step by step, with real code examples and no fluff.
\n\nWhy JavaScript? Because it is a universal language that works everywhere: on servers, in the browser, and even on a Raspberry Pi. And with the Telegraf library, creating a Telegram bot becomes a real pleasure. Ready? Let's go!
\n\nWhat We Need to Create a Telegram Bot in JS
\n\nBefore writing code, let's prepare the tools. You will need:
\n\n- \n
- Node.js (version 14 or newer) — a JavaScript runtime environment on the server. \n
- A text editor — Visual Studio Code, Sublime Text, or any other. \n
- A Telegram account — well, you can't do without it. \n
- A terminal — command line for installing packages. \n
If Node.js is not installed yet, download it from the official website. The installation process is trivial: just keep clicking "Next".
\n\nRegistering the Bot and Getting a Token
\n\nThe first step to creating a Telegram bot in JavaScript is to get an API token. This is a secret key that connects your code to Telegram.
\n\n- \n
- Open Telegram and find the bot @BotFather. \n
- Send the command
/newbot. \n - Think of a name for the bot (e.g., MyFirstTestBot). \n
- Think of a unique username ending in bot (e.g., my_first_test_2025_bot). \n
- Save the received token — it will look something like this:
1234567890:ABCdefGHIjklMNOpqrsTUVwxyz. \n
Important: The token is your password. Do not show it to anyone and do not upload it to GitHub in plain sight.
\n\nInstalling Libraries and Setting Up the Project
\n\nNow let's move on to the code. Create a folder for the project, open it in the terminal, and run:
\n\nnpm init -y\n\nThis command will create a package.json file. Now let's install the main library — Telegraf:
npm install telegraf\n\nTelegraf is a powerful and simple library for working with the Telegram Bot API in JavaScript. It supports commands, keyboards, inline mode, and much more.
\n\nFor convenience, also install dotenv — it will help store the token in a separate file:
\n\nnpm install dotenv\n\nCreate a .env file in the project root and write your token there:
BOT_TOKEN=your_token_here\n\nDon't forget to add .env to .gitignore if you plan to upload the code.
Writing Your First Telegram Bot in JavaScript
\n\nCreate a file bot.js and open it in the editor. Let's start with the simplest — a bot that responds to the /start command.
require('dotenv').config();\nconst { Telegraf } = require('telegraf');\n\nconst bot = new Telegraf(process.env.BOT_TOKEN);\n\nbot.start((ctx) => {\n ctx.reply('Hello! I am your first bot in JavaScript!');\n});\n\nbot.launch();\nconsole.log('Bot is running!');\n\nRun the bot with the command:
\n\nnode bot.js\n\nOpen your bot in Telegram and click Start. If everything is done correctly, the bot will reply with a greeting. Congratulations — you have just created a working Telegram bot in JavaScript!