Sending Telegram Messages using Node.js and the Telegram API
Tags: node, webdev, javascript, tutorial
Published: 2024-03-13 12:25:07


## Introduction

[Telegram](https://telegram.org/) is a popular messaging app that allows users to send messages, photos, videos, and other types of media to other Telegram users. Me personally use it almost everyday as a way to communicate with family and friends, in short words I really prefer it to some more popular ones as Viber and Whatsapp. One of the great features of Telegram is that it also has an API that allows developers to interact with Telegram's servers and send messages to Telegram users programmatically so your possibilities are infinity. In this blog post, I will keep it short, we will learn how to send Telegram messages via the Telegram API using Node.js.

## Setting up a Telegram Bot

Before we can start sending messages, we need to set up a Telegram bot. To create a new Telegram bot, we need to talk to the `BotFather` on Telegram. `The BotFather` is a Telegram bot that helps you create and manage your Telegram bots, so its like spawning point for bots. To start a conversation with the `BotFather`, open Telegram and search for `BotFather`. Once you have started a conversation, you can use the following commands to create a new bot:


```
/newbot
/mybots
/myapps
/token
/revoke
.
.
.
```

### What is @BotFather

As we already mentioned, `BotFather` is a bot on Telegram that manages all the bots that you create via your account on Telegram. You can reach him by searching @BotFather on Telegram and you should see this profile. To see what he can do, send /start or /help in the chat with BotFather, and you should see a list of commands that he has

```
/newbot
```

![Chat screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oba951mxo8rzntlcjp9f.png)


It should look something like this  

Follow the instructions provided by the BotFather to set a name and username for your bot. Once you have created your bot, the BotFather will give you a token that you can use to authenticate with Telegram's servers. Make sure to save your bot's token somewhere safe, as you will need it later.

## Installing the node-telegram-bot-api package

To send messages to Telegram users using Node.js, we will use the [node-telegram-bot-api](https://www.npmjs.com/package/node-telegram-bot-api) package. You can install this package by running the following command in your terminal:

```shell
npm install node-telegram-bot-api
```

## Sending a message

Once we have the package installed, we can use the following code to send a message to a Telegram user:

```javascript
const TelegramBot = require('node-telegram-bot-api');
const token = 'YOUR_BOT_TOKEN';
const bot = new TelegramBot(token, {polling: true});

bot.sendMessage('USER_CHAT_ID', 'Hello, this is a message from your Telegram bot.');
```
Make sure to replace <code>YOUR_BOT_TOKEN</code> with your actual bot token, and <code>USER_CHAT_ID</code> with the chat ID of the Telegram user you want to send the message to.

You can also send more complex messages like photos, videos, documents, etc.

## Conclusion

In this blog post, we have learned how to send Telegram messages via the Telegram API using Node.js. We have seen how to create a Telegram bot, install the `node-telegram-bot-api` package, and send messages to Telegram users. Telegram's API is a powerful tool that can be used to create all sorts of interesting projects, from chatbots to automated news bots. By following the steps outlined in this blog post, you should now be able to send messages to Telegram users using Node.js and the Telegram API.

Please do refer [node-telegram-bot-api](https://www.npmjs.com/package/node-telegram-bot-api)  for more details and examples.
Also you can check original post: {% embed https://www.bojanjagetic.com/post/telegram-bot-node %}