telegram bot typescript

Telegram Bot in Typescript

Learn how to create a bot on Telegram, add command listening to it, and configure notification sending.

Daniel Gustaw

Daniel Gustaw

• 3 min read

Telegram Bot in Typescript

One of the projects we released last month used Telegram to send notifications. This entry will show you how to configure notification sending from scratch using typescript.

Preparing the environment

We will start by preparing the configuration files:

tsc --init
npm init -y

We install telebot - a package that provides the Telegram SDK. Its documentation can be found here:

mullwar/telebot

Along with it, we install the necessary set of libraries for typescript:

npm i telebot @types/telebot @types/node typescript ts-node

In the tsconfig.json file, we overwrite the following options:

"target": "ESNEXT",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,

Obtaining an API TOKEN

To be able to use the API, we will need a token. The simplest way to obtain it is to write to the bot that creates bots on Telegram. This is BotFather.

The whole conversation consists of providing the command to create a bot, its name, username, and receiving the token.

Adding a token to the project

I recommend adding TELEGRAM_TOKEN with this value to the .env file, e.g.

TELEGRAM_TOKEN=xxx

In package.json we add a line

    "start": "ts-node index.ts",

inside scripts. We create a Makefile file

include .env
export

node_modules: package.json
	npm i

up: node_modules
	npm run start

thanks to it we automatically import .env and we do not need to use flags from the command line or packages like dotenv. Don’t forget to add .env to .gitignore.

Code to get chat ID

If we want our bot to respond, we just need to write the code in the index.ts file:

import TeleBot from "telebot"

const bot = new TeleBot({
    token: process.env.TELEGRAM_TOKEN || '',
});

bot.on(["/configure_bot"], (msg) => {
    console.log(msg);
    bot.sendMessage(msg.chat.id, `CHAT_ID: ${msg.chat.id}`);
});

bot.start();

Next, add the bot to the chat and write to it. The result will be as follows:

The chat identifier is a key piece of information if we want to send notifications to it. The chat ID and Token are key pieces of information indicating which bot is writing and where. In our case, the bot was set up to send different data to different groups, so we had to repeat this command for several groups and note them down in the .env file. We add the line to .env

GROUP_LOG_ID=-506870285

Sending signals

We will send a random number to the channel every second if it is greater than 0.5. The following code is sufficient for this.

setInterval(() => {
    const rand = Math.random();
    if (rand > .5) {
        bot.sendMessage(parseInt(process.env.GROUP_LOG_ID || ''), `${rand}`)
    }
}, 1000)

It was a very simple code and a very simple bot. With such bots, you can do practical things. For example:

Other articles

You can find interesting also.