-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.go
95 lines (81 loc) · 2.13 KB
/
matrix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
type matrixBot struct {
cli *mautrix.Client
}
func initMatrixBot(cfg configMatrixBot, data *store) (matrixBot, error) {
us := id.UserID(cfg.AccountID)
cli, err := mautrix.NewClient(cfg.Homeserver, us, cfg.Token)
m := matrixBot{cli}
if err != nil {
return m, err
}
syncer := cli.Syncer.(*mautrix.DefaultSyncer)
syncer.OnSync(ignoreOldMessagesSyncHandler)
syncer.OnEventType(event.EventMessage, func(_ mautrix.EventSource, ev *event.Event) {
if ev.Sender == us {
return
}
reply := handleCommand(cli, data, ev)
for _, msg := range reply {
m.sendNotice(ev.RoomID, msg.msg, msg.msgF)
}
})
syncer.OnEventType(event.StateMember, func(_ mautrix.EventSource, ev *event.Event) {
if ev.Sender == us {
return
}
if ev.Content.AsMember().Membership != "invite" {
return
}
fmt.Println("Invite: ", ev)
// TODO: Welcome message
// TODO: Support only 1-1 rooms
resp, err := cli.JoinRoom(ev.RoomID.String(), "", nil)
fmt.Println("JoinRoom response:", resp)
if err != nil {
fmt.Println(err)
}
})
go func() {
backOff := 0
for {
if err := cli.Sync(); err != nil {
fmt.Println("Sync() returned ", err)
sleep := backOff * 2
<-time.After(time.Duration(sleep) * time.Second)
backOff++
continue
}
backOff = 0
}
}()
return m, nil
}
func (m matrixBot) sendNotice(roomID id.RoomID, msg string, msgF string) error {
return m.sendMatrixMessage(roomID, msg, msgF, event.MsgNotice)
}
func (m matrixBot) sendMessage(roomID id.RoomID, msg string, msgF string) error {
return m.sendMatrixMessage(roomID, msg, msgF, event.MsgText)
}
func (m matrixBot) sendMatrixMessage(roomID id.RoomID, msg string, msgF string, eventType event.MessageType) error {
ev := event.MessageEventContent{
MsgType: eventType,
Body: msg,
}
if msgF != "" {
ev.FormattedBody = msgF
ev.Format = event.FormatHTML
}
_, err := m.cli.SendMessageEvent(roomID, event.EventMessage, ev)
return err
}
func ignoreOldMessagesSyncHandler(resp *mautrix.RespSync, since string) bool {
return since != ""
}