Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to slack upload file v2 api #1468

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -271,3 +271,5 @@ require (
)

replace github.com/hashicorp/go-getter v1.7.3 => github.com/kubeshop/go-getter v0.0.0-20240219121353-061e752b1b28

replace github.com/slack-go/slack v0.13.0 => github.com/kubeshop/slack v0.0.0-20240710080407-1ca22ed3b567
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubeshop/go-getter v0.0.0-20240219121353-061e752b1b28 h1:cIyWkSZbkn/7VU7/T375t9n7esO8/bruaJ82FIk4Gs4=
github.com/kubeshop/go-getter v0.0.0-20240219121353-061e752b1b28/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/kubeshop/slack v0.0.0-20240710080407-1ca22ed3b567 h1:L0LJ92GmIkWo/l0Ht0RJQAbBeIfoTEOl/b4eBfIigAw=
github.com/kubeshop/slack v0.0.0-20240710080407-1ca22ed3b567/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
Expand Down Expand Up @@ -1068,8 +1070,6 @@ 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/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=
Expand Down
21 changes: 13 additions & 8 deletions pkg/bot/slack_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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()
Expand Down Expand Up @@ -553,14 +554,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
}
Expand Down Expand Up @@ -612,16 +613,20 @@ 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)
file, err := b.client.UploadFileV2Context(ctx, params)
if err != nil {
return nil, fmt.Errorf("while uploading file: %w", err)
}
Expand Down Expand Up @@ -666,7 +671,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
Expand Down
20 changes: 12 additions & 8 deletions pkg/bot/slack_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,16 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti
}

// 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 = uploadFileToSlack(ctx, event.Channel, resp, b.client, event.ThreadTimeStamp)
if err != nil {
return err
}
resp = interactive.CoreMessage{
Message: api.Message{
PlaintextInputs: resp.Message.PlaintextInputs,
PlaintextInputs: resp.PlaintextInputs,
},
}
}
Expand Down Expand Up @@ -586,7 +586,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))
}
Expand Down Expand Up @@ -708,7 +708,7 @@ 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)
}
Expand Down Expand Up @@ -767,16 +767,20 @@ 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)
file, err := client.UploadFileV2Context(ctx, params)
if err != nil {
return nil, fmt.Errorf("while uploading file: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion test/commplatform/slack_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ type SlackMessageAssertion func(content slack.Message) (bool, int, string)
func (s *SlackChannel) ID() string {
return s.Channel.ID
}

func (s *SlackChannel) Name() string {
return s.Channel.Name
}

func (s *SlackChannel) Identifier() string {
return s.Channel.Name
}
Expand Down Expand Up @@ -347,7 +349,7 @@ func (s *SlackTester) WaitForMessagePostedWithFileUpload(userID, channelID strin
var fetchedMessages []slack.Message
var lastErr error
err := wait.PollUntilContextTimeout(context.Background(), pollInterval, s.cfg.MessageWaitTimeout, false, func(ctx context.Context) (done bool, err error) {
fetchedMessages, err := s.getMessages(channelID, 2) // Fetching 2 messages, where the first one is the file itself and the second is the filter input.
fetchedMessages, err = s.getMessages(channelID, 2) // Fetching 2 messages, where the first one is the file itself and the second is the filter input.
if err != nil {
lastErr = err
return false, nil
Expand Down
3 changes: 2 additions & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -243,4 +243,5 @@ require (
replace (
github.com/DanielTitkov/go-adaptive-cards => github.com/kubeshop/go-adaptive-cards v0.0.0-20231114223529-d6d8b980f0c8
github.com/kubeshop/botkube => ./..
github.com/slack-go/slack v0.13.0 => github.com/kubeshop/slack v0.0.0-20240710080407-1ca22ed3b567
)
4 changes: 2 additions & 2 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ github.com/kubeshop/botkube-cloud/botkube-cloud-backend v0.0.0-20240508145541-6a
github.com/kubeshop/botkube-cloud/botkube-cloud-backend v0.0.0-20240508145541-6aa7480265af/go.mod h1:qVsXQutuaUP+4eoSs4SCpVWvfZLWxs9yB459JclAAuk=
github.com/kubeshop/go-adaptive-cards v0.0.0-20231114223529-d6d8b980f0c8 h1:uTChAaS5OdD9gGXnafXMUhMo1gyyX2loCjoCyQr5mlg=
github.com/kubeshop/go-adaptive-cards v0.0.0-20231114223529-d6d8b980f0c8/go.mod h1:RtCzt65p/zEos6+zhiCFQmiaHmro6M63l9NP7xXx/Lg=
github.com/kubeshop/slack v0.0.0-20240710080407-1ca22ed3b567 h1:L0LJ92GmIkWo/l0Ht0RJQAbBeIfoTEOl/b4eBfIigAw=
github.com/kubeshop/slack v0.0.0-20240710080407-1ca22ed3b567/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
Expand Down Expand Up @@ -1161,8 +1163,6 @@ 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/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=
Expand Down
Loading