From 0224c97c39b0cc2cc890911d01e325c8c891f8cc Mon Sep 17 00:00:00 2001 From: kathiouchka Date: Fri, 28 Jun 2024 20:56:06 +0200 Subject: [PATCH 1/3] unfurling option not working properly --- options.go | 16 ++++++++++++++++ response_replier.go | 8 ++++++++ response_writer.go | 7 +++++++ 3 files changed, 31 insertions(+) diff --git a/options.go b/options.go index 5ab0b85..b3cd724 100644 --- a/options.go +++ b/options.go @@ -108,12 +108,20 @@ func WithSchedule(timestamp time.Time) ReplyOption { } } +// WithUnfurlLinks sets the unfurl_links option +func WithUnfurlLinks(unfurlLinks bool) ReplyOption { + return func(defaults *replyOptions) { + defaults.UnfurlLinks = &unfurlLinks + } +} + type replyOptions struct { Attachments []slack.Attachment InThread *bool ReplaceMessageTS string IsEphemeral bool ScheduleTime *time.Time + UnfurlLinks *bool } // newReplyOptions builds our ReplyOptions from zero or more ReplyOption. @@ -167,12 +175,20 @@ func SetSchedule(timestamp time.Time) PostOption { } } +// SetUnfurlLinks sets the unfurl_links option +func SetUnfurlLinks(unfurlLinks bool) PostOption { + return func(defaults *postOptions) { + defaults.UnfurlLinks = &unfurlLinks + } +} + type postOptions struct { Attachments []slack.Attachment ThreadTS string ReplaceMessageTS string EphemeralUserID string ScheduleTime *time.Time + UnfurlLinks *bool } // newPostOptions builds our PostOptions from zero or more PostOption. diff --git a/response_replier.go b/response_replier.go index 9184454..0b999ab 100644 --- a/response_replier.go +++ b/response_replier.go @@ -1,6 +1,8 @@ package slacker import ( + "fmt" + "github.com/slack-go/slack" ) @@ -58,5 +60,11 @@ func (r *Replier) convertOptions(options ...ReplyOption) []PostOption { if replyOptions.ScheduleTime != nil { responseOptions = append(responseOptions, SetSchedule(*replyOptions.ScheduleTime)) } + + if replyOptions.UnfurlLinks != nil { + responseOptions = append(responseOptions, SetUnfurlLinks(*replyOptions.UnfurlLinks)) + fmt.Println("Unfurl Links Set to:", *replyOptions.UnfurlLinks) + } + return responseOptions } diff --git a/response_writer.go b/response_writer.go index 4d26930..539e99f 100644 --- a/response_writer.go +++ b/response_writer.go @@ -77,6 +77,13 @@ func (r *Writer) post(channel string, message string, blocks []slack.Block, opti opts = append(opts, slack.MsgOptionSchedule(postAt)) } + if postOptions.UnfurlLinks != nil { + if *postOptions.UnfurlLinks { + opts = append(opts, slack.MsgOptionDisableLinkUnfurl()) + fmt.Println("Unfurl Links Set to:", *postOptions.UnfurlLinks) + } + } + _, timestamp, err := r.slackClient.PostMessageContext( r.ctx, channel, From e9930825338736da927e0ffd518575722d274578 Mon Sep 17 00:00:00 2001 From: kathiouchka Date: Fri, 28 Jun 2024 21:08:46 +0200 Subject: [PATCH 2/3] fix: typo for greptile --- response_replier.go | 3 --- response_writer.go | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/response_replier.go b/response_replier.go index 0b999ab..dd09659 100644 --- a/response_replier.go +++ b/response_replier.go @@ -1,8 +1,6 @@ package slacker import ( - "fmt" - "github.com/slack-go/slack" ) @@ -63,7 +61,6 @@ func (r *Replier) convertOptions(options ...ReplyOption) []PostOption { if replyOptions.UnfurlLinks != nil { responseOptions = append(responseOptions, SetUnfurlLinks(*replyOptions.UnfurlLinks)) - fmt.Println("Unfurl Links Set to:", *replyOptions.UnfurlLinks) } return responseOptions diff --git a/response_writer.go b/response_writer.go index 539e99f..9983b87 100644 --- a/response_writer.go +++ b/response_writer.go @@ -78,9 +78,9 @@ func (r *Writer) post(channel string, message string, blocks []slack.Block, opti } if postOptions.UnfurlLinks != nil { - if *postOptions.UnfurlLinks { + if !*postOptions.UnfurlLinks { opts = append(opts, slack.MsgOptionDisableLinkUnfurl()) - fmt.Println("Unfurl Links Set to:", *postOptions.UnfurlLinks) + fmt.Println("unfurls are disabled") } } From 4c02642d0e19ff437573c31742a9c1783e7b6f56 Mon Sep 17 00:00:00 2001 From: kathiouchka Date: Fri, 28 Jun 2024 21:35:05 +0200 Subject: [PATCH 3/3] feat: slacker now handles unfurling option + main.go for testing purpose --- examples/message-unfurl/main.go | 39 +++++++++++++++++++++++++++++++++ response_writer.go | 4 ++-- 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 examples/message-unfurl/main.go diff --git a/examples/message-unfurl/main.go b/examples/message-unfurl/main.go new file mode 100644 index 0000000..cf6f5c2 --- /dev/null +++ b/examples/message-unfurl/main.go @@ -0,0 +1,39 @@ +// main.go + +package main + +import ( + "context" + "log" + "os" + + "github.com/slack-io/slacker" +) + +// Defining commands using slacker + +func main() { + bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN")) + + bot.AddCommand(&slacker.CommandDefinition{ + Command: "without", + Handler: func(ctx *slacker.CommandContext) { + ctx.Response().Reply("https://signoz.io/", slacker.WithUnfurlLinks(false)) + }, + }) + + bot.AddCommand(&slacker.CommandDefinition{ + Command: "with", + Handler: func(ctx *slacker.CommandContext) { + ctx.Response().Reply("https://signoz.io/") + }, + }) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + err := bot.Listen(ctx) + if err != nil { + log.Fatal(err) + } +} diff --git a/response_writer.go b/response_writer.go index 9983b87..7e7f33c 100644 --- a/response_writer.go +++ b/response_writer.go @@ -77,10 +77,10 @@ func (r *Writer) post(channel string, message string, blocks []slack.Block, opti opts = append(opts, slack.MsgOptionSchedule(postAt)) } + // response_writer.go if postOptions.UnfurlLinks != nil { if !*postOptions.UnfurlLinks { - opts = append(opts, slack.MsgOptionDisableLinkUnfurl()) - fmt.Println("unfurls are disabled") + opts = append(opts, slack.MsgOptionDisableLinkUnfurl(), slack.MsgOptionDisableMediaUnfurl()) } }