Send HTML Message

* Don't send message if it's empty
This commit is contained in:
Locria Cyber 2023-10-07 08:58:43 +00:00
parent e45bf3586f
commit b515907d1a
Signed by: iacore
GPG key ID: F8C16E5157A63006
2 changed files with 22 additions and 6 deletions

View file

@ -5,6 +5,7 @@ import (
"strings"
m "maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
. "maunium.net/go/mautrix/id"
)
@ -56,6 +57,16 @@ func (client *MatrixClient) SendNotice(message string) (*m.RespSendEvent, error)
return client.inner.SendNotice(client.room_id, message)
}
func (client *MatrixClient) SendNoticeHTML(plain string, html string) (*m.RespSendEvent, error) {
return client.inner.SendMessageEvent(client.room_id, event.EventMessage,
&event.MessageEventContent{
MsgType: event.MsgNotice,
Body: plain,
Format: "org.matrix.custom.html",
FormattedBody: html,
})
}
func (client *MatrixClient) RedactEvent(eventId EventID, reason string) (*m.RespSendEvent, error) {
return client.inner.RedactEvent(client.room_id, eventId, m.ReqRedact{Reason: reason})
}

13
main.go
View file

@ -10,6 +10,7 @@ import (
"net/http"
"os"
os_signal "os/signal"
"strings"
"syscall"
"time"
@ -223,7 +224,10 @@ func routineCheck(timeout time.Duration, doNotify bool) error {
}
func notify(updates []scanner.ServiceStatus) error {
message := "```\n"
if len(updates) == 0 {
return nil
}
messageBody := ""
for _, v := range updates {
var upOrDown string
if v.Ok {
@ -232,10 +236,11 @@ func notify(updates []scanner.ServiceStatus) error {
upOrDown = "DOWN"
}
serviceType := fmt.Sprintf("[%s]", v.Type)
message += fmt.Sprintf("%-4s | %-24s %-7s %s\n", upOrDown, v.Name, serviceType, v.Status)
messageBody += fmt.Sprintf("%-4s | %-24s %-7s %s\n", upOrDown, v.Name, serviceType, v.Status)
}
message += "```\n"
_, err := client.SendNotice(message)
_, err := client.SendNoticeHTML(
"```\n"+messageBody+"```",
"<pre><code>"+strings.TrimRight(messageBody, "\n")+"</code></pre>")
log.Printf("notify: err=%v\n%v", err, updates)
return err
}