Skip to content

Commit

Permalink
refactor: improve alert message formatting and remove unnecessary code
Browse files Browse the repository at this point in the history
The commit refactors the alert message formatting in the `alert_pushbullet.sh`, `alert_slack.sh`, and `alert_telegram.sh` files. It improves the structure and readability of the messages by using proper markdown syntax. Additionally, it removes unnecessary code from the files to simplify their implementation.
  • Loading branch information
dgibbs64 committed Oct 17, 2023
1 parent b127005 commit 3912d02
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 51 deletions.
1 change: 0 additions & 1 deletion lgsm/modules/alert_pushbullet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ else
fi

fn_print_dots "Sending Pushbullet alert"

pushbulletsend=$(curl --connect-timeout 10 -sSL -H "Access-Token: ${pushbullettoken}" -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "https://api.pushbullet.com/v2/pushes" | grep "error_code")

if [ -n "${pushbulletsend}" ]; then
Expand Down
110 changes: 62 additions & 48 deletions lgsm/modules/alert_slack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,68 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
json=$(
cat << EOF
{
"attachments": [
{
"color": "#36a64f",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*LinuxGSM Alert*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*${alertemoji} ${alerttitle}* \n ${alertmessage}"
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Game:* \n ${gamename}"
},
{
"type": "mrkdwn",
"text": "*Server IP:* \n ${alertip}:${port}"
},
{
"type": "mrkdwn",
"text": "*Server Name:* \n ${servername}"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hostname: ${HOSTNAME} / More info: ${alerturl}"
}
}
]
}
]
"attachments": [
{
"color": "${alertcolourhex}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${alerttitle}",
"emoji": true
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Game*\n${gamename}"
},
{
"type": "mrkdwn",
"text": "*Server IP*\n\`${alertip}:${port}\`"
},
{
"type": "mrkdwn",
"text": "*Hostname*\n${HOSTNAME}"
}
],
"accessory": {
"type": "image",
"image_url": "${alerticon}",
"alt_text": "cute cat"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Information*\n${alertmessage}"
}
},
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
"alt_text": "LinuxGSM icon"
},
{
"type": "plain_text",
"text": "Sent by LinuxGSM ${version}",
"emoji": true
}
]
}
]
}
]
}
EOF
)
Expand Down
21 changes: 19 additions & 2 deletions lgsm/modules/alert_telegram.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,34 @@

moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"

json=$(
jsoninfo=$(
cat << EOF
{
"chat_id": "${telegramchatid}",
"parse_mode": "HTML",
"text": "<b>${alertemoji} ${alerttitle} ${alertemoji}</b>\n\n<b>Server name</b>\n${servername}\n\n<b>Message</b>\n${alertmessage}\n\n<b>Game</b>\n${gamename}\n\n<b>Server IP</b>\n<a href='https://www.gametracker.com/server_info/${alertip}:${port}'>${alertip}:${port}</a>\n\n<b>Hostname</b>\n${HOSTNAME}\n\n<b>More info</b>\n<a href='${alerturl}'>${alerturl}</a>",
"text": "<b>${alerttitle}</b>\n\n<b>Server name</b>\n${servername}\n\n<b>Information</b>\n${alertmessage}\n\n<b>Game</b>\n${gamename}\n\n<b>Server IP</b>\n${alertip}:${port}\n\n<b>Hostname</b>\n${HOSTNAME}\n\n<b>More info</b>\n<a href='${alerturl}'>${alerturl}</a>",
"disable_web_page_preview": "yes"
}
EOF
)

jsonnoinfo=$(
cat << EOF
{
"chat_id": "${telegramchatid}",
"parse_mode": "HTML",
"text": "<b>${alerttitle}</b>\n\n<b>Server name</b>\n${servername}\n\n<b>Information</b>\n${alertmessage}\n\n<b>Game</b>\n${gamename}\n\n<b>Server IP</b>\n${alertip}:${port}\n\n<b>Hostname</b>\n${HOSTNAME}\n\n<b>More info</b>\n<a href='${alerturl}'>${alerturl}</a>",
"disable_web_page_preview": "yes"
}
EOF
)

if [ -z "${alerturl}" ]; then
json="${jsonnoinfo}"
else
json="${jsoninfo}"
fi

fn_print_dots "Sending Telegram alert"
telegramsend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" ${curlcustomstring} "https://${telegramapi}/bot${telegramtoken}/sendMessage" | grep "error_code")

Expand Down

0 comments on commit 3912d02

Please sign in to comment.