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.
  • Initiate the bot creation process by sending the “/newbot” command.
  • 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.

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.

Adding Telegram Bot To Group

To send the alert to the group, you must first ensure that the telegram bot you created is added as a member of the group.

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

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 👋'

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.

Alerts and Notifications with Telegram with sample Python code

Example Code (Python)

import requests

def send_telegram_notification(bot_token, chat_id, message):
    url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
    payload = {
        'chat_id': chat_id,
        'text': message
    }
    response = requests.post(url, data=payload)
    if response.status_code == 200:
        print("Notification sent successfully.")
    else:
        print("Failed to send notification.")

# Usage
bot_token = '<-YourBotToken->'
chat_id = '<-YourChatID->'
message = 'Hello, world! 👋'
send_telegram_notification(bot_token, chat_id, message)

This Python script utilizes the requests library to send a notification message to a Telegram chat. The send_telegram_notification function constructs a URL using a Telegram Bot API token (bot_token) and recipient’s chat ID (chat_id). It then sends a message (message) using an HTTP POST request with the payload. If the response status code is 200 (OK), it confirms successful message delivery; otherwise, it indicates failure. To use the script, replace placeholders with your bot token, chat ID, and desired message. This approach offers a straightforward method for integrating Telegram notifications into Python applications.

Link to gist: https://gist.github.com/linuxtechin/8d1dbb396a54cc15038514297c5696d7
Link to Official Doc: https://core.telegram.org/bots
See also: Other usefull Python Scripts

Got any queries or feedback? Feel free to drop a comment below!

Categorized in: