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
- Go to https://api.slack.com/apps → Create New App → pick a workspace.
- Enable Incoming Webhooks.
- Add New Webhook to Workspace, pick a channel.
- Copy the webhook URL.
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
- In the same app settings, enable Bots.
- Add OAuth scopes:
chat:write,chat:write.public. - Install the app to your workspace and copy the Bot User OAuth Token (starts with
xoxb-). - Invite the bot into any channel it should post to (
/invite @YourBot).
notifier.RegisterChannel("slack", slack.NewBot(slack.BotConfig{
Token: os.Getenv("SLACK_BOT_TOKEN"),
DefaultChannel: "#alerts",
}))Sending
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"):
func (u User) RouteNotificationFor(channel string) any {
if channel == "slack" {
return u.SlackUserID // "U01ABCD..." for DMs, or "#channel-name"
}
return nil
}Configuration reference
WebhookConfig
| Field | Type | Required | Description |
|---|---|---|---|
URL | string | yes | Incoming webhook URL from the Slack app settings. |
Timeout | time.Duration | no | HTTP timeout per send. Default: 30s. |
BotConfig
| Field | Type | Required | Description |
|---|---|---|---|
Token | string | yes | Bot User OAuth Token (xoxb-...). |
DefaultChannel | string | no | Fallback channel if RouteNotificationFor returns empty. |
Timeout | time.Duration | no | HTTP 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.