go-notificationgo-notification
Channels/Chat

Slack

Post notifications to Slack channels via Incoming Webhooks or the chat.postMessage API.

Two ways to send

  • Incoming Webhooks — one URL, posts to one channel. Simplest to set up.
  • Bot token (chat.postMessage) — one token, can post to many channels (if invited). Needed for DMs, threads, updates.

Both are supported.

Option A — Incoming Webhook

  1. Go to https://api.slack.com/appsCreate New App → pick a workspace.
  2. Enable Incoming Webhooks.
  3. Add New Webhook to Workspace, pick a channel.
  4. Copy the webhook URL.
main.go
import "github.com/gopackx/go-notification/channel/chat/slack"

notifier.RegisterChannel("slack", slack.NewWebhook(slack.WebhookConfig{
    URL: os.Getenv("SLACK_WEBHOOK_URL"),
}))

Option B — Bot token

  1. In the same app settings, enable Bots.
  2. Add OAuth scopes: chat:write, chat:write.public.
  3. Install the app to your workspace and copy the Bot User OAuth Token (starts with xoxb-).
  4. Invite the bot into any channel it should post to (/invite @YourBot).
main.go
notifier.RegisterChannel("slack", slack.NewBot(slack.BotConfig{
    Token:          os.Getenv("SLACK_BOT_TOKEN"),
    DefaultChannel: "#alerts",
}))

Sending

main.go
func (n DeployFailed) Via(u notification.Notifiable) []string {
    return []string{"slack"}
}

func (n DeployFailed) ToSlack(u notification.Notifiable) *slack.Message {
    return slack.NewMessage().
        Text("Deploy failed: " + n.Service).
        Attachment(
            slack.NewAttachment().
                Color("danger").
                Field("Commit", n.Commit, true).
                Field("Actor",  n.Actor,  true),
        )
}

For routing per recipient (e.g. DM a specific user), return the user ID or channel ID from RouteNotificationFor("slack"):

main.go
func (u User) RouteNotificationFor(channel string) any {
    if channel == "slack" {
        return u.SlackUserID // "U01ABCD..." for DMs, or "#channel-name"
    }
    return nil
}

Configuration reference

WebhookConfig

FieldTypeRequiredDescription
URLstringyesIncoming webhook URL from the Slack app settings.
Timeouttime.DurationnoHTTP timeout per send. Default: 30s.

BotConfig

FieldTypeRequiredDescription
TokenstringyesBot User OAuth Token (xoxb-...).
DefaultChannelstringnoFallback channel if RouteNotificationFor returns empty.
Timeouttime.DurationnoHTTP timeout per send. Default: 30s.

Troubleshooting

  • channel_not_found — bot isn't invited, or channel ID is wrong. Invite with /invite @YourBot.
  • not_allowed_token_type — you passed a user token (xoxp-) where a bot token (xoxb-) was expected.
  • Webhook returns 200 but nothing appears — webhook is for a different workspace or the channel was archived.