| 1 | package generic |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "log" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | |
| 10 | "github.com/jarcoal/httpmock" |
| 11 | |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 | ) |
| 14 | |
| 15 | // Example demonstrates basic webhook sending to a generic endpoint. |
| 16 | func Example() { |
| 17 | // Activate HTTP mocking for the example |
| 18 | httpmock.Activate() |
| 19 | |
| 20 | defer httpmock.DeactivateAndReset() |
| 21 | |
| 22 | // Mock a successful webhook response |
| 23 | httpmock.RegisterResponder("POST", "https://api.example.com/webhook", |
| 24 | httpmock.NewStringResponder(200, "OK")) |
| 25 | |
| 26 | // Create and initialize the service |
| 27 | service := &Service{} |
| 28 | serviceURL, _ := url.Parse("generic://api.example.com/webhook") |
| 29 | |
| 30 | err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 31 | if err != nil { |
| 32 | fmt.Println("Error:", err) |
| 33 | |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | // Send a notification |
| 38 | err = service.Send("Hello, webhook!", nil) |
| 39 | if err != nil { |
| 40 | fmt.Println("Error:", err) |
| 41 | |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | fmt.Println("Notification sent successfully") |
| 46 | // Output: Notification sent successfully |
| 47 | } |
| 48 | |
| 49 | // ExampleJSONTemplate demonstrates sending a JSON-formatted payload. |
| 50 | func ExampleJSONTemplate() { |
| 51 | // Activate HTTP mocking for the example |
| 52 | httpmock.Activate() |
| 53 | |
| 54 | defer httpmock.DeactivateAndReset() |
| 55 | |
| 56 | // Mock a successful webhook response that accepts any JSON payload |
| 57 | httpmock.RegisterResponder("POST", "https://api.example.com/webhook", |
| 58 | httpmock.NewStringResponder(200, "OK")) |
| 59 | |
| 60 | // Create and initialize the service with JSON template |
| 61 | service := &Service{} |
| 62 | serviceURL, _ := url.Parse("generic://api.example.com/webhook?template=json") |
| 63 | |
| 64 | err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 65 | if err != nil { |
| 66 | fmt.Println("Error:", err) |
| 67 | |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | // Send a notification with title |
| 72 | params := types.Params{"title": "Alert"} |
| 73 | |
| 74 | err = service.Send("System is down", ¶ms) |
| 75 | if err != nil { |
| 76 | fmt.Println("Error:", err) |
| 77 | |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | fmt.Println("JSON notification sent successfully") |
| 82 | // Output: JSON notification sent successfully |
| 83 | } |
| 84 | |
| 85 | // Example_customHeaders demonstrates sending with custom HTTP headers. |
| 86 | func Example_customHeaders() { |
| 87 | // Activate HTTP mocking for the example |
| 88 | httpmock.Activate() |
| 89 | |
| 90 | defer httpmock.DeactivateAndReset() |
| 91 | |
| 92 | // Mock a successful webhook response that checks for custom header |
| 93 | httpmock.RegisterResponder("POST", "https://api.example.com/webhook", |
| 94 | func(req *http.Request) (*http.Response, error) { |
| 95 | if req.Header.Get("Authorization") == "Bearer token123" && |
| 96 | req.Header.Get("X-Custom") == "value" { |
| 97 | return httpmock.NewStringResponse(200, "OK"), nil |
| 98 | } |
| 99 | |
| 100 | return httpmock.NewStringResponse(401, "Unauthorized"), nil |
| 101 | }) |
| 102 | |
| 103 | // Create and initialize the service with custom headers |
| 104 | service := &Service{} |
| 105 | serviceURL, _ := url.Parse( |
| 106 | "generic://api.example.com/webhook?@Authorization=Bearer%20token123&@X-Custom=value", |
| 107 | ) |
| 108 | |
| 109 | err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 110 | if err != nil { |
| 111 | fmt.Println("Error:", err) |
| 112 | |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | // Send a notification |
| 117 | err = service.Send("Authenticated message", nil) |
| 118 | if err != nil { |
| 119 | fmt.Println("Error:", err) |
| 120 | |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | fmt.Println("Notification with custom headers sent successfully") |
| 125 | // Output: Notification with custom headers sent successfully |
| 126 | } |
| 127 | |
| 128 | // Example_configurationParsing demonstrates parsing various configuration options from URL. |
| 129 | func Example_configurationParsing() { |
| 130 | // Create a service URL with various configuration options |
| 131 | serviceURL, _ := url.Parse( |
| 132 | "generic://webhook.example.com/alert?template=json&disabletls=yes&method=POST&titlekey=alert_title&messagekey=alert_message&$source=shoutrrr&@Authorization=Bearer%20token", |
| 133 | ) |
| 134 | |
| 135 | // Initialize the service |
| 136 | service := &Service{} |
| 137 | |
| 138 | err := service.Initialize(serviceURL, log.New(io.Discard, "", 0)) |
| 139 | if err != nil { |
| 140 | fmt.Println("Error:", err) |
| 141 | |
| 142 | return |
| 143 | } |
| 144 | |
| 145 | // Display parsed configuration |
| 146 | fmt.Printf("Webhook URL: %s\n", service.Config.WebhookURL().String()) |
| 147 | fmt.Printf("Template: %s\n", service.Config.Template) |
| 148 | fmt.Printf("TLS Disabled: %t\n", service.Config.DisableTLS) |
| 149 | fmt.Printf("Method: %s\n", service.Config.RequestMethod) |
| 150 | fmt.Printf("Title Key: %s\n", service.Config.TitleKey) |
| 151 | fmt.Printf("Message Key: %s\n", service.Config.MessageKey) |
| 152 | fmt.Printf("Extra Data: %v\n", service.Config.extraData) |
| 153 | fmt.Printf("Headers: %v\n", service.Config.headers) |
| 154 | |
| 155 | // Output: |
| 156 | // Webhook URL: http://webhook.example.com/alert |
| 157 | // Template: json |
| 158 | // TLS Disabled: true |
| 159 | // Method: POST |
| 160 | // Title Key: alert_title |
| 161 | // Message Key: alert_message |
| 162 | // Extra Data: map[source:shoutrrr] |
| 163 | // Headers: map[Authorization:Bearer token] |
| 164 | } |