go-notificationgo-notification
Channels/Chat

Discord

Post notifications to Discord channels via webhooks.

Discord webhooks are the fastest way to post into a Discord channel from external services. One URL per channel, no OAuth, no bot.

Setup

  1. In Discord, open the target channel's settings → IntegrationsWebhooksNew Webhook.
  2. Name it, optionally set an avatar, pick a channel.
  3. Copy the Webhook URL.
main.go
import "github.com/gopackx/go-notification/channel/chat/discord"

notifier.RegisterChannel(discord.New(discord.Config{
    Name:       "discord",
    WebhookURL: os.Getenv("DISCORD_WEBHOOK_URL"),
}))

Sending

main.go
import "github.com/gopackx/go-notification/channel/chat"

func (n BuildFailed) Via(u notification.Notifiable) []string {
    return []string{"discord"}
}

func (n BuildFailed) ToChat(u notification.Notifiable) *chat.Message {
    return chat.NewMessage().
        SetText("Build failed on " + n.Branch).
        AddDiscordEmbed(chat.DiscordEmbed{
            Title:       n.Service,
            Description: n.Error,
            Color:       0xFF0000, // red
            Fields: []chat.DiscordField{
                {Name: "Commit", Value: n.Commit, Inline: true},
                {Name: "Author", Value: n.Author, Inline: true},
            },
        })
}

Configuration reference

FieldTypeRequiredDescription
WebhookURLstringyesDiscord webhook URL (includes token — treat as a secret).
DefaultUsernamestringnoDefault bot name; override per message via DiscordUsername.
NamestringnoChannel name used by Via(). Defaults to "chat".
Timeouttime.DurationnoHTTP timeout per send. Default: 30s.

Tips

  • Rate limits — Discord caps webhook traffic at 30 requests per minute per webhook. Above that, requests get 429 Too Many Requests. Either throttle in-app, batch into fewer messages, or use multiple webhooks and round-robin.
  • Embed limits — 10 embeds per message, 6000 characters total across all embeds, 25 fields per embed. The builder won't stop you from exceeding these; Discord will reject.
  • Secret in URL — rotate the webhook (delete + recreate) if you leak the URL.

Troubleshooting

  • 401 Unauthorized — webhook was deleted or regenerated.
  • 400 Bad Request — usually a malformed embed (too many fields, too long, or invalid color). Error body includes which field.
  • Messages appear with wrong username/avatar — your Username override may be blocked by server rules (rare).