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

Unfurling option #13

Closed
wants to merge 3 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
39 changes: 39 additions & 0 deletions examples/message-unfurl/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
16 changes: 16 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions response_replier.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@ 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))
}

return responseOptions
}
7 changes: 7 additions & 0 deletions response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ 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(), slack.MsgOptionDisableMediaUnfurl())
}
}

_, timestamp, err := r.slackClient.PostMessageContext(
r.ctx,
channel,
Expand Down
Loading