Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions alert/matrix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package alert

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"

blackfriday "gopkg.in/russross/blackfriday.v2"
)

// Matrix alert container.
type Matrix struct {
client *http.Client
token string
room string
}

// NewMatrix returns a Matrix alerter from the private token
// and the room identifier, both available in the Riot web UI.
func NewMatrix(token string, room string) *Matrix {
return &Matrix{
client: &http.Client{},
token: token,
room: room,
}
}

// Alert sends a message to the matrix room.
func (p *Matrix) Alert(title, body string) {
data := "☢️ **" + title + "**: " + body
markdownBody := blackfriday.Run(
[]byte(data),
blackfriday.WithExtensions(blackfriday.HardLineBreak|blackfriday.NoEmptyLineBeforeBlock),
)
raw, _ := json.Marshal(map[string]string{
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"formatted_body": string(markdownBody),
"body": data,
})

url := fmt.Sprintf(
"https://matrix.org/_matrix/client/r0/rooms/%s/send/m.room.message/%d?access_token=%s",
url.PathEscape(p.room),
time.Now().UnixNano(),
p.token,
)

req, _ := http.NewRequest("PUT", url, bytes.NewReader(raw))
req.Header.Set("Content-Type", "application/json")

_, _ = p.client.Do(req)
}
5 changes: 4 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/Lesterpig/board/alert"
"github.com/Lesterpig/board/probe"

"github.com/spf13/viper"
)

Expand All @@ -20,6 +19,7 @@ type serviceConfig struct {
type alertConfig struct {
Type string
Token string
Dest string
}

var probeConstructors = map[string](func() probe.Prober){
Expand All @@ -34,6 +34,9 @@ var alertConstructors = map[string](func(c alertConfig) alert.Alerter){
"pushbullet": func(c alertConfig) alert.Alerter {
return alert.NewPushbullet(c.Token)
},
"matrix": func(c alertConfig) alert.Alerter {
return alert.NewMatrix(c.Token, c.Dest)
},
}

var alerters []alert.Alerter
Expand Down
3 changes: 3 additions & 0 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ services:
alerts:
- type: pushbullet
token: <your token here>
- type: matrix
token: <emitter private token>
dest: <room identifier>
# ...