-
Notifications
You must be signed in to change notification settings - Fork 2
/
methods.go
333 lines (306 loc) · 9.78 KB
/
methods.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package chat
import (
"context"
"net/url"
"go.autokitteh.dev/autokitteh/sdk/sdkmodule"
"go.autokitteh.dev/autokitteh/sdk/sdkservices"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
"go.autokitteh.dev/autokitteh/integrations/slack/api"
)
const (
DefaultApprovalGreenButton = "Approve"
DefaultApprovalRedButton = "Deny"
)
type API struct {
Vars sdkservices.Vars
}
// Delete an existing message sent by the caller.
//
// Based on: https://api.slack.com/methods/chat.delete
//
// Required Slack app scopes:
// - https://api.slack.com/scopes/chat:write
// - https://api.slack.com/scopes/chat:write.public (for posting in public
// channels even when our Slack app isn't a member)
func (a API) Delete(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// Parse the input arguments.
req := DeleteRequest{}
err := sdkmodule.UnpackArgs(args, kwargs,
"channel", &req.Channel,
"ts", &req.TS,
)
if err != nil {
return sdktypes.InvalidValue, err
}
// Invoke the API method.
resp := &DeleteResponse{}
err = api.PostJSON(ctx, a.Vars, req, resp, "chat.delete")
if err != nil {
return sdktypes.InvalidValue, err
}
// Parse and return the response.
return sdktypes.WrapValue(resp)
}
// GetPermalink generates a permalink URL for a specific extant message.
//
// Based on: https://api.slack.com/methods/chat.getPermalink
//
// Required Slack app scopes: none.
func (a API) GetPermalink(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// Parse the input arguments.
var channel, messageTS string
err := sdkmodule.UnpackArgs(args, kwargs,
"channel", &channel,
"message_ts", &messageTS,
)
if err != nil {
return sdktypes.InvalidValue, err
}
req := url.Values{}
req.Set("channel", channel)
req.Set("message_ts", messageTS)
// Invoke the API method.
// TODO: Use HTTP GET instead of POST.
resp := &GetPermalinkResponse{}
err = api.PostForm(ctx, a.Vars, req, resp, "chat.getPermalink")
if err != nil {
return sdktypes.InvalidValue, err
}
// Parse and return the response.
return sdktypes.WrapValue(resp)
}
// PostEphemeral sends an ephemeral message to a user in a group/channel
// (visible only to the assigned user). For text formatting tips, see
// https://api.slack.com/reference/surfaces/formatting. This message
// may also contain a rich layout and/or interactive blocks:
// https://api.slack.com/messaging/composing/layouts.
//
// It returns the channel ID and the message's timestamp, but this timestamp
// may not be used for subsequent updates.
//
// https://api.slack.com/methods/chat.postEphemeral#markdown: ephemeral message
// delivery is not guaranteed — the user must be currently active in Slack and
// a member of the specified channel. By nature, ephemeral messages do not
// persist across reloads, desktop and mobile apps, or sessions. Once the
// session is closed, ephemeral messages will disappear and cannot be recovered.
//
// Based on: https://api.slack.com/methods/chat.postEphemeral
//
// Required Slack app scopes:
// - https://api.slack.com/scopes/chat:write
// - https://api.slack.com/scopes/chat:write.public (for posting in public
// channels even when our Slack app isn't a member)
func (a API) PostEphemeral(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// Parse the input arguments.
req := PostEphemeralRequest{}
err := sdkmodule.UnpackArgs(args, kwargs,
"channel", &req.Channel,
"user", &req.User,
"text", &req.Text,
"blocks?", &req.Blocks,
"thread_ts?", &req.ThreadTS,
)
if err != nil {
return sdktypes.InvalidValue, err
}
// Invoke the API method.
resp := &PostEphemeralResponse{}
err = api.PostJSON(ctx, a.Vars, req, resp, "chat.postEphemeral")
if err != nil {
return sdktypes.InvalidValue, err
}
// Parse and return the response.
return sdktypes.WrapValue(resp)
}
// PostMessage sends a message to a user/group/channel. For text formatting
// tips, see https://api.slack.com/reference/surfaces/formatting. This message
// may also contain a rich layout and/or interactive blocks:
// https://api.slack.com/messaging/composing/layouts.
//
// It returns the channel ID, the message's timestamp (for subsequent updates
// or in-thread replies), and a copy of the rendered message.
//
// Based on: https://api.slack.com/methods/chat.postMessage
//
// Required Slack app scopes:
// - https://api.slack.com/scopes/chat:write
// - https://api.slack.com/scopes/chat:write.public (for posting in public
// channels even when our Slack app isn't a member)
func (a API) PostMessage(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// Parse the input arguments.
req := PostMessageRequest{}
err := sdkmodule.UnpackArgs(args, kwargs,
"channel", &req.Channel,
"text?", &req.Text,
"blocks?", &req.Blocks,
"thread_ts?", &req.ThreadTS,
"reply_broadcast?", &req.ReplyBroadcast,
"username?", &req.Username,
"icon_url?", &req.IconURL,
)
if err != nil {
return sdktypes.InvalidValue, err
}
// Invoke the API method.
resp := &PostMessageResponse{}
err = api.PostJSON(ctx, a.Vars, req, resp, "chat.postMessage")
if err != nil {
return sdktypes.InvalidValue, err
}
// Parse and return the response.
return sdktypes.WrapValue(resp)
}
// Update an existing message sent by the caller. For text formatting tips,
// see https://api.slack.com/reference/surfaces/formatting. This message
// may also contain a rich layout and/or interactive blocks:
// https://api.slack.com/messaging/composing/layouts.
//
// It returns the channel ID, the message's timestamp (for subsequent
// updates or in-thread replies), and a copy of the rendered message.
//
// Based on: https://api.slack.com/methods/chat.update
//
// Required Slack app scopes:
// - https://api.slack.com/scopes/chat:write
// - https://api.slack.com/scopes/chat:write.public (for posting in
// public channels even when our Slack app isn't a member)
func (a API) Update(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// Parse the input arguments.
req := UpdateRequest{}
err := sdkmodule.UnpackArgs(args, kwargs,
"channel", &req.Channel,
"ts", &req.TS,
"text?", &req.Text,
"blocks?", &req.Blocks,
"reply_broadcast?", &req.ReplyBroadcast,
)
if err != nil {
return sdktypes.InvalidValue, err
}
// Invoke the API method.
resp := &UpdateResponse{}
err = api.PostJSON(ctx, a.Vars, req, resp, "chat.update")
if err != nil {
return sdktypes.InvalidValue, err
}
// Parse and return the response.
return sdktypes.WrapValue(resp)
}
// SendTextMessage sends a message to a user/group/channel. For text formatting
// tips, see https://api.slack.com/reference/surfaces/formatting.
//
// It returns the channel ID, the message's timestamp (for subsequent updates
// or in-thread replies), and a copy of the rendered message.
//
// This is a convenience wrapper over [PostMessage].
func (a API) SendTextMessage(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// Parse the input arguments.
req := PostMessageRequest{}
err := sdkmodule.UnpackArgs(args, kwargs,
"target", &req.Channel,
"text", &req.Text,
"thread_ts?", &req.ThreadTS,
"reply_broadcast?", &req.ReplyBroadcast,
)
if err != nil {
return sdktypes.InvalidValue, err
}
// Invoke the API method.
resp := &PostMessageResponse{}
err = api.PostJSON(ctx, a.Vars, req, resp, "chat.postMessage")
if err != nil {
return sdktypes.InvalidValue, err
}
// Parse and return the response.
return sdktypes.WrapValue(resp)
}
// SendApprovalMessage sends an interactive message to a user/group/channel,
// with a short header, a longer message, and 2 buttons. For message formatting
// tips, see https://api.slack.com/reference/surfaces/formatting.
//
// It returns the channel ID, the message's timestamp (for subsequent updates
// or in-thread replies), and a copy of the rendered message. The user's button
// choice will be relayed as an asynchronous interaction event.
//
// This is a convenience wrapper over [PostMessage].
func (a API) SendApprovalMessage(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// Parse the input arguments.
req := PostMessageRequest{}
var message, greenButton, redButton string
err := sdkmodule.UnpackArgs(args, kwargs,
"target", &req.Channel,
"header", &req.Text,
"message", &message,
"greenButton?", &greenButton,
"redButton?", &redButton,
"thread_ts?", &req.ThreadTS,
"reply_broadcast?", &req.ReplyBroadcast,
)
if err != nil {
return sdktypes.InvalidValue, err
}
if greenButton == "" {
greenButton = DefaultApprovalGreenButton
}
if redButton == "" {
redButton = DefaultApprovalRedButton
}
req.Blocks = []Block{
{
Type: "header",
Text: &Text{
Type: "plain_text",
Emoji: true,
Text: req.Text,
},
},
{
Type: "divider",
},
{
Type: "section",
Text: &Text{
Type: "mrkdwn",
Text: message,
},
},
{
Type: "divider",
},
{
Type: "actions",
Elements: []Button{
{
Type: "button",
Style: "primary",
Text: &Text{
Type: "plain_text",
Emoji: true,
Text: greenButton,
},
Value: DefaultApprovalGreenButton,
ActionID: DefaultApprovalGreenButton,
},
{
Type: "button",
Style: "danger",
Text: &Text{
Type: "plain_text",
Emoji: true,
Text: redButton,
},
Value: DefaultApprovalRedButton,
ActionID: DefaultApprovalRedButton,
},
},
},
}
// Invoke the API method.
resp := &PostMessageResponse{}
err = api.PostJSON(ctx, a.Vars, req, resp, "chat.postMessage")
if err != nil {
return sdktypes.InvalidValue, err
}
// Parse and return the response.
return sdktypes.WrapValue(resp)
}