How to send Alerts and Notifications with Telegram
Tags: devops, monitoring, sre, python
Published: 2024-04-16 17:13:00

## Understanding Telegram Bot API
For sending Alerts and Notifications with Telegram we utilize Telegram Bot API which supports programmatic interaction between developers and the Telegram platform. Create a Telegram bot using BotFather, the official bot for managing Telegram bots, to get started. Let us closely look into this process detailed.

## Creating a Telegram Bot

- 
Start a conversation with BotFather on Telegram by searching for “@BotFather” in the search bar.

![conversation with BotFather on Telegram
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1vbw0xghmnhm7r04x6q.png)


![Click on start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/puqow3iafpmy1cei4muk.png)
- 
Initiate the bot creation process by sending the “/newbot” command.


![select /newbot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dj7nk22vqrvl6usvw2u2.png)

- 
Follow BotFather’s instructions to set a name and username for your bot. Once created, BotFather will provide you with a unique API token, which you’ll need to access the Telegram Bot API.

![your api key generated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/seh30yv5mgmwz6al6q0e.png)

## Obtaining Chat IDs
To send notifications to specific users or groups, you’ll need their unique chat IDs.

## Get Chat ID for a Private Chat

- 
Search and open your Telegram bot in the Telegram app.

- 
Click “Start” or send a message to initiate a conversation with the bot.

- 
Open the following URL in a web browser: `https://api.telegram.org/bot<your_bot_token>/getUpdates`

- 
Replace `<your_bot_token>` with your actual bot token.

- 
You’ll receive a JSON response containing information about recent updates, including the chat ID of the conversation.

![Telegram Get Chat ID for a Private Chat
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1391u57ehq07khn0hbtj.png)

## Get Chat ID for a Group Chat

- 
Add your Telegram bot to a group chat where you want to obtain the chat ID.

- 
Send a message to the group chat.

- 
Open the following URL in a web browser: `https://api.telegram.org/bot<your_bot_token>/getUpdates`

- 
Replace `<your_bot_token>` with your actual bot token.

Following these steps should allow you to obtain the chat ID for both private and group chats on Telegram.

![Telegram Get Chat ID for a Group Chat
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8umbep98fz0q9rlundv.png)

## 
Sending Alerts and Notifications:
To send a text message, you can use a programming language that can handle HTTP requests, like Python or JavaScript.
**Here is a simple cURL request**

Replace `<YourBotToken>` with your actual bot token and `<CHAT_ID>` with the extracted chat ID.

```
curl -X POST "https://api.telegram.org/bot<YourBotToken>/sendMessage" \
-d 'chat_id=<CHAT_ID>' -d 'text=Hello, world 👋'
```

![Text notification⁄alert received using Telegram Api
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82mxl0lps3vgmtavf8zx.png)

## Conclusion
It’s never been easier to send quick messages and alerts with Telegram’s powerful Bot API. If you follow the steps in this article, you’ll be able to add notification features to your apps or systems without any problems. This will allow you to keep your users updated and engaged in real time.

In addition, the Telegram Bot API can be used for more than just chatting. You can also use it to connect to monitoring tools like Prometheus AlertManager or any other tool you choose. This integration lets you get immediate alerts and notifications about critical events, so you can control your infrastructure actively and respond quickly.


**_Ready to implement Telegram alerts in Python?_**  
Explore detailed steps and sample code on [LinuxTech: How to Send Alerts and Notifications with Telegram](https://linuxtech.in/how-to-send-alerts-and-notifications-with-telegram/).  
**Start Sending Notifications with Telegram Programmatically!**