From 7beb7eaba99317e25cb61f6bd5ac601ef346b4e9 Mon Sep 17 00:00:00 2001 From: Vaidas Jablonskis Date: Thu, 4 Jul 2024 13:03:09 +0300 Subject: [PATCH] Switch to slack upload file v2 api --- go.mod | 2 +- go.sum | 4 ++-- pkg/bot/slack_cloud.go | 27 +++++++++++++++++++-------- pkg/bot/slack_socket.go | 30 +++++++++++++++++++----------- test/go.mod | 2 +- test/go.sum | 4 ++-- 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index 76fee5b75..65ab80163 100644 --- a/go.mod +++ b/go.mod @@ -50,7 +50,7 @@ require ( github.com/segmentio/analytics-go v3.1.0+incompatible github.com/sha1sum/aws_signing_client v0.0.0-20200229211254-f7815c59d5c1 github.com/sirupsen/logrus v1.9.3 - github.com/slack-go/slack v0.12.2 + github.com/slack-go/slack v0.13.0 github.com/sourcegraph/conc v0.3.0 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 7ad8abd7f..aa8dc7015 100644 --- a/go.sum +++ b/go.sum @@ -1068,8 +1068,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/slack-go/slack v0.12.2 h1:x3OppyMyGIbbiyFhsBmpf9pwkUzMhthJMRNmNlA4LaQ= -github.com/slack-go/slack v0.12.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= +github.com/slack-go/slack v0.13.0 h1:7my/pR2ubZJ9912p9FtvALYpbt0cQPAqkRy2jaSI1PQ= +github.com/slack-go/slack v0.13.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= diff --git a/pkg/bot/slack_cloud.go b/pkg/bot/slack_cloud.go index 7917ada73..abfc21248 100644 --- a/pkg/bot/slack_cloud.go +++ b/pkg/bot/slack_cloud.go @@ -74,7 +74,8 @@ func NewCloudSlack(log logrus.FieldLogger, cfg config.CloudSlack, clusterName string, executorFactory ExecutorFactory, - reporter AnalyticsCommandReporter) (*CloudSlack, error) { + reporter AnalyticsCommandReporter, +) (*CloudSlack, error) { client := slack.New(cfg.Token) _, err := client.AuthTest() @@ -551,14 +552,14 @@ func (b *CloudSlack) send(ctx context.Context, event slackMessage, resp interact } // Upload message as a file if too long - var file *slack.File var err error + var file *slack.File if len(markdown) >= slackMaxMessageSize { file, err = b.uploadFileToSlack(ctx, event, resp) if err != nil { return err } - // the main message body was sent as a file, the only think that left is the filter input (if any) + // the main message body was sent as a file, the only thing that left is the filter input (if any) if len(resp.PlaintextInputs) == 0 { return nil } @@ -610,20 +611,30 @@ func (b *CloudSlack) send(ctx context.Context, event slackMessage, resp interact } func (b *CloudSlack) uploadFileToSlack(ctx context.Context, event slackMessage, resp interactive.CoreMessage) (*slack.File, error) { - params := slack.FileUploadParameters{ + content := interactive.MessageToPlaintext(resp, interactive.NewlineFormatter) + r := strings.NewReader(content) + + params := slack.UploadFileV2Parameters{ Filename: "Response.txt", + FileSize: len(content), Title: "Response.txt", InitialComment: resp.Description, - Content: interactive.MessageToPlaintext(resp, interactive.NewlineFormatter), - Channels: []string{event.Channel}, + Reader: r, + Channel: event.Channel, ThreadTimestamp: b.resolveMessageTimestamp(resp, event), } - file, err := b.client.UploadFileContext(ctx, params) + sum, err := b.client.UploadFileV2Context(ctx, params) if err != nil { return nil, fmt.Errorf("while uploading file: %w", err) } + // Fetch uploaded file info. + file, _, _, err := b.client.GetFileInfoContext(ctx, sum.ID, 0, 0) + if err != nil { + return nil, fmt.Errorf("while fetching uploaded file info: %w", err) + } + return file, nil } @@ -664,7 +675,7 @@ func (b *CloudSlack) getThreadOptionIfNeeded(resp interactive.CoreMessage, event func (b *CloudSlack) resolveMessageTimestamp(resp interactive.CoreMessage, event slackMessage) string { // If the message is coming e.g. from source, it may already belong to a given thread if resp.ParentActivityID != "" { - return resp.Message.ParentActivityID + return resp.ParentActivityID } // otherwise, we use the event timestamp to respond in the thread to the message that triggered our response diff --git a/pkg/bot/slack_socket.go b/pkg/bot/slack_socket.go index e5b95a4db..c479157d7 100644 --- a/pkg/bot/slack_socket.go +++ b/pkg/bot/slack_socket.go @@ -7,6 +7,7 @@ import ( "regexp" "strings" "sync" + "time" "github.com/google/uuid" "github.com/sirupsen/logrus" @@ -539,7 +540,8 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti } resp = interactive.CoreMessage{ Message: api.Message{ - PlaintextInputs: resp.Message.PlaintextInputs, + PlaintextInputs: resp.PlaintextInputs, + Timestamp: time.Now(), }, } } @@ -585,7 +587,7 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti options = append(options, slack.MsgOptionTS(resp.Message.ParentActivityID)) } - _, _, err = b.client.PostMessageContext(ctx, id, options...) + _, _, err := b.client.PostMessageContext(ctx, id, options...) if err != nil { return fmt.Errorf("while posting Slack message: %w", slackError(err, event.Channel)) } @@ -707,15 +709,11 @@ func resolveBlockActionCommand(act slack.BlockAction) (string, command.Origin) { } func (b *SocketSlack) getThreadOptionIfNeeded(event slackMessage, file *slack.File) slack.MsgOption { - //if the message is from thread then add an option to return the response to the thread + // if the message is from thread then add an option to return the response to the thread if event.ThreadTimeStamp != "" { return slack.MsgOptionTS(event.ThreadTimeStamp) } - if file == nil { - return nil - } - // If the message was already as a file attachment, reply it a given thread for _, share := range file.Shares.Public { if len(share) >= 1 && share[0].Ts != "" { @@ -764,19 +762,29 @@ func (b *SocketSlack) GetStatus() health.PlatformStatus { } func uploadFileToSlack(ctx context.Context, channel string, resp interactive.CoreMessage, client *slack.Client, ts string) (*slack.File, error) { - params := slack.FileUploadParameters{ + content := interactive.MessageToPlaintext(resp, interactive.NewlineFormatter) + r := strings.NewReader(content) + + params := slack.UploadFileV2Parameters{ Filename: "Response.txt", + FileSize: len(content), Title: "Response.txt", InitialComment: resp.Description, - Content: interactive.MessageToPlaintext(resp, interactive.NewlineFormatter), - Channels: []string{channel}, + Reader: r, + Channel: channel, ThreadTimestamp: ts, } - file, err := client.UploadFileContext(ctx, params) + sum, err := client.UploadFileV2Context(ctx, params) if err != nil { return nil, fmt.Errorf("while uploading file: %w", err) } + // Fetch uploaded file info. + file, _, _, err := client.GetFileInfoContext(ctx, sum.ID, 0, 0) + if err != nil { + return nil, fmt.Errorf("while fetching uploaded file info: %w", err) + } + return file, nil } diff --git a/test/go.mod b/test/go.mod index 6e9fc3e06..54063aa7c 100644 --- a/test/go.mod +++ b/test/go.mod @@ -22,7 +22,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 github.com/sanity-io/litter v1.5.5 - github.com/slack-go/slack v0.12.3 + github.com/slack-go/slack v0.13.0 github.com/stretchr/testify v1.9.0 github.com/vrischmann/envconfig v1.3.0 golang.org/x/oauth2 v0.16.0 diff --git a/test/go.sum b/test/go.sum index fb7e0a569..7da763e51 100644 --- a/test/go.sum +++ b/test/go.sum @@ -1161,8 +1161,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/slack-go/slack v0.12.3 h1:92/dfFU8Q5XP6Wp5rr5/T5JHLM5c5Smtn53fhToAP88= -github.com/slack-go/slack v0.12.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= +github.com/slack-go/slack v0.13.0 h1:7my/pR2ubZJ9912p9FtvALYpbt0cQPAqkRy2jaSI1PQ= +github.com/slack-go/slack v0.13.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=