send messages to matrix
This commit is contained in:
41
message/matrix.go
Normal file
41
message/matrix.go
Normal file
@@ -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)
|
||||
}
|
||||
32
message/matrix_test.go
Normal file
32
message/matrix_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
5
message/message.go
Normal file
5
message/message.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package message
|
||||
|
||||
type Sender interface {
|
||||
Send(string) error
|
||||
}
|
||||
Reference in New Issue
Block a user