-
Notifications
You must be signed in to change notification settings - Fork 47
/
webhook_sender.go
146 lines (127 loc) · 4.05 KB
/
webhook_sender.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright (c) 2021 - Present. Blend Labs, Inc. All rights reserved
Blend Confidential - Restricted
*/
package slack
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/blend/go-sdk/ex"
"github.com/blend/go-sdk/r2"
"github.com/blend/go-sdk/webutil"
)
const (
// ErrNon200 is the exception class when a non-200 is returned from slack.
ErrNon200 = "slack; non-200 status code returned from remote"
)
var (
_ Sender = (*WebhookSender)(nil)
)
// New creates a new webhook sender.
func New(cfg Config) *WebhookSender {
return &WebhookSender{
Transport: new(http.Transport),
Config: cfg,
}
}
// WebhookSender sends slack webhooks.
type WebhookSender struct {
Transport *http.Transport
RequestDefaults []r2.Option
Config Config
}
// MessageDefaults returns default message options.
func (whs WebhookSender) MessageDefaults() []MessageOption {
return []MessageOption{
OptMessageUsernameOrDefault(whs.Config.Username),
OptMessageChannelOrDefault(whs.Config.Channel),
OptMessageIconEmojiOrDefault(whs.Config.IconEmoji),
OptMessageIconURLOrDefault(whs.Config.IconURL),
}
}
func (whs WebhookSender) send(ctx context.Context, message Message) (*http.Response, error) {
message = ApplyMessageOptions(message, whs.MessageDefaults()...)
options := append(whs.RequestDefaults,
r2.OptPost(),
r2.OptTransport(whs.Transport),
r2.OptJSONBody(message),
r2.OptHeaderValue(webutil.HeaderContentType, "application/json"),
)
return r2.New(whs.Config.Webhook, options...).Do()
}
// Send sends a slack hook.
func (whs WebhookSender) Send(ctx context.Context, message Message) error {
res, err := whs.send(ctx, message)
if err != nil {
return err
}
defer res.Body.Close()
if statusCode := res.StatusCode; statusCode < http.StatusOK || statusCode > 299 {
contents, _ := ioutil.ReadAll(res.Body)
return ex.New(ErrNon200, ex.OptMessage(string(contents)))
}
return nil
}
// SendAndReadResponse sends a slack hook and returns the deserialized response
func (whs WebhookSender) SendAndReadResponse(ctx context.Context, message Message) (*PostMessageResponse, error) {
res, err := whs.send(ctx, message)
if err != nil {
return nil, err
}
defer res.Body.Close()
var contents PostMessageResponse
err = json.NewDecoder(res.Body).Decode(&contents)
if err != nil {
return nil, ex.New(err)
}
if statusCode := res.StatusCode; statusCode < http.StatusOK || statusCode > 299 {
return &contents, ex.New(ErrNon200, ex.OptMessagef("%#v", contents))
}
return &contents, nil
}
// PostMessage posts a basic message to a given channel.
func (whs WebhookSender) PostMessage(channel, messageText string, options ...MessageOption) error {
message := Message{
Channel: channel,
Text: messageText,
}
for _, option := range options {
option(&message)
}
return whs.Send(context.Background(), message)
}
// PostMessageAndReadResponse posts a basic message to a given channel and returns the deserialized response
func (whs WebhookSender) PostMessageAndReadResponse(channel, messageText string, options ...MessageOption) (*PostMessageResponse, error) {
message := Message{
Channel: channel,
Text: messageText,
}
for _, option := range options {
option(&message)
}
return whs.SendAndReadResponse(context.Background(), message)
}
// PostMessageContext posts a basic message to a given chanel with a given context.
func (whs WebhookSender) PostMessageContext(ctx context.Context, channel, messageText string, options ...MessageOption) error {
message := Message{
Channel: channel,
Text: messageText,
}
for _, option := range options {
option(&message)
}
return whs.Send(ctx, message)
}
// PostMessageAndReadResponseContext posts a basic message to a given channel and returns the deserialized response
func (whs WebhookSender) PostMessageAndReadResponseContext(ctx context.Context, channel, messageText string, options ...MessageOption) (*PostMessageResponse, error) {
message := Message{
Channel: channel,
Text: messageText,
}
for _, option := range options {
option(&message)
}
return whs.SendAndReadResponse(ctx, message)
}