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 manages the creation and routing of notification services.
30 var defaultRouter = router.ServiceRouter{
31 Timeout: router.DefaultTimeout,
32 }
33
34 // Send delivers a notification message using the specified URL.
35 func Send(rawURL, message string) error {
36 service, err := defaultRouter.Locate(rawURL)
37 if err != nil {
38 return fmt.Errorf("locating service for URL %q: %w", rawURL, err)
39 }
40
41 if err := service.Send(message, &types.Params{}); err != nil {
42 return 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 CreateSender(rawURLs ...string) (*router.ServiceRouter, error) {
52 serviceRouter, err := router.NewWithOptions(nil, types.SenderOptions{}, rawURLs...)
53 if err != nil {
54 return nil, 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 CreateSenderWithOptions(opts types.SenderOptions, rawURLs ...string) (*router.ServiceRouter, error) {
63 serviceRouter, err := router.NewWithOptions(nil, opts, rawURLs...)
64 if err != nil {
65 return nil, 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 NewSender(logger types.StdLogger, serviceURLs ...string) (*router.ServiceRouter, error) {
75 serviceRouter, err := router.NewWithOptions(logger, types.SenderOptions{}, serviceURLs...)
76 if err != nil {
77 return nil, 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 HTTP
85 // services and DialContext for TCP services (e.g. for SSRF protection or
86 // custom proxies/TLS).
87 func NewSenderWithOptions(logger types.StdLogger, opts types.SenderOptions, serviceURLs ...string) (*router.ServiceRouter, error) {
88 serviceRouter, err := router.NewWithOptions(logger, opts, serviceURLs...)
89 if err != nil {
90 return nil, fmt.Errorf("creating sender with options for URLs %v: %w", serviceURLs, err)
91 }
92
93 return serviceRouter, nil
94 }
95
96 // SetLogger configures the logger for all services in the default router.
97 func SetLogger(logger types.StdLogger) {
98 defaultRouter.SetLogger(logger)
99 }
100
101 // Version returns the current Shoutrrr version.
102 func Version() string {
103 return meta.Version
104 }