Skip to content

Commit 79d0fbb

Browse files
authored
feat: add HTML formatting to Matrix alert messages (#5087)
Matrix alerts are now sent as m.notice messages with an HTML formatted_body: section headings are bold and the "Is my Game Server Online?" and "More info" links are clickable. The plain-text body is kept as the fallback for clients that do not render HTML. All values are escaped with jq's @html before being placed in the HTML, and apostrophes use the numeric ' entity.
1 parent cbc0f74 commit 79d0fbb

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

‎lgsm/modules/alert_matrix.sh‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ matrixroomencoded="$(jq -rn --arg room "${matrixroom}" '$room|@uri')"
1818
matrixtxnid="$(date +%s%N)"
1919
matrixurl="${matrixbase}/_matrix/client/v3/rooms/${matrixroomencoded}/send/m.room.message/${matrixtxnid}"
2020

21+
alerttime="$(date)"
22+
2123
message="${alerttitle}
2224
2325
Server Name
@@ -33,13 +35,14 @@ Server IP
3335
${alertip}:${port}
3436
3537
Server Time
36-
$(date)"
38+
${alerttime}"
3739

3840
if [ -n "${querytype}" ]; then
41+
imgsourl="https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}"
3942
message+="
4043
4144
Is my Game Server Online?
42-
https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}"
45+
${imgsourl}"
4346
fi
4447

4548
if [ -n "${alerturl}" ]; then
@@ -49,7 +52,21 @@ More info
4952
${alerturl}"
5053
fi
5154

52-
json="$(jq -cn --arg body "${message}" '{msgtype: "m.notice", body: $body}')"
55+
json="$(jq -cn \
56+
--arg body "${message}" \
57+
--arg title "${alerttitle}" \
58+
--arg servername "${servername}" \
59+
--arg information "${alertmessage}" \
60+
--arg game "${gamename}" \
61+
--arg serverip "${alertip}:${port}" \
62+
--arg servertime "${alerttime}" \
63+
--arg imgsourl "${imgsourl}" \
64+
--arg alerturl "${alerturl}" \
65+
'(@html "<strong>\($title)</strong><br><br><strong>Server Name</strong><br>\($servername)<br><br><strong>Information</strong><br>\($information)<br><br><strong>Game</strong><br>\($game)<br><br><strong>Server IP</strong><br>\($serverip)<br><br><strong>Server Time</strong><br>\($servertime)")
66+
+ (if $imgsourl != "" then @html "<br><br><strong>Is my Game Server Online?</strong><br><a href=\"\($imgsourl)\">Check here</a>" else "" end)
67+
+ (if $alerturl != "" then @html "<br><br><strong>More info</strong><br><a href=\"\($alerturl)\">\($alerturl)</a>" else "" end)
68+
| gsub("&apos;"; "&#39;")
69+
| {msgtype: "m.notice", body: $body, format: "org.matrix.custom.html", formatted_body: .}')"
5370

5471
fn_print_dots "Sending Matrix alert"
5572

0 commit comments

Comments
 (0)