send messages to matrix
parent
1ef59469a0
commit
cfbed43632
11
config.json
11
config.json
|
|
@ -1,11 +1,20 @@
|
|||
{
|
||||
"Interval": 60000000000,
|
||||
"Interval": "4h",
|
||||
"States": [
|
||||
"GA"
|
||||
],
|
||||
"Storage": [
|
||||
"map"
|
||||
],
|
||||
"Message": {
|
||||
"Matrix": {
|
||||
"Homeserver": "https://matrix-client.matrix.org",
|
||||
"Username": "@breellocaldev:matrix.org",
|
||||
"Token": "syt_YnJlZWxsb2NhbGRldg_HTewKMMePdEcLvceAKEz_2fHsHa",
|
||||
"Device": "TGNIOGKATZ",
|
||||
"Room": "!vVwjXhWXMxZtOwexKa:matrix.org"
|
||||
}
|
||||
},
|
||||
"Client": "breellocaldev@gmail.com",
|
||||
"Once": true,
|
||||
"Brokers": {
|
||||
|
|
|
|||
|
|
@ -14,8 +14,17 @@ type Config struct {
|
|||
States []State
|
||||
Storage []string
|
||||
Client string
|
||||
Once bool
|
||||
Brokers struct {
|
||||
Message struct {
|
||||
Matrix struct {
|
||||
Homeserver string
|
||||
Username string
|
||||
Token string
|
||||
Device string
|
||||
Room string
|
||||
}
|
||||
}
|
||||
Once bool
|
||||
Brokers struct {
|
||||
NTG struct {
|
||||
Mock bool
|
||||
Token string
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -33,6 +33,7 @@ require (
|
|||
github.com/json-iterator/go v1.1.9 // indirect
|
||||
github.com/klauspost/compress v1.9.5 // indirect
|
||||
github.com/klauspost/cpuid v1.2.3 // indirect
|
||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16 // indirect
|
||||
github.com/minio/md5-simd v1.1.0 // indirect
|
||||
github.com/minio/minio-go/v6 v6.0.57 // indirect
|
||||
github.com/minio/sha256-simd v0.1.1 // indirect
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -115,6 +115,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
|||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
|
||||
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
|
||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16 h1:ZtO5uywdd5dLDCud4r0r55eP4j9FuUNpl60Gmntcop4=
|
||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
|
||||
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/minio/md5-simd v1.1.0 h1:QPfiOqlZH+Cj9teu0t9b1nTBfPbyTl16Of5MeuShdK4=
|
||||
github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw=
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/truckstop/config"
|
||||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
)
|
||||
|
||||
type Matrix struct {
|
||||
homeserver string
|
||||
username string
|
||||
token string
|
||||
room string
|
||||
}
|
||||
|
||||
func NewMatrix() Matrix {
|
||||
conf := config.Get().Message.Matrix
|
||||
return Matrix{
|
||||
homeserver: conf.Homeserver,
|
||||
username: conf.Username,
|
||||
token: conf.Token,
|
||||
room: conf.Room,
|
||||
}
|
||||
}
|
||||
|
||||
func (m Matrix) client() (*gomatrix.Client, error) {
|
||||
return gomatrix.NewClient(m.homeserver, m.username, m.token)
|
||||
}
|
||||
|
||||
func (m Matrix) Send(text string) error {
|
||||
c, err := m.client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := c.SendText(m.room, text)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("%+v", resp)
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"local/truckstop/config"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMatrixSend(t *testing.T) {
|
||||
if len(os.Getenv("INTEGRATION")) == 0 {
|
||||
t.Skip("$INTEGRATION not set")
|
||||
}
|
||||
var c config.Config
|
||||
b, err := ioutil.ReadFile("../config.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(b, &c); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sender Sender = Matrix{
|
||||
homeserver: c.Message.Matrix.Homeserver,
|
||||
username: c.Message.Matrix.Username,
|
||||
token: c.Message.Matrix.Token,
|
||||
room: c.Message.Matrix.Room,
|
||||
}
|
||||
if err := sender.Send("hello world from unittest"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package message
|
||||
|
||||
type Sender interface {
|
||||
Send(string) error
|
||||
}
|
||||
Loading…
Reference in New Issue