| 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 shoutrrrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/ |
| 20 | |
| 21 | import ( |
| 22 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 23 | |
| 24 | "github.com/nicholas-fedor/shoutrrr/internal/metascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/internal/meta`/" |
| 25 | "github.com/nicholas-fedor/shoutrrr/pkg/routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/" |
| 26 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 27 | ) |
| 28 | |
| 29 | // defaultRouter manages the creation and routing of notification services. |
| 30 | var defaultRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/defaultRouter. = routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.ServiceRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#{ |
| 31 | Timeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#Timeout.: routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.DefaultTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/DefaultTimeout., |
| 32 | } |
| 33 | |
| 34 | // Send delivers a notification message using the specified URL. |
| 35 | func Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/Send().(rawURL, message string) error { |
| 36 | service, err := defaultRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/defaultRouter..Locatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#Locate().(rawURL) |
| 37 | if err != nil { |
| 38 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("locating service for URL %q: %w", rawURL, err) |
| 39 | } |
| 40 | |
| 41 | if err := service.Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Sender#Send.(message, &typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#{}); err != nil { |
| 42 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("sending message via service at %q: %w", rawURL, err) |
| 43 | } |
| 44 | |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | // CreateSender constructs a new service router for the given URLs without a logger. |
| 49 | // |
| 50 | // Deprecated: Use CreateSenderWithOptions. |
| 51 | func CreateSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/CreateSender().(rawURLs ...string) (*routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.ServiceRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#, error) { |
| 52 | serviceRouter, err := routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.NewWithOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/NewWithOptions().(nil, typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.SenderOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/SenderOptions#{}, rawURLs...) |
| 53 | if err != nil { |
| 54 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating sender for URLs %v: %w", rawURLs, err) |
| 55 | } |
| 56 | |
| 57 | return serviceRouter, nil |
| 58 | } |
| 59 | |
| 60 | // CreateSenderWithOptions constructs a new service router for the given URLs |
| 61 | // and SenderOptions without a logger. |
| 62 | func CreateSenderWithOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/CreateSenderWithOptions().(opts typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.SenderOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/SenderOptions#, rawURLs ...string) (*routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.ServiceRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#, error) { |
| 63 | serviceRouter, err := routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.NewWithOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/NewWithOptions().(nil, opts, rawURLs...) |
| 64 | if err != nil { |
| 65 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating sender with options for URLs %v: %w", rawURLs, err) |
| 66 | } |
| 67 | |
| 68 | return serviceRouter, nil |
| 69 | } |
| 70 | |
| 71 | // NewSender constructs a new service router with a logger for the given URLs. |
| 72 | // |
| 73 | // Deprecated: Use NewSenderWithOptions. |
| 74 | func NewSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/NewSender().(logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#, serviceURLs ...string) (*routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.ServiceRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#, error) { |
| 75 | serviceRouter, err := routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.NewWithOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/NewWithOptions().(logger, typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.SenderOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/SenderOptions#{}, serviceURLs...) |
| 76 | if err != nil { |
| 77 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating sender with logger for URLs %v: %w", serviceURLs, err) |
| 78 | } |
| 79 | |
| 80 | return serviceRouter, nil |
| 81 | } |
| 82 | |
| 83 | // NewSenderWithOptions constructs a new service router using the given logger, |
| 84 | // SenderOptions, and URLs. Use this to supply a custom HTTPClient for all |
| 85 | // outbound requests (e.g. for SSRF protection or custom proxies/TLS). |
| 86 | func NewSenderWithOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/NewSenderWithOptions().(logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#, opts typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.SenderOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/SenderOptions#, serviceURLs ...string) (*routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.ServiceRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#, error) { |
| 87 | serviceRouter, err := routerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/.NewWithOptionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/NewWithOptions().(logger, opts, serviceURLs...) |
| 88 | if err != nil { |
| 89 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . 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 SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/SetLogger().(logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#) { |
| 97 | defaultRouterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/defaultRouter..SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/router`/ServiceRouter#SetLogger().(logger) |
| 98 | } |
| 99 | |
| 100 | // Version returns the current Shoutrrr version. |
| 101 | func Versionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr`/Version().() string { |
| 102 | return metascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/internal/meta`/.Versionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/internal/meta`/Version. |
| 103 | } |