File size: 6,209 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package mockslack

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"strconv"
	"strings"
	"time"
)

// ChatPostMessageOptions are parameters for a `chat.postMessage` call.
type ChatPostMessageOptions struct {
	ChannelID string
	Text      string
	Color     string

	Actions []Action

	AsUser bool

	User string

	UpdateTS  string
	ThreadTS  string
	Broadcast bool
}

func (ch *channelState) nextTS() string {
	t := time.Now()
	if !t.After(ch.TS) {
		t = ch.TS.Add(1)
	}
	ch.TS = t

	return strconv.FormatFloat(time.Duration(t.UnixNano()).Seconds(), 'f', -1, 64)
}

// ChatPostMessage posts a message to a channel.
func (st *API) ChatPostMessage(ctx context.Context, opts ChatPostMessageOptions) (*Message, error) {
	var err error
	var user string
	if opts.AsUser {
		err = checkPermission(ctx, "chat:write:user")
		user = userID(ctx)
	} else {
		err = checkPermission(ctx, "bot", "chat:write:bot")
		user = botID(ctx)
	}
	if err != nil {
		return nil, err
	}

	if len(opts.Text) > 40000 {
		return nil, &response{Err: "msg_too_long"}
	}

	st.mx.Lock()
	defer st.mx.Unlock()

	ch := st.channels[opts.ChannelID]
	if ch == nil && strings.HasPrefix(opts.ChannelID, "W") {
		// We need to create a new "channel" for the DM conversation.
		u, ok := st.users[opts.ChannelID]
		if !ok {
			return nil, &response{Err: "user_not_found"}
		}

		ch = &channelState{
			Channel: Channel{
				ID:        opts.ChannelID,
				Name:      "DM:" + u.Name,
				IsChannel: true,
			},
			Users: []string{userID(ctx)},
		}
		st.channels[opts.ChannelID] = ch
	}

	if ch == nil {
		if !st.flags.autoCreateChannel && !strings.HasPrefix(opts.ChannelID, "W") {
			return nil, &response{Err: "channel_not_found"}
		}

		// auto create channel
		ch = &channelState{Channel: Channel{
			ID:        opts.ChannelID,
			Name:      cleanChannelName(opts.ChannelID),
			IsChannel: true,
		}}
		if opts.AsUser {
			// add the user if needed
			ch.Users = append(ch.Users, userID(ctx))
		}

		st.channels[opts.ChannelID] = ch
	}

	if opts.AsUser && !contains(ch.Users, userID(ctx)) {
		return nil, &response{Err: "not_in_channel"}
	}

	if ch.IsArchived {
		return nil, &response{Err: "is_archived"}
	}

	msg := &Message{
		TS:    ch.nextTS(),
		Text:  opts.Text,
		User:  user,
		Color: opts.Color,

		ChannelID: opts.ChannelID,
		ToUserID:  opts.User,

		Actions: opts.Actions,

		UpdateTS: opts.UpdateTS,

		ThreadTS:  opts.ThreadTS,
		Broadcast: opts.Broadcast,
	}
	ch.Messages = append(ch.Messages, msg)

	return msg, nil
}

var errNoAttachment = errors.New("no attachment")

type attachments struct {
	Text    string
	Color   string
	Actions []Action
}
type Action struct {
	ChannelID string
	AppID     string
	TeamID    string

	BlockID  string
	ActionID string
	Text     string
	Value    string
	URL      string
}

// parseAttachments parses the attachments from the payload value.
func parseAttachments(appID, teamID, chanID, value string) (*attachments, error) {
	if value == "" {
		return nil, errNoAttachment
	}
	type textBlock struct{ Text string }

	var data [1]struct {
		Color  string
		Blocks []struct {
			Type     string
			BlockID  string `json:"block_id"`
			Elements json.RawMessage
			Text     textBlock
		}
	}
	err := json.Unmarshal([]byte(value), &data)
	if err != nil {
		return nil, err
	}

	var payload strings.Builder
	appendText := func(b textBlock) {
		if b.Text == "" {
			return
		}
		payload.WriteString(b.Text + "\n")
	}

	var actions []Action
	for _, b := range data[0].Blocks {
		appendText(b.Text)
		switch b.Type {
		case "context":
			var txtEl []textBlock
			err = json.Unmarshal(b.Elements, &txtEl)
			if err != nil {
				return nil, err
			}

			for _, e := range txtEl {
				appendText(e)
			}
		case "actions":
			var acts []struct {
				Text     textBlock
				ActionID string `json:"action_id"`
				Value    string
				URL      string
			}
			err = json.Unmarshal(b.Elements, &acts)
			if err != nil {
				return nil, err
			}

			for _, a := range acts {
				actions = append(actions, Action{
					ChannelID: chanID,
					TeamID:    teamID,
					AppID:     appID,
					BlockID:   b.BlockID,
					ActionID:  a.ActionID,
					Text:      a.Text.Text,
					Value:     a.Value,
					URL:       a.URL,
				})
			}
		default:
			continue
		}

	}

	return &attachments{
		Text:    payload.String(),
		Color:   data[0].Color,
		Actions: actions,
	}, nil
}

// ServeChatPostMessage serves a request to the `chat.postMessage` API call.
//
// https://api.slack.com/methods/chat.postMessage
func (s *Server) ServeChatPostMessage(w http.ResponseWriter, req *http.Request) {
	s.serveChatPostMessage(w, req, false)
}

// ServeChatUpdate serves a request to the `chat.update` API call.
//
// https://api.slack.com/methods/chat.update
func (s *Server) ServeChatUpdate(w http.ResponseWriter, req *http.Request) {
	s.serveChatPostMessage(w, req, true)
}

func (s *Server) serveChatPostMessage(w http.ResponseWriter, req *http.Request, isUpdate bool) {
	chanID := req.FormValue("channel")

	var text, color string
	var actions []Action

	var appID string
	s.mx.Lock()
	for id := range s.apps {
		if appID != "" {
			panic("multiple apps not supported")
		}
		appID = id
	}
	s.mx.Unlock()

	attachment, err := parseAttachments(appID, s.teamID, chanID, req.FormValue("attachments"))
	if err == errNoAttachment {
		err = nil
		text = req.FormValue("text")
	} else {
		text = attachment.Text
		color = attachment.Color
		actions = attachment.Actions
	}
	if respondErr(w, err) {
		return
	}

	var updateTS string
	if isUpdate {
		updateTS = req.FormValue("ts")
	}

	msg, err := s.API().ChatPostMessage(req.Context(), ChatPostMessageOptions{
		ChannelID: chanID,
		Text:      text,
		Color:     color,
		Actions:   actions,
		AsUser:    req.FormValue("as_user") == "true",
		ThreadTS:  req.FormValue("thread_ts"),
		UpdateTS:  updateTS,

		User: req.FormValue("user"),

		Broadcast: req.FormValue("reply_broadcast") == "true",
	})
	if respondErr(w, err) {
		return
	}

	var respData struct {
		response
		TS      string
		Channel string   `json:"channel"`
		Message *Message `json:"message"`
	}
	respData.TS = msg.TS
	respData.OK = true
	respData.Channel = chanID
	respData.Message = msg

	respondWith(w, respData)
}