-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_test.go
More file actions
38 lines (32 loc) · 971 Bytes
/
Copy pathexample_test.go
File metadata and controls
38 lines (32 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package notify_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/go-pkgz/notify"
)
// ExampleSend demonstrates sending a notification to a webhook destination.
// Webhook is the only notifier which needs no credentials, so this example is runnable as is.
func ExampleSend() {
// stand-in for the real webhook receiver
receiver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, _ = fmt.Printf("webhook received: %s\n", body)
}))
defer receiver.Close()
notifiers := []notify.Notifier{notify.NewWebhook(notify.WebhookParams{})}
if err := notify.Send(context.Background(), notifiers, receiver.URL, "Hello, world!"); err != nil {
_, _ = fmt.Printf("send error: %s\n", err)
return
}
_, _ = fmt.Println("sent")
// Output:
// webhook received: Hello, world!
// sent
}