How to diable slack url previews in golang

Viewed 25

I want to send a message with a website link to slack through slack bot. Slack by defaults create the preview for that link. I wan't to disable the preview.

Here is my code:

s := slack.New("Bot-Token", slack.OptionDebug(true))

    data := `TEST: Slack link Preview 101 
     <https://www.google.com/maps|publish>`

    params := slack.PostMessageParameters{
        UnfurlLinks: false,
        UnfurlMedia: false,
    }

    _, t, err := s.PostMessage(
        "ChannelID",
        slack.MsgOptionText(data, false),
        slack.MsgOptionPostMessageParameters(params),
    )

    if err != nil {
        log.Error(err.Error())
    }

Its displaying the message as

But I want output like this

1 Answers

Remove http:// and https:// from links that you send. The link will be shown as clickable and will open in a browser but the preview will not be visible.

Source: https://slack.com/intl/en-in/help/articles/204399343-Share-links-and-set-preview-preferences#:~:text=Turn%20off%20link%20previews&text=From%20your%20desktop%2C%20click%20on,text%20previews%20of%20linked%20websites.

Format your code as follows -

s := slack.New("Bot-Token", slack.OptionDebug(true))

data := `TEST: Slack link Preview 101 
 <www.google.com/maps|publish>`

params := slack.PostMessageParameters{
    UnfurlLinks: false,
    UnfurlMedia: false,
}

_, t, err := s.PostMessage(
    "ChannelID",
    slack.MsgOptionText(data, false),
    slack.MsgOptionPostMessageParameters(params),
)

if err != nil {
    log.Error(err.Error())
}
Related