-
-
Save ashleymavericks/375f66b6d35ba222855ed5d207138771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BOT_TOKEN="<token>" | |
CHAT_ID="<id>" | |
TELEGRAM_USERNAME="<username>" | |
send_telegram_message() { | |
local message="$1" | |
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \ | |
-d chat_id="$CHAT_ID" \ | |
-d text="$message" \ | |
-d parse_mode="HTML" | |
} | |
# https://core.telegram.org/bots/api#formatting-options | |
escape_special_chars() { | |
local string="$1" | |
echo "$string" | sed 's/[_*[\]()~`>#+=|{}.-]/\\&/g' | |
} | |
process_log_line() { | |
local line="$1" | |
if echo "$line" | grep -q "level=ERROR"; then | |
local timestamp=$(echo "$line" | grep -oP 'time=\K[^ ]+') | |
local error_msg=$(echo "$line" | sed -E 's/.*msg=("([^"]*)"|([^ ]*)).*/\2\3/') | |
local error_details=$(echo "$line" | sed -E 's/.*error=("([^"]*)"|([^ ]*)).*/\2\3/') | |
error_msg=$(escape_special_chars "$error_msg") | |
error_details=$(escape_special_chars "$error_details") | |
local message="⚠️ <b>Error Alert</b> ⚠️ | |
<b>Time:</b> $timestamp | |
<b>Message:</b> $error_msg | |
<b>Error:</b> $error_details | |
@$TELEGRAM_USERNAME" | |
send_telegram_message "$message" | |
fi | |
} | |
journalctl -u ykbot.service -f -n 0 | while read -r line; do | |
process_log_line "$line" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment