SendTracked returns a string id for a send message, and Update can be used to change that message
parent
f2c9602d70
commit
d4c1e20230
|
|
@ -87,6 +87,7 @@ func (m *Matrix) Receive() ([]Message, error) {
|
|||
logtr.Debugf("%s => {Start:%s End:%v};; %v, (%d)", m.Continuation(), result.Start, result.End, err, len(result.Chunk))
|
||||
m.continuation = result.End
|
||||
for _, event := range result.Chunk {
|
||||
logtr.Verbosef("matrix event: %+v", event)
|
||||
if _, ok := matrixIDs[event.Sender]; !ok {
|
||||
continue
|
||||
}
|
||||
|
|
@ -124,19 +125,65 @@ func (m *Matrix) Receive() ([]Message, error) {
|
|||
return messages, nil
|
||||
}
|
||||
|
||||
func (m Matrix) Send(text string) error {
|
||||
func (m Matrix) Update(id, text string) error {
|
||||
if m.mock {
|
||||
logtr.Infof("matrix.Send(%s)", text)
|
||||
logtr.Infof("matrix.Update(%s)", text)
|
||||
return nil
|
||||
}
|
||||
c, err := m.getclient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.SendText(m.room, text)
|
||||
type MRelatesTo struct {
|
||||
EventID string `json:"event_id"`
|
||||
RelType string `json:"rel_type"`
|
||||
}
|
||||
type NewContent struct {
|
||||
Body string `json:"body"`
|
||||
MsgType string `json:"msgtype"`
|
||||
}
|
||||
type RelatesToRoomMessage struct {
|
||||
Body string `json:"body"`
|
||||
MsgType string `json:"msgtype"`
|
||||
MRelatesTo MRelatesTo `json:"m.relates_to"`
|
||||
MNewContent NewContent `json:"m.new_content"`
|
||||
}
|
||||
_, err = c.SendMessageEvent(m.room, "m.room.message", RelatesToRoomMessage{
|
||||
Body: "",
|
||||
MsgType: "m.text",
|
||||
MNewContent: NewContent{
|
||||
Body: text,
|
||||
MsgType: "m.text",
|
||||
},
|
||||
MRelatesTo: MRelatesTo{
|
||||
EventID: id,
|
||||
RelType: "m.replace",
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (m Matrix) Send(text string) error {
|
||||
_, err := m.SendTracked(text)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m Matrix) SendTracked(text string) (string, error) {
|
||||
if m.mock {
|
||||
logtr.Infof("matrix.SendTracked(%s)", text)
|
||||
return "", nil
|
||||
}
|
||||
c, err := m.getclient()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := c.SendText(m.room, text)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resp.EventID, nil
|
||||
}
|
||||
|
||||
func (m Matrix) SendImage(uri string) error {
|
||||
if m.mock {
|
||||
logtr.Infof("matrix.SendImage(%s)", uri)
|
||||
|
|
|
|||
|
|
@ -1,57 +1,63 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"local/truckstop/config"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
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,
|
||||
}
|
||||
sender := testMatrix(t)
|
||||
if err := sender.Send("hello world from unittest"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatrixReceive(t *testing.T) {
|
||||
if len(os.Getenv("INTEGRATION")) == 0 {
|
||||
t.Skip("$INTEGRATION not set")
|
||||
}
|
||||
var c config.Config
|
||||
b, err := ioutil.ReadFile("../config.json")
|
||||
func TestMatrixUpdate(t *testing.T) {
|
||||
// 19:32:34: VER: matrix event: {StateKey:<nil> Sender:@bot:m.bltrucks.top Type:m.room.message Timestamp:1642471594729 ID:$n0ln9TFZrko_cBNFXeh8YyICZ3fFm17Jhz3bmZcqig4 RoomID:!OYZqtInrBCn1cyz90D:m.bltrucks.top Redacts: Unsigned:map[transaction_id:go1642471594571852423] Content:map[body:hello world from unittest format: formatted_body: msgtype:m.text] PrevContent:map[]}
|
||||
// func (m Matrix) Update(id, text string) error {
|
||||
m := testMatrix(t)
|
||||
uid := uuid.New().String()[:3]
|
||||
id, err := m.SendTracked("hello, heheheh from test matrix update " + uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(b, &c); err != nil {
|
||||
err = m.Update(id, "heheheh i updated from test matrix update "+uid)
|
||||
if 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,
|
||||
m.Receive()
|
||||
}
|
||||
|
||||
func TestMatrixReceive(t *testing.T) {
|
||||
sender := testMatrix(t)
|
||||
if msgs, err := sender.Receive(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
t.Logf("%+v", msgs)
|
||||
}
|
||||
}
|
||||
|
||||
func testMatrix(t *testing.T) Matrix {
|
||||
if len(os.Getenv("INTEGRATION")) == 0 {
|
||||
t.Skip("$INTEGRATION not set")
|
||||
}
|
||||
d := t.TempDir()
|
||||
f := path.Join(d, "config.test.json")
|
||||
b, err := ioutil.ReadFile("../config.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ioutil.WriteFile(f, b, os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.Setenv("CONFIG", f)
|
||||
if err := config.Refresh(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return NewMatrix()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import "time"
|
|||
|
||||
type Sender interface {
|
||||
Send(string) error
|
||||
SendTracked(string) (string, error)
|
||||
Update(string, string) (string, error)
|
||||
Receive() ([]Message, error)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue