| 1 |
package notifiarr |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"log" |
| 7 |
"net/url" |
| 8 |
|
| 9 |
"github.com/jarcoal/httpmock" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 |
) |
| 13 |
|
| 14 |
// Example demonstrates basic notification sending to Notifiarr. |
| 15 |
func Example() { |
| 16 |
// Activate HTTP mocking for the example |
| 17 |
httpmock.Activate() |
| 18 |
|
| 19 |
defer httpmock.DeactivateAndReset() |
| 20 |
|
| 21 |
// Mock a successful Notifiarr API response |
| 22 |
httpmock.RegisterResponder( |
| 23 |
"POST", |
| 24 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 25 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 26 |
) |
| 27 |
|
| 28 |
// Create and initialize the service |
| 29 |
service := &Service{} |
| 30 |
serviceURL, _ := url.Parse("notifiarr://test-api-key") |
| 31 |
|
| 32 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 33 |
if err != nil { |
| 34 |
fmt.Println("Error:", err) |
| 35 |
|
| 36 |
return |
| 37 |
} |
| 38 |
|
| 39 |
// Send a notification |
| 40 |
err = service.Send("Hello from Notifiarr!", nil) |
| 41 |
if err != nil { |
| 42 |
fmt.Println("Error:", err) |
| 43 |
|
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
// Output: Notification sent successfully |
| 48 |
fmt.Println("Notification sent successfully") |
| 49 |
} |
| 50 |
|
| 51 |
// Example_eventID demonstrates sending a notification with an event ID for message updates. |
| 52 |
func Example_eventID() { |
| 53 |
// Activate HTTP mocking for the example |
| 54 |
httpmock.Activate() |
| 55 |
|
| 56 |
defer httpmock.DeactivateAndReset() |
| 57 |
|
| 58 |
// Mock a successful Notifiarr API response |
| 59 |
httpmock.RegisterResponder( |
| 60 |
"POST", |
| 61 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 62 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 63 |
) |
| 64 |
|
| 65 |
// Create and initialize the service |
| 66 |
service := &Service{} |
| 67 |
serviceURL, _ := url.Parse("notifiarr://test-api-key") |
| 68 |
|
| 69 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 70 |
if err != nil { |
| 71 |
fmt.Println("Error:", err) |
| 72 |
|
| 73 |
return |
| 74 |
} |
| 75 |
|
| 76 |
// Send a notification with event ID and title |
| 77 |
params := types.Params{ |
| 78 |
"id": "alert-123", |
| 79 |
"title": "System Alert", |
| 80 |
} |
| 81 |
|
| 82 |
err = service.Send("Server CPU usage is high", ¶ms) |
| 83 |
if err != nil { |
| 84 |
fmt.Println("Error:", err) |
| 85 |
|
| 86 |
return |
| 87 |
} |
| 88 |
|
| 89 |
// Output: Notification with event ID sent successfully |
| 90 |
fmt.Println("Notification with event ID sent successfully") |
| 91 |
} |
| 92 |
|
| 93 |
// Example_discordMentions demonstrates Discord mention parsing from message content. |
| 94 |
func Example_discordMentions() { |
| 95 |
// Activate HTTP mocking for the example |
| 96 |
httpmock.Activate() |
| 97 |
|
| 98 |
defer httpmock.DeactivateAndReset() |
| 99 |
|
| 100 |
// Mock a successful Notifiarr API response |
| 101 |
httpmock.RegisterResponder( |
| 102 |
"POST", |
| 103 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 104 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 105 |
) |
| 106 |
|
| 107 |
// Create and initialize the service |
| 108 |
service := &Service{} |
| 109 |
serviceURL, _ := url.Parse("notifiarr://test-api-key") |
| 110 |
|
| 111 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 112 |
if err != nil { |
| 113 |
fmt.Println("Error:", err) |
| 114 |
|
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
// Send a notification with Discord mentions |
| 119 |
message := "Alert for <@123456789> and role <@&987654321>" |
| 120 |
|
| 121 |
err = service.Send(message, nil) |
| 122 |
if err != nil { |
| 123 |
fmt.Println("Error:", err) |
| 124 |
|
| 125 |
return |
| 126 |
} |
| 127 |
|
| 128 |
// Output: Notification with mentions sent successfully |
| 129 |
fmt.Println("Notification with mentions sent successfully") |
| 130 |
} |
| 131 |
|
| 132 |
// Example_imageThumbnail demonstrates sending a notification with a thumbnail image. |
| 133 |
func Example_imageThumbnail() { |
| 134 |
// Activate HTTP mocking for the example |
| 135 |
httpmock.Activate() |
| 136 |
|
| 137 |
defer httpmock.DeactivateAndReset() |
| 138 |
|
| 139 |
// Mock a successful Notifiarr API response |
| 140 |
httpmock.RegisterResponder( |
| 141 |
"POST", |
| 142 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 143 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 144 |
) |
| 145 |
|
| 146 |
// Create and initialize the service with thumbnail |
| 147 |
service := &Service{} |
| 148 |
serviceURL, _ := url.Parse("notifiarr://test-api-key?thumbnail=https://example.com/alert.png") |
| 149 |
|
| 150 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 151 |
if err != nil { |
| 152 |
fmt.Println("Error:", err) |
| 153 |
|
| 154 |
return |
| 155 |
} |
| 156 |
|
| 157 |
// Send a notification |
| 158 |
err = service.Send("System alert with thumbnail", nil) |
| 159 |
if err != nil { |
| 160 |
fmt.Println("Error:", err) |
| 161 |
|
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
// Output: Notification with thumbnail sent successfully |
| 166 |
fmt.Println("Notification with thumbnail sent successfully") |
| 167 |
} |
| 168 |
|
| 169 |
// Example_colorCustomization demonstrates sending a notification with custom color. |
| 170 |
func Example_colorCustomization() { |
| 171 |
// Activate HTTP mocking for the example |
| 172 |
httpmock.Activate() |
| 173 |
|
| 174 |
defer httpmock.DeactivateAndReset() |
| 175 |
|
| 176 |
// Mock a successful Notifiarr API response |
| 177 |
httpmock.RegisterResponder( |
| 178 |
"POST", |
| 179 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 180 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 181 |
) |
| 182 |
|
| 183 |
// Create and initialize the service with color |
| 184 |
service := &Service{} |
| 185 |
serviceURL, _ := url.Parse("notifiarr://test-api-key?color=16711680") // Red color |
| 186 |
|
| 187 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 188 |
if err != nil { |
| 189 |
fmt.Println("Error:", err) |
| 190 |
|
| 191 |
return |
| 192 |
} |
| 193 |
|
| 194 |
// Send a notification |
| 195 |
err = service.Send("Red alert notification", nil) |
| 196 |
if err != nil { |
| 197 |
fmt.Println("Error:", err) |
| 198 |
|
| 199 |
return |
| 200 |
} |
| 201 |
|
| 202 |
// Output: Colored notification sent successfully |
| 203 |
fmt.Println("Colored notification sent successfully") |
| 204 |
} |
| 205 |
|
| 206 |
// Example_urlParameterConfiguration demonstrates configuring notifications via URL parameters. |
| 207 |
func Example_urlParameterConfiguration() { |
| 208 |
// Activate HTTP mocking for the example |
| 209 |
httpmock.Activate() |
| 210 |
|
| 211 |
defer httpmock.DeactivateAndReset() |
| 212 |
|
| 213 |
// Mock a successful Notifiarr API response |
| 214 |
httpmock.RegisterResponder( |
| 215 |
"POST", |
| 216 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 217 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 218 |
) |
| 219 |
|
| 220 |
// Create and initialize the service with multiple parameters |
| 221 |
service := &Service{} |
| 222 |
serviceURL, _ := url.Parse( |
| 223 |
"notifiarr://test-api-key?channel=123456789012345678&thumbnail=https://example.com/icon.png&image=https://example.com/image.png&color=65280", |
| 224 |
) |
| 225 |
|
| 226 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 227 |
if err != nil { |
| 228 |
fmt.Println("Error:", err) |
| 229 |
|
| 230 |
return |
| 231 |
} |
| 232 |
|
| 233 |
// Send a notification |
| 234 |
err = service.Send("Fully configured notification with image", nil) |
| 235 |
if err != nil { |
| 236 |
fmt.Println("Error:", err) |
| 237 |
|
| 238 |
return |
| 239 |
} |
| 240 |
|
| 241 |
// Output: Fully configured notification with image sent successfully |
| 242 |
fmt.Println("Fully configured notification with image sent successfully") |
| 243 |
} |
| 244 |
|
| 245 |
// Example_imageAndThumbnail demonstrates sending a notification with both image and thumbnail URLs. |
| 246 |
func Example_imageAndThumbnail() { |
| 247 |
// Activate HTTP mocking for the example |
| 248 |
httpmock.Activate() |
| 249 |
|
| 250 |
defer httpmock.DeactivateAndReset() |
| 251 |
|
| 252 |
// Mock a successful Notifiarr API response |
| 253 |
httpmock.RegisterResponder( |
| 254 |
"POST", |
| 255 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 256 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 257 |
) |
| 258 |
|
| 259 |
// Create and initialize the service with both image and thumbnail |
| 260 |
service := &Service{} |
| 261 |
serviceURL, _ := url.Parse( |
| 262 |
"notifiarr://test-api-key?thumbnail=https://example.com/thumbnail.png&image=https://example.com/full-image.png", |
| 263 |
) |
| 264 |
|
| 265 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 266 |
if err != nil { |
| 267 |
fmt.Println("Error:", err) |
| 268 |
|
| 269 |
return |
| 270 |
} |
| 271 |
|
| 272 |
// Send a notification |
| 273 |
err = service.Send("Notification with both thumbnail and full image", nil) |
| 274 |
if err != nil { |
| 275 |
fmt.Println("Error:", err) |
| 276 |
|
| 277 |
return |
| 278 |
} |
| 279 |
|
| 280 |
// Output: Notification with image and thumbnail sent successfully |
| 281 |
fmt.Println("Notification with image and thumbnail sent successfully") |
| 282 |
} |
| 283 |
|
| 284 |
// Example_advancedTextFields demonstrates sending a notification with advanced text fields including icon, content, and footer. |
| 285 |
func Example_advancedTextFields() { |
| 286 |
// Activate HTTP mocking for the example |
| 287 |
httpmock.Activate() |
| 288 |
|
| 289 |
defer httpmock.DeactivateAndReset() |
| 290 |
|
| 291 |
// Mock a successful Notifiarr API response |
| 292 |
httpmock.RegisterResponder( |
| 293 |
"POST", |
| 294 |
"https://notifiarr.com/api/v1/notification/passthrough/test-api-key", |
| 295 |
httpmock.NewStringResponder(200, `{"success": true}`), |
| 296 |
) |
| 297 |
|
| 298 |
// Create and initialize the service |
| 299 |
service := &Service{} |
| 300 |
serviceURL, _ := url.Parse("notifiarr://test-api-key") |
| 301 |
|
| 302 |
err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 303 |
if err != nil { |
| 304 |
fmt.Println("Error:", err) |
| 305 |
|
| 306 |
return |
| 307 |
} |
| 308 |
|
| 309 |
// Send a notification with advanced text fields |
| 310 |
params := types.Params{ |
| 311 |
"title": "System Status Update", |
| 312 |
"icon": "https://example.com/icon.png", |
| 313 |
"content": "Detailed content about the system status", |
| 314 |
"footer": "Generated by Monitoring System v2.1", |
| 315 |
} |
| 316 |
|
| 317 |
err = service.Send("System is running normally", ¶ms) |
| 318 |
if err != nil { |
| 319 |
fmt.Println("Error:", err) |
| 320 |
|
| 321 |
return |
| 322 |
} |
| 323 |
|
| 324 |
// Output: Notification with advanced text fields sent successfully |
| 325 |
fmt.Println("Notification with advanced text fields sent successfully") |
| 326 |
} |