| 1 | # Using the Shoutrrr Package |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | The Shoutrrr Go package (`github.com/nicholas-fedor/shoutrrr`) enables sending notifications to various services (e.g., `discord`, `slack`, `telegram`, `smtp`, etc.) using service URLs. It provides two primary methods: a direct `Send` function for simple use cases and a `Sender` struct for advanced scenarios with multiple URLs, message queuing, and parameter customization. |
| 6 | |
| 7 | ## Usage |
| 8 | |
| 9 | ```go title="Go Import Statement" |
| 10 | import "github.com/nicholas-fedor/shoutrrr" |
| 11 | ``` |
| 12 | |
| 13 | ### Direct Send |
| 14 | |
| 15 | Sends a notification to a single service URL. |
| 16 | |
| 17 | - **Function**: `shoutrrr.Send(url string, message string) error` |
| 18 | - **Behavior**: Initializes a service from the provided URL, sends the message, and returns any error. |
| 19 | |
| 20 | !!! Example |
| 21 | ```go title="Send to a Single Slack URL" |
| 22 | url := "slack://token-a/token-b/token-c" |
| 23 | err := shoutrrr.Send(url, "Hello, Slack!") |
| 24 | if err != nil { |
| 25 | fmt.Println("Error:", err) |
| 26 | } |
| 27 | ``` |
| 28 | |
| 29 | ### Sender |
| 30 | |
| 31 | Creates a `Sender` (`*ServiceRouter`) to manage multiple service URLs, support message queuing, and allow parameter customization. |
| 32 | |
| 33 | - **Function**: `shoutrrr.CreateSender(urls ...string) (*ServiceRouter, error)` |
| 34 | - **Methods**: |
| 35 | - `Send(message string, params *types.Params) []error`: Sends a message to all configured services. |
| 36 | - `SendItems(items []types.MessageItem, params types.Params) []error`: Sends structured message items to services that support rich formatting. |
| 37 | - `SendAsync(message string, params *types.Params) chan error`: Sends a message asynchronously and returns a channel of errors. |
| 38 | - `Enqueue(message string, v ...interface{})`: Queues a formatted message for later sending. |
| 39 | - `Flush(params *types.Params)`: Sends all queued messages and resets the queue. |
| 40 | - **Behavior**: Deduplicates URLs, initializes services, and supports asynchronous sending with a 10-second timeout per service. |
| 41 | |
| 42 | !!! Example |
| 43 | ```go title="Create Sender with Multiple URLs" |
| 44 | urls := []string{ |
| 45 | "slack://token-a/token-b/token-c", |
| 46 | "telegram://110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw@telegram?channels=@mychannel", |
| 47 | } |
| 48 | sender, err := shoutrrr.CreateSender(urls...) |
| 49 | if err != nil { |
| 50 | log.Fatal(err) |
| 51 | } |
| 52 | params := types.Params{} |
| 53 | params.SetTitle("Test Notification") |
| 54 | errs := sender.Send("Hello, world!", ¶ms) |
| 55 | if len(errs) > 0 { |
| 56 | for _, err := range errs { |
| 57 | fmt.Println("Error:", err) |
| 58 | } |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ### Message Queuing |
| 63 | |
| 64 | Allows queuing messages for deferred sending, useful for aggregating notifications during a process. |
| 65 | |
| 66 | - Queues messages with `Enqueue` and sends them with `Flush`. Queued messages use the `params` provided during `Flush`. |
| 67 | |
| 68 | <!-- markdownlint-disable --> |
| 69 | !!! Example |
| 70 | ```go title="Queue and Flush Notifications" |
| 71 | url := "discord://abc123@123456789" |
| 72 | sender, err := shoutrrr.CreateSender(url) |
| 73 | if err != nil { |
| 74 | log.Fatal(err) |
| 75 | } |
| 76 | defer sender.Flush(nil) |
| 77 | |
| 78 | sender.Enqueue("Started doing work") |
| 79 | if err := doWork(); err != nil { |
| 80 | sender.Enqueue("Error: %v", err) |
| 81 | return |
| 82 | } |
| 83 | sender.Enqueue("Work completed successfully!") |
| 84 | ``` |
| 85 | <!-- markdownlint-restore --> |
| 86 | |
| 87 | ### SendItems and RichSender |
| 88 | |
| 89 | Sends structured message items to services that support rich formatting. |
| 90 | Services implementing `types.RichSender` receive the full `[]types.MessageItem` slice, preserving level, fields, and file attachments. |
| 91 | Services that do not implement `RichSender` fall back to plain text automatically. |
| 92 | |
| 93 | - **Method**: `SendItems(items []types.MessageItem, params types.Params) []error` |
| 94 | - **Behavior**: Dispatches to `ContextAttachmentSender` if available, otherwise `RichSender`, otherwise falls back to `Send` with plain text. |
| 95 | |
| 96 | !!! Example |
| 97 | ```go title="Send Structured Message Items" |
| 98 | items := []types.MessageItem{ |
| 99 | {Text: "Deployment complete", Level: types.Info}, |
| 100 | {Text: "Rollback available", Level: types.Warning}, |
| 101 | } |
| 102 | sender, err := shoutrrr.CreateSender("discord://webhook") |
| 103 | if err != nil { |
| 104 | log.Fatal(err) |
| 105 | } |
| 106 | errs := sender.SendItems(items, types.Params{}) |
| 107 | ``` |
| 108 | |
| 109 | ### Context Propagation |
| 110 | |
| 111 | Services that implement `types.ContextSender` or `types.ContextAttachmentSender` receive a `context.Context` derived from the router's base context with a per-service timeout. |
| 112 | This enables cancellation and deadline propagation without changing the existing `Sender` or `RichSender` contracts. |
| 113 | |
| 114 | ### Per-Target Errors |
| 115 | |
| 116 | `*ServiceRouter.Send`, `*ServiceRouter.SendAsync`, `*ServiceRouter.SendItems`, and `*ServiceRouter.Route` return one error per unique configured target, in the deduplicated target order produced by `CreateSender`. Each error is wrapped in `*types.TargetError`, which carries the service URL/ID and supports `errors.Unwrap`, `errors.Is`, and `errors.As`. |
| 117 | |
| 118 | !!! Example |
| 119 | ```go title="Handle Per-Target Errors" |
| 120 | errs := sender.Send("deploy complete", nil) |
| 121 | for i, err := range errs { |
| 122 | if err == nil { |
| 123 | continue |
| 124 | } |
| 125 | var targetErr *types.TargetError |
| 126 | if errors.As(err, &targetErr) { |
| 127 | log.Printf("failed to send to %s: %v", targetErr.URL, targetErr.Err) |
| 128 | } |
| 129 | } |
| 130 | ``` |
| 131 | |
| 132 | ### Message Levels |
| 133 | |
| 134 | Services that support severity or priority can receive a semantic level through the `level` param. |
| 135 | Shoutrrr defines five levels: `Unknown`, `Debug`, `Info`, `Warning`, and `Error`. |
| 136 | The default is `Info`. |
| 137 | |
| 138 | !!! Example |
| 139 | ```go title="Set Message Level" |
| 140 | params := types.Params{} |
| 141 | params.SetLevel(types.Warning) |
| 142 | params.SetTitle("Disk usage high") |
| 143 | sender, err := shoutrrr.CreateSender("discord://webhook") |
| 144 | if err != nil { |
| 145 | log.Fatal(err) |
| 146 | } |
| 147 | errs := sender.Send("Root partition at 92%", ¶ms) |
| 148 | ``` |
| 149 | |
| 150 | ### Format Conversion |
| 151 | |
| 152 | Converts message bodies between supported formats. |
| 153 | |
| 154 | ```go title="Convert Message Format" |
| 155 | import "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 156 | |
| 157 | body, err := format.ConvertFormat("Hello **world**", "markdown", "text") |
| 158 | ``` |
| 159 | |
| 160 | Supported conversions: `text` ↔ `markdown` ↔ `html`. |
| 161 | |
| 162 | ### Service Discovery |
| 163 | |
| 164 | Enumerates or checks available notification services without constructing a router. |
| 165 | |
| 166 | ```go title="Discover Available Services" |
| 167 | import "github.com/nicholas-fedor/shoutrrr/pkg/services" |
| 168 | |
| 169 | for _, schema := range services.SupportedSchemas() { |
| 170 | fmt.Println(schema) |
| 171 | } |
| 172 | |
| 173 | if services.SupportsSchema("discord") { |
| 174 | // ... |
| 175 | } |
| 176 | ``` |
| 177 | |
| 178 | ## Examples |
| 179 | |
| 180 | <!-- markdownlint-disable --> |
| 181 | ### Send with Parameters and Error Handling |
| 182 | |
| 183 | !!! Example |
| 184 | ```go title="Send with Title and Error Handling" |
| 185 | url := "discord://abc123@123456789" |
| 186 | sender, err := shoutrrr.CreateSender(url) |
| 187 | if err != nil { |
| 188 | log.Fatal(err) |
| 189 | } |
| 190 | params := types.Params{} |
| 191 | params.SetTitle("Alert") |
| 192 | errs := sender.Send("System alert!", ¶ms) |
| 193 | if len(errs) > 0 { |
| 194 | for _, err := range errs { |
| 195 | fmt.Println("Error:", err) |
| 196 | } |
| 197 | } |
| 198 | ``` |
| 199 | |
| 200 | ```text title="Expected Output (Success)" |
| 201 | (No output on success) |
| 202 | ``` |
| 203 | |
| 204 | ```text title="Expected Output (Error)" |
| 205 | Error: failed to send message: unexpected response status code |
| 206 | ``` |
| 207 | |
| 208 | ### Send to Multiple Services with Queuing |
| 209 | |
| 210 | !!! Example |
| 211 | ```go title="Queue Messages for Multiple Services" |
| 212 | urls := []string{ |
| 213 | "slack://token-a/token-b/token-c", |
| 214 | "discord://abc123@123456789", |
| 215 | } |
| 216 | sender, err := shoutrrr.CreateSender(urls...) |
| 217 | if err != nil { |
| 218 | log.Fatal(err) |
| 219 | } |
| 220 | start := time.Now() |
| 221 | params := types.Params{} |
| 222 | params.SetTitle("Task Summary") |
| 223 | defer sender.Flush(¶ms) |
| 224 | sender.Enqueue("Task started") |
| 225 | time.Sleep(time.Second) |
| 226 | sender.Enqueue("Task finished in %v", time.Now().Sub(start)) |
| 227 | ``` |
| 228 | |
| 229 | ```text title="Expected Output (Success)" |
| 230 | (No output on success) |
| 231 | ``` |
| 232 | |
| 233 | ```text title="Expected Output (Error)" |
| 234 | Error: failed to initialize service: invalid URL format |
| 235 | ``` |
| 236 | <!-- markdownlint-restore --> |
| 237 | |
| 238 | ## Notes |
| 239 | |
| 240 | - **Error Handling**: `Send` returns a single error. `Sender.Send`, `SendItems`, and `SendAsync` return one error per service. Check `len(errs) > 0` to handle failures. Each error is wrapped in `*types.TargetError` with the service URL. |
| 241 | - **Parameters**: `params` is a `*types.Params` value for `Send`, `SendAsync`, and `Flush`. `SendItems` accepts `types.Params` by value. Use setter methods such as `SetTitle`, `SetMessage`, and `SetLevel` to configure service-specific options. Use `shoutrrr docs` to view supported parameters for each service. |
| 242 | - **Timeouts**: Each service send operation has a 10-second timeout. |
| 243 | - **Deduplication**: Duplicate URLs are automatically removed when creating a `Sender`. |