-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathslack.go
755 lines (642 loc) · 24.2 KB
/
slack.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
package slack
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
amConfig "github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/types"
"github.com/grafana/alerting/images"
"github.com/grafana/alerting/logging"
"github.com/grafana/alerting/receivers"
"github.com/grafana/alerting/templates"
)
const (
// maxImagesPerThreadTs is the maximum number of images that can be posted as
// replies to the same thread_ts. It should prevent tokens from exceeding the
// rate limits for uploads https://api.slack.com/docs/rate-limits#tier_t2
maxImagesPerThreadTs = 5
maxImagesPerThreadTsMessage = "There are more images than can be shown here. To see the panels for all firing and resolved alerts please check Grafana"
footerIconURL = "https://grafana.com/static/assets/img/fav32.png"
)
// APIURL of where the notification payload is sent. It is public to be overridable in integration tests.
var APIURL = "https://slack.com/api/chat.postMessage"
var (
slackClient = &http.Client{
Timeout: time.Second * 30,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateFreelyAsClient,
},
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
},
}
)
type sendMessageFunc func(ctx context.Context, req *http.Request, logger logging.Logger) (string, error)
type initFileUploadFunc func(ctx context.Context, req *http.Request, logger logging.Logger) (*FileUploadURLResponse, error)
type uploadFileFunc func(ctx context.Context, req *http.Request, logger logging.Logger) error
type completeFileUploadFunc func(ctx context.Context, req *http.Request, logger logging.Logger) error
// https://api.slack.com/reference/messaging/attachments#legacy_fields - 1024, no units given, assuming runes or characters.
const slackMaxTitleLenRunes = 1024
// Notifier is responsible for sending
// alert notification to Slack.
type Notifier struct {
*receivers.Base
log logging.Logger
tmpl *templates.Template
images images.Provider
webhookSender receivers.WebhookSender
sendMessageFn sendMessageFunc
initFileUploadFn initFileUploadFunc
uploadFileFn uploadFileFunc
completeFileUploadFn completeFileUploadFunc
settings Config
appVersion string
}
// isIncomingWebhook returns true if the settings are for an incoming webhook.
func isIncomingWebhook(s Config) bool {
return s.Token == ""
}
// endpointURL returns the combined URL for the endpoint based on the config and apiMethod
func endpointURL(s Config, apiMethod string) (string, error) {
u, err := url.Parse(s.URL)
if err != nil {
return "", fmt.Errorf("failed to parse URL: %w", err)
}
dir, _ := path.Split(u.Path)
u.Path = path.Join(dir, apiMethod)
return u.String(), nil
}
func New(cfg Config, meta receivers.Metadata, template *templates.Template, sender receivers.WebhookSender, images images.Provider, logger logging.Logger, appVersion string) *Notifier {
return &Notifier{
Base: receivers.NewBase(meta),
settings: cfg,
images: images,
webhookSender: sender,
sendMessageFn: sendSlackMessage,
initFileUploadFn: initFileUpload,
uploadFileFn: uploadFile,
completeFileUploadFn: completeFileUpload,
log: logger,
tmpl: template,
appVersion: appVersion,
}
}
// slackMessage is the slackMessage for sending a slack notification.
type slackMessage struct {
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
Username string `json:"username,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Attachments []attachment `json:"attachments"`
Blocks []map[string]interface{} `json:"blocks,omitempty"`
ThreadTs string `json:"thread_ts,omitempty"`
}
// attachment is used to display a richly-formatted message block.
type attachment struct {
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text"`
ImageURL string `json:"image_url,omitempty"`
Fallback string `json:"fallback"`
Fields []amConfig.SlackField `json:"fields,omitempty"`
Footer string `json:"footer"`
FooterIcon string `json:"footer_icon"`
Color string `json:"color,omitempty"`
Ts int64 `json:"ts,omitempty"`
Pretext string `json:"pretext,omitempty"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
}
// generic api response from slack
type CommonAPIResponse struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}
// the response from the slack API when sending a message (i.e. chat.postMessage)
type slackMessageResponse struct {
Ts string `json:"ts"`
Channel string `json:"channel"`
}
// the response to get the URL to upload a file to (files.getUploadURLExternal)
type FileUploadURLResponse struct {
UploadURL string `json:"upload_url"`
FileID string `json:"file_id"`
}
type CompleteFileUploadRequest struct {
Files []struct {
ID string `json:"id"`
} `json:"files"`
ChannelID string `json:"channel_id"`
ThreadTs string `json:"thread_ts"`
InitialComment string `json:"initial_comment"`
}
// Notify sends an alert notification to Slack.
func (sn *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) {
sn.log.Debug("Creating slack message", "alerts", len(alerts))
m, err := sn.createSlackMessage(ctx, alerts)
if err != nil {
sn.log.Error("Failed to create Slack message", "err", err)
return false, fmt.Errorf("failed to create Slack message: %w", err)
}
threadTs, err := sn.sendSlackMessage(ctx, m)
if err != nil {
sn.log.Error("Failed to send Slack message", "err", err)
return false, fmt.Errorf("failed to send Slack message: %w", err)
}
// Do not upload images if using an incoming webhook as incoming webhooks cannot upload files
if !isIncomingWebhook(sn.settings) {
if err := images.WithStoredImages(ctx, sn.log, sn.images, func(index int, image images.Image) error {
// If we have exceeded the maximum number of images for this threadTs
// then tell the recipient and stop iterating subsequent images
if index >= maxImagesPerThreadTs {
if _, err := sn.sendSlackMessage(ctx, &slackMessage{
Channel: sn.settings.Recipient,
Text: maxImagesPerThreadTsMessage,
ThreadTs: threadTs,
}); err != nil {
sn.log.Error("Failed to send Slack message", "err", err)
}
return images.ErrImagesDone
}
comment := initialCommentForImage(alerts[index])
return sn.uploadImage(ctx, image, sn.settings.Recipient, comment, threadTs)
}, alerts...); err != nil {
// Do not return an error here as we might have exceeded the rate limit for uploading files
sn.log.Error("Failed to upload image", "err", err)
}
}
return true, nil
}
func (sn *Notifier) commonAlertGeneratorURL(_ context.Context, alerts []*types.Alert) bool {
if len(alerts[0].GeneratorURL) == 0 {
return false
}
firstURL := alerts[0].GeneratorURL
for _, a := range alerts {
if a.GeneratorURL != firstURL {
return false
}
}
return true
}
func (sn *Notifier) createSlackMessage(ctx context.Context, alerts []*types.Alert) (*slackMessage, error) {
var tmplErr error
tmpl, _ := templates.TmplText(ctx, sn.tmpl, alerts, sn.log, &tmplErr)
ruleURL := receivers.JoinURLPath(sn.tmpl.ExternalURL.String(), "/alerting/list", sn.log)
// If all alerts have the same GeneratorURL, use that.
if sn.commonAlertGeneratorURL(ctx, alerts) {
ruleURL = alerts[0].GeneratorURL
}
title, truncated := receivers.TruncateInRunes(tmpl(sn.settings.Title), slackMaxTitleLenRunes)
if truncated {
key, err := notify.ExtractGroupKey(ctx)
if err != nil {
return nil, err
}
sn.log.Warn("Truncated title", "key", key, "max_runes", slackMaxTitleLenRunes)
}
if tmplErr != nil {
sn.log.Warn("failed to template Slack title", "error", tmplErr.Error())
tmplErr = nil
}
req := &slackMessage{
Channel: tmpl(sn.settings.Recipient),
Username: tmpl(sn.settings.Username),
IconEmoji: tmpl(sn.settings.IconEmoji),
IconURL: tmpl(sn.settings.IconURL),
// TODO: We should use the Block Kit API instead:
// https://api.slack.com/messaging/composing/layouts#when-to-use-attachments
Attachments: []attachment{
{
Color: receivers.GetAlertStatusColor(types.Alerts(alerts...).Status()),
Title: title,
Fallback: title,
Footer: "Grafana v" + sn.appVersion,
FooterIcon: footerIconURL,
Ts: time.Now().Unix(),
TitleLink: ruleURL,
Text: tmpl(sn.settings.Text),
Fields: nil, // TODO. Should be a config.
},
},
}
if isIncomingWebhook(sn.settings) {
// Incoming webhooks cannot upload files, instead share images via their URL
_ = images.WithStoredImages(ctx, sn.log, sn.images, func(_ int, image images.Image) error {
if image.URL != "" {
req.Attachments[0].ImageURL = image.URL
return images.ErrImagesDone
}
return nil
}, alerts...)
}
if tmplErr != nil {
sn.log.Warn("failed to template Slack message", "error", tmplErr.Error())
}
mentionsBuilder := strings.Builder{}
appendSpace := func() {
if mentionsBuilder.Len() > 0 {
mentionsBuilder.WriteString(" ")
}
}
mentionChannel := strings.TrimSpace(sn.settings.MentionChannel)
if mentionChannel != "" {
mentionsBuilder.WriteString(fmt.Sprintf("<!%s|%s>", mentionChannel, mentionChannel))
}
if len(sn.settings.MentionGroups) > 0 {
appendSpace()
for _, g := range sn.settings.MentionGroups {
mentionsBuilder.WriteString(fmt.Sprintf("<!subteam^%s>", tmpl(g)))
}
}
if len(sn.settings.MentionUsers) > 0 {
appendSpace()
for _, u := range sn.settings.MentionUsers {
mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", tmpl(u)))
}
}
if mentionsBuilder.Len() > 0 {
// Use markdown-formatted pretext for any mentions.
req.Attachments[0].MrkdwnIn = []string{"pretext"}
req.Attachments[0].Pretext = mentionsBuilder.String()
}
return req, nil
}
func (sn *Notifier) sendSlackMessage(ctx context.Context, m *slackMessage) (string, error) {
b, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("failed to marshal Slack message: %w", err)
}
sn.log.Debug("sending Slack API request", "url", sn.settings.URL, "data", string(b))
request, err := http.NewRequestWithContext(ctx, http.MethodPost, sn.settings.URL, bytes.NewReader(b))
if err != nil {
return "", fmt.Errorf("failed to create HTTP request: %w", err)
}
request.Header.Set("Content-Type", "application/json; charset=utf-8")
request.Header.Set("User-Agent", "Grafana")
if sn.settings.Token == "" {
if sn.settings.URL == APIURL {
panic("Token should be set when using the Slack chat API")
}
sn.log.Debug("Looks like we are using an incoming webhook, no Authorization header required")
} else {
sn.log.Debug("Looks like we are using the Slack API, have set the Bearer token for this request")
request.Header.Set("Authorization", "Bearer "+sn.settings.Token)
}
threadTs, err := sn.sendMessageFn(ctx, request, sn.log)
if err != nil {
return "", err
}
return threadTs, nil
}
// createImageMultipart returns the multipart/form-data request and headers for the url from getUploadURL
// It returns an error if the image does not exist or there was an error preparing the
// multipart form.
func (sn *Notifier) createImageMultipart(image images.Image) (http.Header, []byte, error) {
buf := bytes.Buffer{}
w := multipart.NewWriter(&buf)
defer func() {
if err := w.Close(); err != nil {
sn.log.Error("Failed to close multipart writer", "err", err)
}
}()
f, err := os.Open(image.Path)
if err != nil {
return nil, nil, err
}
defer func() {
if err := f.Close(); err != nil {
sn.log.Error("Failed to close image file reader", "error", err)
}
}()
fw, err := w.CreateFormFile("filename", image.Path)
if err != nil {
return nil, nil, fmt.Errorf("failed to create form file: %w", err)
}
if _, err := io.Copy(fw, f); err != nil {
return nil, nil, fmt.Errorf("failed to copy file to form: %w", err)
}
if err := w.Close(); err != nil {
return nil, nil, fmt.Errorf("failed to close multipart writer: %w", err)
}
b := buf.Bytes()
headers := http.Header{}
headers.Set("Content-Type", w.FormDataContentType())
return headers, b, nil
}
func (sn *Notifier) sendMultipart(ctx context.Context, uploadURL string, headers http.Header, data io.Reader) error {
sn.log.Debug("Sending multipart request", "url", uploadURL)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL, data)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
for k, v := range headers {
req.Header[k] = v
}
req.Header.Set("Authorization", "Bearer "+sn.settings.Token)
return sn.uploadFileFn(ctx, req, sn.log)
}
// uploadImage shares the image to the channel names or IDs. It returns an error if the file
// does not exist, or if there was an error either preparing or sending the multipart/form-data
// request.
func (sn *Notifier) uploadImage(ctx context.Context, image images.Image, channel, comment, threadTs string) error {
sn.log.Debug("Uploading image", "image", image.Token)
imageData, err := os.Stat(image.Path)
if err != nil {
return fmt.Errorf("failed to get image info: %w", err)
}
// get the upload url
uploadURLResponse, err := sn.getUploadURL(ctx, image.Path, imageData.Size())
if err != nil {
return fmt.Errorf("failed to get upload URL: %w", err)
}
// upload the image
headers, data, err := sn.createImageMultipart(image)
if err != nil {
return fmt.Errorf("failed to create multipart form: %w", err)
}
uploadErr := sn.sendMultipart(ctx, uploadURLResponse.UploadURL, headers, bytes.NewReader(data))
if uploadErr != nil {
return fmt.Errorf("failed to upload image: %w", uploadErr)
}
// complete file upload to upload the image to the channel/thread with the comment
// need to use uploadURLResponse.FileID to complete the upload
return sn.finalizeUpload(ctx, uploadURLResponse.FileID, channel, threadTs, comment)
}
// getUploadURL returns the URL to upload the image to. It returns an error if the image cannot be uploaded.
func (sn *Notifier) getUploadURL(ctx context.Context, filename string, imageSize int64) (*FileUploadURLResponse, error) {
apiEndpoint, err := endpointURL(sn.settings, "files.getUploadURLExternal")
if err != nil {
return nil, fmt.Errorf("failed to get URL for files.getUploadURLExternal: %w", err)
}
data := url.Values{}
data.Set("filename", filename)
data.Set("length", fmt.Sprintf("%d", imageSize))
url := fmt.Sprintf("%s?%s", apiEndpoint, data.Encode())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
req.Header.Set("Authorization", "Bearer "+sn.settings.Token)
return sn.initFileUploadFn(ctx, req, sn.log)
}
func (sn *Notifier) finalizeUpload(ctx context.Context, fileID, channel, threadTs, comment string) error {
completeUploadEndpoint, err := endpointURL(sn.settings, "files.completeUploadExternal")
if err != nil {
return fmt.Errorf("failed to get URL for files.completeUploadExternal: %w", err)
}
// make json request to complete the upload
body := CompleteFileUploadRequest{
Files: []struct {
ID string `json:"id"`
}{
{ID: fileID},
},
ChannelID: channel,
ThreadTs: threadTs,
InitialComment: comment,
}
completeUploadData, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal complete upload request: %w", err)
}
completeUploadReq, err := http.NewRequestWithContext(ctx, http.MethodPost, completeUploadEndpoint, bytes.NewReader(completeUploadData))
if err != nil {
return fmt.Errorf("failed to create complete upload request: %w", err)
}
completeUploadReq.Header.Set("Content-Type", "application/json; charset=utf-8")
completeUploadReq.Header.Set("Authorization", "Bearer "+sn.settings.Token)
return sn.completeFileUploadFn(ctx, completeUploadReq, sn.log)
}
func (sn *Notifier) SendResolved() bool {
return !sn.GetDisableResolveMessage()
}
// initialCommentForImage returns the initial comment for the image.
// Here is an example of the initial comment for an alert called
// AlertName with two labels:
//
// Resolved|Firing: AlertName, Labels: A=B, C=D
//
// where Resolved|Firing and Labels is in bold text.
func initialCommentForImage(alert *types.Alert) string {
sb := strings.Builder{}
if alert.Resolved() {
sb.WriteString("*Resolved*:")
} else {
sb.WriteString("*Firing*:")
}
sb.WriteString(" ")
sb.WriteString(alert.Name())
sb.WriteString(", ")
sb.WriteString("*Labels*: ")
var n int
for k, v := range alert.Labels {
sb.WriteString(string(k))
sb.WriteString(" = ")
sb.WriteString(string(v))
if n < len(alert.Labels)-1 {
sb.WriteString(", ")
n++
}
}
return sb.String()
}
func errorForStatusCode(logger logging.Logger, statusCode int) error {
if statusCode < http.StatusOK {
logger.Error("Unexpected 1xx response", "status", statusCode)
return fmt.Errorf("unexpected 1xx status code: %d", statusCode)
} else if statusCode >= 300 && statusCode < 400 {
logger.Error("Unexpected 3xx response", "status", statusCode)
return fmt.Errorf("unexpected 3xx status code: %d", statusCode)
} else if statusCode >= http.StatusInternalServerError {
logger.Error("Unexpected 5xx response", "status", statusCode)
return fmt.Errorf("unexpected 5xx status code: %d", statusCode)
}
return nil
}
// sendSlackMessage sends a request to the Slack API.
// Stubbable by tests.
func sendSlackMessage(_ context.Context, req *http.Request, logger logging.Logger) (string, error) {
resp, err := slackClient.Do(req)
if err != nil {
return "", fmt.Errorf("failed to send request: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.Warn("Failed to close response body", "err", err)
}
}()
if err := errorForStatusCode(logger, resp.StatusCode); err != nil {
return "", err
}
content := resp.Header.Get("Content-Type")
if strings.HasPrefix(content, "application/json") {
return handleSlackMessageJSONResponse(resp, logger)
}
// If the response is not JSON it could be the response to an incoming webhook
return handleSlackIncomingWebhookResponse(resp, logger)
}
func handleSlackIncomingWebhookResponse(resp *http.Response, logger logging.Logger) (string, error) {
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %w", err)
}
// Incoming webhooks return the string "ok" on success
if bytes.Equal(b, []byte("ok")) {
logger.Debug("The incoming webhook was successful")
return "", nil
}
logger.Debug("Incoming webhook was unsuccessful", "status", resp.StatusCode, "body", string(b))
// There are a number of known errors that we can check. The documentation incoming webhooks
// errors can be found at https://api.slack.com/messaging/webhooks#handling_errors and
// https://api.slack.com/changelog/2016-05-17-changes-to-errors-for-incoming-webhooks
if bytes.Equal(b, []byte("user_not_found")) {
return "", errors.New("the user does not exist or is invalid")
}
if bytes.Equal(b, []byte("channel_not_found")) {
return "", errors.New("the channel does not exist or is invalid")
}
if bytes.Equal(b, []byte("channel_is_archived")) {
return "", errors.New("cannot send an incoming webhook for an archived channel")
}
if bytes.Equal(b, []byte("posting_to_general_channel_denied")) {
return "", errors.New("cannot send an incoming webhook to the #general channel")
}
if bytes.Equal(b, []byte("no_service")) {
return "", errors.New("the incoming webhook is either disabled, removed, or invalid")
}
if bytes.Equal(b, []byte("no_text")) {
return "", errors.New("cannot send an incoming webhook without a message")
}
return "", fmt.Errorf("failed incoming webhook: %s", string(b))
}
func handleSlackMessageJSONResponse(resp *http.Response, logger logging.Logger) (string, error) {
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %w", err)
}
if len(b) == 0 {
logger.Error("Expected JSON but got empty response")
return "", errors.New("unexpected empty response")
}
// Slack responds to some requests with a JSON document, that might contain an error.
result := struct {
CommonAPIResponse
slackMessageResponse
}{}
if err := json.Unmarshal(b, &result); err != nil {
logger.Error("Failed to unmarshal response", "body", string(b), "err", err)
return "", fmt.Errorf("failed to unmarshal response: %w", err)
}
if !result.OK {
logger.Error("The request was unsuccessful", "body", string(b), "err", result.Error)
return "", fmt.Errorf("failed to send request: %s", result.Error)
}
logger.Debug("The request was successful")
return result.Ts, nil
}
func initFileUpload(_ context.Context, req *http.Request, logger logging.Logger) (*FileUploadURLResponse, error) {
resp, err := slackClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.Warn("Failed to close response body", "err", err)
}
}()
if err := errorForStatusCode(logger, resp.StatusCode); err != nil {
return nil, err
}
content := resp.Header.Get("Content-Type")
if strings.HasPrefix(content, "application/json") {
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
if len(b) == 0 {
logger.Error("Expected JSON but got empty response")
return nil, errors.New("unexpected empty response")
}
// Slack responds to some requests with a JSON document, that might contain an error.
result := struct {
CommonAPIResponse
FileUploadURLResponse
}{}
if err := json.Unmarshal(b, &result); err != nil {
logger.Error("Failed to unmarshal response", "body", string(b), "err", err)
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
if !result.OK {
logger.Error("The request was unsuccessful", "body", string(b), "err", result.Error)
return nil, fmt.Errorf("failed to send request: %s", result.Error)
}
logger.Debug("The request was successful")
return &result.FileUploadURLResponse, nil
}
return nil, fmt.Errorf("unexpected content type: %s", content)
}
func uploadFile(_ context.Context, req *http.Request, logger logging.Logger) error {
resp, err := slackClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
// no need to check body, just check the status code
return errorForStatusCode(logger, resp.StatusCode)
}
func completeFileUpload(_ context.Context, req *http.Request, logger logging.Logger) error {
resp, err := slackClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.Warn("Failed to close response body", "err", err)
}
}()
if err := errorForStatusCode(logger, resp.StatusCode); err != nil {
return err
}
content := resp.Header.Get("Content-Type")
if strings.HasPrefix(content, "application/json") {
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}
if len(b) == 0 {
logger.Error("Expected JSON but got empty response")
return errors.New("unexpected empty response")
}
// Slack responds to some requests with a JSON document, that might contain an error.
result := CommonAPIResponse{}
if err := json.Unmarshal(b, &result); err != nil {
logger.Error("Failed to unmarshal response", "body", string(b), "err", err)
return fmt.Errorf("failed to unmarshal response: %w", err)
}
if !result.OK {
logger.Error("The request was unsuccessful", "body", string(b), "err", result.Error)
return fmt.Errorf("failed to send request: %s", result.Error)
}
logger.Debug("The request was successful")
return nil
}
return fmt.Errorf("unexpected content type: %s", content)
}