Skip to content

Instantly share code, notes, and snippets.

@dmitrydyomin
Last active December 9, 2024 12:26
Show Gist options
  • Save dmitrydyomin/88a68385e92414457b01fe6a5aec4013 to your computer and use it in GitHub Desktop.
Save dmitrydyomin/88a68385e92414457b01fe6a5aec4013 to your computer and use it in GitHub Desktop.
Tail file and send lines to Telegram
#!/bin/bash
# https://stackoverflow.com/a/48779000
# https://dev.to/nathannosudo/bash-script-to-send-a-message-in-telegram-from-your-terminal-20c7
if [ "$#" -ne 1 ]; then
echo "Usage: API_TOKEN=123 CHAT_ID=456 teletail /var/log/test.log"
exit 0
fi
if [ ! -f "$1" ]; then
echo "File $1 not found!"
exit 1
fi
process_new_line() {
local var="" #declare some local variables here
read
while true
do
#process $REPLY
#The new line content is in the variable $REPLY
#store the result in the local variable according to your rules.
curl -s -X POST https://api.telegram.org/bot$API_TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$REPLY"
read
done
}
tail -n0 -f "$1" | process_new_line
@dmitrydyomin
Copy link
Author

systemd service example:

[Service]
ExecStart=/usr/local/bin/teletail /var/log/test.log
Restart=always
User=www-data
Group=www-data

Environment=API_TOKEN=123
Environment=CHAT_ID=123

[Install]
WantedBy=multi-user.target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment