-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
72 lines (58 loc) · 1.77 KB
/
main.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
package main
import (
"os"
"github.com/matrix-org/gomatrix"
"github.com/russross/blackfriday/v2"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("matrix-cli", "Command line client for the [matrix] decentralized communication network.")
serverURL = app.Flag("server-url", "URL to the matrix server").Default("https://matrix.org").String()
username = app.Flag("username", "matrix username").Required().String()
password = app.Flag("password", "matrix password").Required().String()
send = app.Command("send", "Send a message to a matrix room")
sendRoom = send.Arg("room", "matrix room").Required().String()
sendMessages = send.Arg("message", "Message to send to the matrix room").Required().Strings()
)
func main() {
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
client, err := gomatrix.NewClient(*serverURL, "", "")
if err != nil {
panic(err)
}
login(client, username, password)
switch cmd {
case "send":
sendMsgs(client, *sendRoom, *sendMessages)
}
}
func login(client *gomatrix.Client, username, password *string) {
loginResp, err := client.Login(&gomatrix.ReqLogin{
Type: "m.login.password",
User: *username,
Password: *password,
})
if err != nil {
panic(err)
}
client.SetCredentials(loginResp.UserID, loginResp.AccessToken)
}
func sendMsgs(client *gomatrix.Client, room string, messages []string) {
resp, err := client.JoinRoom(room, "", nil)
if err != nil {
panic(err)
}
for _, msg := range messages {
formatted := blackfriday.Run([]byte(msg))
message := gomatrix.HTMLMessage {
Body: msg,
MsgType: "m.text",
Format: "org.matrix.custom.html",
FormattedBody: string(formatted),
}
_, err := client.SendMessageEvent(resp.RoomID, "m.room.message", message)
if err != nil {
panic(err)
}
}
}