telegramTelegram

Creating a Telegram Bot

To send messages, you first need a Telegram Bot.

1

Open BotFather

Open Telegram and search for @BotFatherarrow-up-right.

2

Create a new bot

Start a chat with BotFather and send /newbot.

3

Name your bot

Follow the instructions to give your bot a name and username.

4

Keep the Bot Token safe

BotFather will provide a Bot Token — keep this secret. You will use it to authenticate API requests.

For more detailed information, see the official Telegram Bot API documentationarrow-up-right.

circle-exclamation

Obtain Chat ID

You have to start a chat with your newly created bot. Once done, there will be a new chat ID associated with your bot. You can extract this code by calling the following endpoint:

https://api.telegram.org/bot{TELEGRAM_TOKEN}/getUpdates

Notification Implementation

Once the bot token and the chat ID are obtained, the API can be pinged to send a message inside that chat.

send_telegram_message.py
import polars as pl
import requests
import os

TELEGRAM_TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]
TELEGRAM_CHAT_ID = "191699299"

def send_telegram_message(message, chat_id):
    url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage?parse_mode=HTML&chat_id={chat_id}&text={message}"
    response = requests.get(url).json()
    print(response)
    return response


def transform():
    text = """
    ⚠️ <b>New Alert!</b>
    """
    response = send_telegram_message(text, TELEGRAM_CHAT_ID)    
    return pl.LazyFrame(response)