Telegram Alert Bot

Background

I wanted to create a simple stand alone service that moitors log files and when a log entry matches a regex, it sends an alert to a Telegram channel. I wanted the app to:

  • have no dependencies on anything else - pure stand alone
  • no silly agents or remote networks access (it just needs telegram)
  • it needs to auto restart, and sit in the background and be configurable
  • check when Linux rolls over logs files to ensure its always monitoring the latest version of the log.

...and tele_alert does this!

I used these libraries:

  • https://github.com/python-telegram-bot/python-telegram-bot
  • https://github.com/kasun/python-tail/
  • asyncio for threating (pip install asyncio)
  • pyinstaller to create the exec (pip install pyinstaller)

If you'd like to know more about this app, you can reach me at one@roguemission.com

Generate Telegram Tokens

To create a telegram API_TOKEN, open telegram, go to the BotFather and issue /newbot command and follow the instructions.

To get the CHANNEL_ID, post a message to the bot, and in a terminal type:

curl https://api.telegram.org/bot<TOKEN>/getUpdates

In the returned JSON, "chat":{"id":<CHANNEL_ID> will give the channel id.

Create a Linux Daemon

In /etc/systemd/system/tele_alert.service:

[Unit]
Description=Telegram Alerting Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/etc/tele_alert/
ExecStart=/etc/tele_alert/tele

[Install]
WantedBy=multi-user.target

And then:

sudo systemctl daemon-reload
sudo systemctl enable tele_alert.service
sudo systemctl start tele_alert.service