diff --git a/config.json b/config.json index 39e47d5..a73b98c 100644 --- a/config.json +++ b/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": { diff --git a/config/config.go b/config/config.go index a896e59..a321db0 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/go.mod b/go.mod index ce61026..047b5a1 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 2709755..5219be9 100644 --- a/go.sum +++ b/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= diff --git a/message/matrix.go b/message/matrix.go new file mode 100644 index 0000000..5386da0 --- /dev/null +++ b/message/matrix.go @@ -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) +} diff --git a/message/matrix_test.go b/message/matrix_test.go new file mode 100644 index 0000000..46e7885 --- /dev/null +++ b/message/matrix_test.go @@ -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) + } +} diff --git a/message/message.go b/message/message.go new file mode 100644 index 0000000..6e674b1 --- /dev/null +++ b/message/message.go @@ -0,0 +1,5 @@ +package message + +type Sender interface { + Send(string) error +}