🤖 Build a Personal Telegram Notification Bot with Python (Minecraft‑Ready)

Featured post image
Random image for inspiration

🤖 Build a Personal Telegram Notification Bot with Python (Minecraft‑Ready)

Creating a personal notification system with the Telegram Bot API and Python lets you receive instant alerts on your phone for any event you choose to monitor. By leveraging Telegram’s reliable push infrastructure, you avoid the need for separate apps and can customize messages with emojis, links, or formatted text.

When applied to a Minecraft server, this setup can inform you of player joins, server crashes, or resource‑usage spikes in real time, helping administrators stay on top of gameplay without constantly checking the console. The same pattern works for any script or service, making it a versatile tool for hobbyists and developers alike.

1. Create the Telegram Bot and Get Your API Token

Start by opening a chat with BotFather on Telegram and sending the /newbot command; follow the prompts to choose a name and username, after which BotFather will give you an HTTP API token that looks like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.

Save this token securely; you will use it in your Python code with the python‑telegram‑bot library to send messages. Install the library via pip install python‑telegram‑bot and test connectivity by sending a simple hello message to your own chat ID.

import os from telegram import Bot TOKEN = os.getenv('TELEGRAM_TOKEN') CHAT_ID = os.getenv('TELEGRAM_CHAT_ID') bot = Bot(token=TOKEN) async def send_test(): await bot.send_message(chat_id=CHAT_ID, text='🔔 Test notification from your Python bot!') # Run with asyncio import asyncio asyncio.run(send_test())

2. Monitor Minecraft Server Events and Trigger Alerts

If you run a Minecraft server on Linux, you can tail the latest.log file in the world folder; each line contains timestamps and events such as player joins, deaths, or server warnings. A Python script can watch this file for new lines and forward relevant ones to Telegram.

For example, when a line contains '[INFO] Player joined the game', the script extracts the player name and sends a formatted message like '🟢 Player Steve just joined the server!'. This gives you real‑time awareness without needing to stare at the console.

import time import os from telegram import Bot TOKEN = os.getenv('TELEGRAM_TOKEN') CHAT_ID = os.getenv('TELEGRAM_CHAT_ID') bot = Bot(token=TOKEN) LOG_PATH = '/home/minecraft/server/logs/latest.log' def follow(thefile): thefile.seek(0, os.SEEK_END) while True: line = thefile.readline() if not line: time.sleep(0.5) continue yield line with open(LOG_PATH, 'r') as f: for line in follow(f): if '[INFO]' in line and 'joined the game' in line: player = line.split(']')[2].strip().split()[0] msg = f'🟢 Player {player} just joined the server!' bot.send_message(chat_id=CHAT_ID, text=msg)

3. Run the Notifier as a Reliable Background Service

To keep the script running even after you log out, wrap it in a systemd service on Linux. Create a file /etc/systemd/system/minecraft-notifier.service with appropriate ExecStart, WorkingDirectory, and environment variables for the token and chat ID.

Enable and start the service with sudo systemctl enable minecraft-notifier && sudo systemctl start minecraft-notifier; you can then check its status via journalctl -u minecraft-notifier -f to see logs and ensure it restarts automatically on failure, giving you a hassle‑free, 24/7 notification pipeline.

[Unit] Description=Minecraft Telegram Notifier After=network.target [Service] Type=simple User=minecraft WorkingDirectory=/home/minecraft/notifier Environment=TELEGRAM_TOKEN=your_token_here Environment=TELEGRAM_CHAT_ID=your_chat_id_here ExecStart=/usr/bin/python3 /home/minecraft/notifier/notifier.py Restart=on-failure [Install] WantedBy=multi-user.target

Wrap Up and Next Steps

With these three steps—creating the bot, linking it to Minecraft server events, and running the script as a service—you now have a lightweight, customizable alert system that pushes important game‑world happenings straight to your phone. Feel free to extend the script to monitor CPU usage, disk space, or even in‑game achievements by parsing different log patterns.

Experiment with adding inline keyboards or sending screenshots via the Telegram Bot API to make your notifications richer, and consider deploying the same pattern to other projects like CI/CD pipelines or home‑automation sensors for a unified personal notification hub.

🚀 Share files with us securely! : www.simpledrop.net

Post a Comment

Previous Post Next Post