Debian Codegraph @

shoutrrr.go

raw
1 // Package shoutrrr provides a simple API for sending notifications to various services.
2 //
3 // The package supports multiple notification services including Slack, Discord,
4 // Telegram, and many others. Notifications are sent using service-specific URLs
5 // that contain all necessary configuration and authentication details.
6 //
7 // Basic usage:
8 //
9 // if err := shoutrrr.Send("slack://webhook/...", "Hello, World!"); err != nil {
10 // // handle error
11 // }
12 //
13 // For more complex scenarios, create a sender with multiple service URLs:
14 //
15 // sender, err := shoutrrr.CreateSender("slack://webhook/...", "discord://webhook/...")
16 //
17 // The package uses a default router, but you can also create custom routers
18 // using router.NewWithOptions for more control over the notification pipeline.
19 package shoutrrr
20
21 import (
22 "fmt"
23
24 "github.com/nicholas-fedor/shoutrrr/internal/meta"
25 "github.com/nicholas-fedor/shoutrrr/pkg/router"
26 "github.com/nicholas-fedor/shoutrrr/pkg/types"
27 )
28
29 // defaultRouter locates services for [Send].
30 // Its timeout is unset, so a send through this router uses each service's own budget.
31 var defaultRouter = router.ServiceRouter{}
32
33 // Send delivers a notification message using the specified URL.
34 func Send(rawURL, message string) error {
35 service, err := defaultRouter.Locate(rawURL)
36 if err != nil {
37 return fmt.Errorf("locating service for URL %q: %w", rawURL, err)
38 }
39
40 if err := service.Send(message, &types.Params{}); err != nil {
41 return fmt.Errorf("sending message via service at %q: %w", rawURL, err)
42 }
43
44 return nil
45 }
46
47 // CreateSender constructs a new service router for the given URLs without a logger.
48 //
49 // Deprecated: Use CreateSenderWithOptions.
50 func CreateSender(rawURLs ...string) (*router.ServiceRouter, error) {
51 serviceRouter, err := router.NewWithOptions(nil, types.SenderOptions{}, rawURLs...)
52 if err != nil {
53 return nil, fmt.Errorf("creating sender for URLs %v: %w", rawURLs, err)
54 }
55
56 return serviceRouter, nil
57 }
58
59 // CreateSenderWithOptions constructs a new service router for the given URLs
60 // and SenderOptions without a logger.
61 func CreateSenderWithOptions(opts types.SenderOptions, rawURLs ...string) (*router.ServiceRouter, error) {
62 serviceRouter, err := router.NewWithOptions(nil, opts, rawURLs...)
63 if err != nil {
64 return nil, fmt.Errorf("creating sender with options for URLs %v: %w", rawURLs, err)
65 }
66
67 return serviceRouter, nil
68 }
69
70 // NewSender constructs a new service router with a logger for the given URLs.
71 //
72 // Deprecated: Use NewSenderWithOptions.
73 func NewSender(logger types.StdLogger, serviceURLs ...string) (*router.ServiceRouter, error) {
74 serviceRouter, err := router.NewWithOptions(logger, types.SenderOptions{}, serviceURLs...)
75 if err != nil {
76 return nil, fmt.Errorf("creating sender with logger for URLs %v: %w", serviceURLs, err)
77 }
78
79 return serviceRouter, nil
80 }
81
82 // NewSenderWithOptions constructs a new service router using the given logger,
83 // SenderOptions, and URLs. Use this to supply a custom HTTPClient for HTTP
84 // services and DialContext for TCP services (e.g. for SSRF protection or
85 // custom proxies/TLS).
86 func NewSenderWithOptions(logger types.StdLogger, opts types.SenderOptions, serviceURLs ...string) (*router.ServiceRouter, error) {
87 serviceRouter, err := router.NewWithOptions(logger, opts, serviceURLs...)
88 if err != nil {
89 return nil, fmt.Errorf("creating sender with options for URLs %v: %w", serviceURLs, err)
90 }
91
92 return serviceRouter, nil
93 }
94
95 // SetLogger configures the logger for all services in the default router.
96 func SetLogger(logger types.StdLogger) {
97 defaultRouter.SetLogger(logger)
98 }
99
100 // Version returns the current Shoutrrr version.
101 func Version() string {
102 return meta.Version
103 }