forked from gigawhitlocks/marvin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (107 loc) · 2.54 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bytes"
"encoding/json"
"flag"
"io"
"net/http"
)
var (
password string
username string
server string
)
type logonData struct {
UserId string
AuthToken string
}
type result struct {
Status string
}
type logonResponse struct {
Status string
Data logonData
}
type Session struct {
logonData
server string
}
type Message struct {
Contents string `json:"msg"`
}
func init() {
flag.StringVar(&username, "username", "bot", "bot's username")
flag.StringVar(&password, "password", "password", "bot's password")
flag.StringVar(&server, "server", "https://localhost:3000", "server url")
flag.Parse()
}
func (s *Session) Logon(username, password, server string) {
s.server = server
b := new(bytes.Buffer)
b.WriteString("user=")
b.WriteString(username)
b.WriteString("&password=")
b.WriteString(password)
resp, _ := http.Post(server+"/api/login",
"application/x-www-form-urlencoded",
b)
var l logonResponse
err := json.NewDecoder(resp.Body).Decode(&l)
if err != nil {
panic("Probably not the right username/password/server")
}
s.UserId = l.Data.UserId
s.AuthToken = l.Data.AuthToken
}
func (s *Session) authRequest(url string) (*http.Request, *http.Client) {
client := &http.Client{}
b := new(bytes.Buffer)
req, _ := http.NewRequest("POST", url, b)
req.Header.Add("X-Auth-Token", s.AuthToken)
req.Header.Add("X-User-Id", s.UserId)
return req, client
}
func checkResult(body io.Reader) {
r := &result{}
err := json.NewDecoder(body).Decode(&r)
if err != nil {
panic(err)
}
if r.Status != "success" {
panic(r.Status)
}
}
func (s *Session) JoinChannel(channelName string) {
req, client := s.authRequest(s.server + "/api/rooms/" + channelName + "/join")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
checkResult(resp.Body)
}
func (s *Session) Say(channelName, message string) {
client := &http.Client{}
b := new(bytes.Buffer)
b.WriteString("{\"msg\": \"" + message + "\"}")
req, _ := http.NewRequest("POST",
s.server+"/api/rooms/"+channelName+"/send", b)
req.Header.Add("X-Auth-Token", s.AuthToken)
req.Header.Add("X-User-Id", s.UserId)
req.Header.Add("Content-Type", "application/json")
client.Do(req)
}
func (s *Session) LeaveChannel(channelName string) {
req, client := s.authRequest(s.server + "/api/rooms/" + channelName + "/leave")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
checkResult(resp.Body)
}
func main() {
s := &Session{}
s.Logon(username, password, server)
s.JoinChannel("GENERAL")
s.Say("GENERAL", "test")
s.LeaveChannel("GENERAL")
}