Send HTML Message
* Don't send message if it's empty
This commit is contained in:
parent
e45bf3586f
commit
b515907d1a
2 changed files with 22 additions and 6 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
m "maunium.net/go/mautrix"
|
m "maunium.net/go/mautrix"
|
||||||
|
"maunium.net/go/mautrix/event"
|
||||||
. "maunium.net/go/mautrix/id"
|
. "maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -46,8 +47,8 @@ func NewMatrixClient(config MatrixConfig) (*MatrixClient, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MatrixClient{
|
return &MatrixClient{
|
||||||
inner: *client,
|
inner: *client,
|
||||||
config: config,
|
config: config,
|
||||||
room_id: opaque_room_id,
|
room_id: opaque_room_id,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -56,6 +57,16 @@ func (client *MatrixClient) SendNotice(message string) (*m.RespSendEvent, error)
|
||||||
return client.inner.SendNotice(client.room_id, message)
|
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) {
|
func (client *MatrixClient) RedactEvent(eventId EventID, reason string) (*m.RespSendEvent, error) {
|
||||||
return client.inner.RedactEvent(client.room_id, eventId, m.ReqRedact{Reason: reason})
|
return client.inner.RedactEvent(client.room_id, eventId, m.ReqRedact{Reason: reason})
|
||||||
}
|
}
|
||||||
|
|
13
main.go
13
main.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
os_signal "os/signal"
|
os_signal "os/signal"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -223,7 +224,10 @@ func routineCheck(timeout time.Duration, doNotify bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func notify(updates []scanner.ServiceStatus) error {
|
func notify(updates []scanner.ServiceStatus) error {
|
||||||
message := "```\n"
|
if len(updates) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
messageBody := ""
|
||||||
for _, v := range updates {
|
for _, v := range updates {
|
||||||
var upOrDown string
|
var upOrDown string
|
||||||
if v.Ok {
|
if v.Ok {
|
||||||
|
@ -232,10 +236,11 @@ func notify(updates []scanner.ServiceStatus) error {
|
||||||
upOrDown = "DOWN"
|
upOrDown = "DOWN"
|
||||||
}
|
}
|
||||||
serviceType := fmt.Sprintf("[%s]", v.Type)
|
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.SendNoticeHTML(
|
||||||
_, err := client.SendNotice(message)
|
"```\n"+messageBody+"```",
|
||||||
|
"<pre><code>"+strings.TrimRight(messageBody, "\n")+"</code></pre>")
|
||||||
log.Printf("notify: err=%v\n%v", err, updates)
|
log.Printf("notify: err=%v\n%v", err, updates)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue