Konstantinos Antoniou.

← Projects

Automating Tasks with the Telegram API and BotFather

Overview

In this project, I explored how to use the Telegram API and BotFather to automate notifications and alerts for my various projects. I wanted a solution that would allow me to get real-time updates directly on my phone or desktop, without relying on emails or third-party apps. Here’s how I approached it.


Step 1: Creating a Bot with BotFather

The first step was to create a Telegram bot. Telegram provides BotFather, a dedicated bot that helps you create and manage other bots.

  1. I opened Telegram and searched for BotFather.
  2. I started a chat with BotFather and sent the command /newbot.
  3. BotFather asked for a name and username for my bot:
    • Name: MyAutomationBot
    • Username: MyAutomationBotBot (must end with bot)
  4. After successful creation, BotFather provided me with an API token. This token is a secret key that allows my scripts to communicate with Telegram through this bot.

⚠️ Tip: Keep your API token secure. Anyone with access to it can send messages as your bot.


Step 2: Setting Up the Python Environment

I chose Python for automating notifications because of its rich ecosystem and ease of use.

# Create a virtual environment python3 -m venv telegram-env source telegram-env/bin/activate

# Install required packages pip install python-telegram-bot requests


Step 3: Sending a Simple Message

With the token from BotFather, I was able to send my first test message.

import asyncio from telegram import Bot

async def main(): bot = Bot(token="") await bot.send_message(chat_id=, text=“Hello! Test message from automation bot.”)

asyncio.run(main())

To get your CHAT_ID, I used the @userinfobot on Telegram. This is a quick way to get the ID where the bot will send messages.

Screenshot 1: Step 3: Sending a Simple Message


Step 4: Integrating Telegram into Automations

Once I confirmed that messages were being delivered, I integrated the bot into my automation scripts. For example:

  • Docker Updates: I automated notifications whenever a container was updated.
  • Service Status Checks: My Python scripts could send real-time alerts if a service went down.
  • Home Lab Alerts: Temperature, disk usage, or backup completions were all sent as Telegram messages.

import requests

def send_telegram_message(message): token = “YOUR_API_TOKEN” chat_id = “YOUR_CHAT_ID” url = f”https://api.telegram.org/bot{token}/sendMessage” payload = {“chat_id”: chat_id, “text”: message} requests.post(url, data=payload)

# Example usage send_telegram_message(“Docker container ‘nginx’ has been updated successfully ✅”)

Using requests makes it easy to send messages without relying on external libraries, which is useful for lightweight scripts.

Screenshot 2: Step 4: Integrating Telegram into Automations


Step 5: Advanced Features

After getting basic messages working, I explored more advanced capabilities:

  1. Inline Buttons: My bot could provide buttons to trigger certain actions.
  2. File and Image Notifications: I sent log files or screenshots directly from my scripts.
import asyncio
from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup

keyboard = [[InlineKeyboardButton("Restart Service", callback_data='restart')]]
markup = InlineKeyboardMarkup(keyboard)

async def main():
    bot = Bot(token="")
    await bot.send_message(
        chat_id=,
        text="Service alert!",
        reply_markup=markup
    )

asyncio.run(main())

Screenshot 3: Step 5: Advanced Features


Step 6: Lessons Learned

  • Telegram bots are extremely flexible for personal and team automation.
  • BotFather makes creation and management simple, and the API is very accessible.
  • Integrating Telegram notifications into scripts significantly improved monitoring efficiency for my home lab and DevOps projects.

Conclusion

Through this project, I built a Telegram bot that acts as a personal automation assistant. From Docker updates to system monitoring, my bot now delivers important alerts directly to me. This hands-on experience strengthened my Python skills and taught me how to leverage APIs for real-world automation.