Browse Source

refactor: improve alert message formatting and remove unnecessary code

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.
pull/4345/head
Daniel Gibbs 2 years ago
parent
commit
3912d02caa
No known key found for this signature in database GPG Key ID: 2076B128385E8C55
  1. 1
      lgsm/modules/alert_pushbullet.sh
  2. 110
      lgsm/modules/alert_slack.sh
  3. 21
      lgsm/modules/alert_telegram.sh

1
lgsm/modules/alert_pushbullet.sh

@ -36,7 +36,6 @@ else
fi fi
fn_print_dots "Sending Pushbullet alert" 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") 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 if [ -n "${pushbulletsend}" ]; then

110
lgsm/modules/alert_slack.sh

@ -10,54 +10,68 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
json=$( json=$(
cat << EOF cat << EOF
{ {
"attachments": [ "attachments": [
{ {
"color": "#36a64f", "color": "${alertcolourhex}",
"blocks": [ "blocks": [
{ {
"type": "section", "type": "header",
"text": { "text": {
"type": "mrkdwn", "type": "plain_text",
"text": "*LinuxGSM Alert*" "text": "${alerttitle}",
} "emoji": true
}, }
{ },
"type": "section", {
"text": { "type": "divider"
"type": "mrkdwn", },
"text": "*${alertemoji} ${alerttitle}* \n ${alertmessage}" {
} "type": "section",
}, "fields": [
{ {
"type": "divider" "type": "mrkdwn",
}, "text": "*Game*\n${gamename}"
{ },
"type": "section", {
"fields": [ "type": "mrkdwn",
{ "text": "*Server IP*\n\`${alertip}:${port}\`"
"type": "mrkdwn", },
"text": "*Game:* \n ${gamename}" {
}, "type": "mrkdwn",
{ "text": "*Hostname*\n${HOSTNAME}"
"type": "mrkdwn", }
"text": "*Server IP:* \n ${alertip}:${port}" ],
}, "accessory": {
{ "type": "image",
"type": "mrkdwn", "image_url": "${alerticon}",
"text": "*Server Name:* \n ${servername}" "alt_text": "cute cat"
} }
] },
}, {
{ "type": "section",
"type": "section", "text": {
"text": { "type": "mrkdwn",
"type": "mrkdwn", "text": "*Information*\n${alertmessage}"
"text": "Hostname: ${HOSTNAME} / More info: ${alerturl}" }
} },
} {
] "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 EOF
) )

21
lgsm/modules/alert_telegram.sh

@ -7,17 +7,34 @@
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
json=$( jsoninfo=$(
cat << EOF cat << EOF
{ {
"chat_id": "${telegramchatid}", "chat_id": "${telegramchatid}",
"parse_mode": "HTML", "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" "disable_web_page_preview": "yes"
} }
EOF 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" 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") 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")

Loading…
Cancel
Save