-
Notifications
You must be signed in to change notification settings - Fork 7
/
message.go
255 lines (231 loc) · 6.61 KB
/
message.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package gozulipbot
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// A Message is all of the necessary metadata to post on Zulip.
// It can be either a public message, where Topic is set, or a private message,
// where there is at least one element in Emails.
//
// If the length of Emails is not 0, functions will always assume it is a private message.
type Message struct {
Stream string
Topic string
Emails []string
Content string
}
type EventMessage struct {
AvatarURL string `json:"avatar_url"`
Client string `json:"client"`
Content string `json:"content"`
ContentType string `json:"content_type"`
DisplayRecipient DisplayRecipient `json:"display_recipient"`
GravatarHash string `json:"gravatar_hash"`
ID int `json:"id"`
RecipientID int `json:"recipient_id"`
SenderDomain string `json:"sender_domain"`
SenderEmail string `json:"sender_email"`
SenderFullName string `json:"sender_full_name"`
SenderID int `json:"sender_id"`
SenderShortName string `json:"sender_short_name"`
Subject string `json:"subject"`
SubjectLinks []interface{} `json:"subject_links"`
Timestamp int `json:"timestamp"`
Type string `json:"type"`
Queue *Queue `json:"-"`
}
type DisplayRecipient struct {
Users []User `json:"users,omitempty"`
Topic string `json:"topic,omitempty"`
}
type User struct {
Domain string `json:"domain"`
Email string `json:"email"`
FullName string `json:"full_name"`
ID int `json:"id"`
IsMirrorDummy bool `json:"is_mirror_dummy"`
ShortName string `json:"short_name"`
}
func (d *DisplayRecipient) UnmarshalJSON(b []byte) (err error) {
topic, users := "", make([]User, 1)
if err = json.Unmarshal(b, &topic); err == nil {
d.Topic = topic
return
}
if err = json.Unmarshal(b, &users); err == nil {
d.Users = users
return
}
return
}
// Message posts a message to Zulip. If any emails have been set on the message,
// the message will be re-routed to the PrivateMessage function.
func (b *Bot) Message(m Message) (*http.Response, error) {
if m.Content == "" {
return nil, fmt.Errorf("content cannot be empty")
}
// if any emails are set, this is a private message
if len(m.Emails) != 0 {
return b.PrivateMessage(m)
}
// otherwise it's a stream message
if m.Stream == "" {
return nil, fmt.Errorf("stream cannot be empty")
}
if m.Topic == "" {
return nil, fmt.Errorf("topic cannot be empty")
}
req, err := b.constructMessageRequest(m)
if err != nil {
return nil, err
}
return b.Client.Do(req)
}
// PrivateMessage sends a message to the users in the message email slice.
func (b *Bot) PrivateMessage(m Message) (*http.Response, error) {
if len(m.Emails) == 0 {
return nil, fmt.Errorf("there must be at least one recipient")
}
req, err := b.constructMessageRequest(m)
if err != nil {
return nil, err
}
return b.Client.Do(req)
}
// Respond sends a given message as a response to whatever context from which
// an EventMessage was received.
func (b *Bot) Respond(e EventMessage, response string) (*http.Response, error) {
if response == "" {
return nil, fmt.Errorf("Message response cannot be blank")
}
m := Message{
Stream: e.DisplayRecipient.Topic,
Topic: e.Subject,
Content: response,
}
if m.Topic != "" {
return b.Message(m)
}
// private message
if m.Stream == "" {
emails, err := b.privateResponseList(e)
if err != nil {
return nil, err
}
m.Emails = emails
return b.Message(m)
}
return nil, fmt.Errorf("EventMessage is not understood: %v\n", e)
}
// privateResponseList gets the list of other users in a private multiple
// message conversation.
func (b *Bot) privateResponseList(e EventMessage) ([]string, error) {
var out []string
for _, u := range e.DisplayRecipient.Users {
if u.Email != b.Email {
out = append(out, u.Email)
}
}
if len(out) == 0 {
return nil, fmt.Errorf("EventMessage had no Users within the DisplayRecipient")
}
return out, nil
}
// constructMessageRequest is a helper for simplifying sending a message.
func (b *Bot) constructMessageRequest(m Message) (*http.Request, error) {
to := m.Stream
mtype := "stream"
le := len(m.Emails)
if le != 0 {
mtype = "private"
}
if le == 1 {
to = m.Emails[0]
}
if le > 1 {
to = ""
for i, e := range m.Emails {
to += e
if i != le-1 {
to += ","
}
}
}
values := url.Values{}
values.Set("type", mtype)
values.Set("to", to)
values.Set("content", m.Content)
if mtype == "stream" {
values.Set("subject", m.Topic)
}
return b.constructRequest("POST", "messages", values.Encode())
}
// React adds an emoji reaction to an EventMessage.
func (b *Bot) React(e EventMessage, emoji string) (*http.Response, error) {
requestURL := fmt.Sprintf("messages/%d/reactions", e.ID)
values := url.Values{}
values.Set("emoji_name", emoji)
req, err := b.constructRequest("POST", requestURL, values.Encode())
if err != nil {
return nil, err
}
return b.Client.Do(req)
}
// Unreact removes an emoji reaction from an EventMessage.
func (b *Bot) Unreact(e EventMessage, emoji string) (*http.Response, error) {
requestURL := fmt.Sprintf("messages/%d/reactions", e.ID)
values := url.Values{}
values.Set("emoji_name", emoji)
req, err := b.constructRequest("DELETE", requestURL, values.Encode())
if err != nil {
return nil, err
}
return b.Client.Do(req)
}
type Emoji struct {
Author string `json:"author"`
DisplayURL string `json:"display_url"`
SourceURL string `json:"source_url"`
}
type EmojiResponse struct {
Emoji map[string]*Emoji `json:"emoji"`
Msg string `json:"msg"`
Result string `json:"result"`
}
// RealmEmoji gets the custom emoji information for the Zulip instance.
func (b *Bot) RealmEmoji() (map[string]*Emoji, error) {
req, err := b.constructRequest("GET", "realm/emoji", "")
if err != nil {
return nil, err
}
resp, err := b.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var emjResp EmojiResponse
err = json.Unmarshal(body, &emjResp)
if err != nil {
return nil, err
}
return emjResp.Emoji, nil
}
// RealmEmojiSet makes a set of the names of the custom emoji in the Zulip instance.
func (b *Bot) RealmEmojiSet() (map[string]struct{}, error) {
emj, err := b.RealmEmoji()
if err != nil {
return nil, nil
}
out := map[string]struct{}{}
for k, _ := range emj {
out[k] = struct{}{}
}
return out, nil
}