-
Notifications
You must be signed in to change notification settings - Fork 26
/
send.go
126 lines (106 loc) · 3.2 KB
/
send.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
package goteamsnotify
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
// Known webhook URL prefixes for submitting messages to Microsoft Teams
const (
WebhookURLOfficecomPrefix = "https://outlook.office.com"
WebhookURLOffice365Prefix = "https://outlook.office365.com"
)
// API - interface of MS Teams notify
type API interface {
Send(webhookURL string, webhookMessage MessageCard) error
}
type teamsClient struct {
httpClient *http.Client
}
// NewClient - create a brand new client for MS Teams notify
func NewClient() API {
client := teamsClient{
httpClient: &http.Client{
Timeout: 5 * time.Second,
},
}
return &client
}
// Send - will post a notification to MS Teams webhook URL
func (c teamsClient) Send(webhookURL string, webhookMessage MessageCard) error {
// Validate input data
if valid, err := IsValidInput(webhookMessage, webhookURL); !valid {
return err
}
// prepare message
webhookMessageByte, _ := json.Marshal(webhookMessage)
webhookMessageBuffer := bytes.NewBuffer(webhookMessageByte)
// prepare request (error not possible)
req, _ := http.NewRequest(http.MethodPost, webhookURL, webhookMessageBuffer)
req.Header.Add("Content-Type", "application/json;charset=utf-8")
// do the request
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 299 {
err = errors.New("error on notification: " + res.Status)
return err
}
return nil
}
// helper --------------------------------------------------------------------------------------------------------------
// IsValidInput is a validation "wrapper" function. This function is intended
// to run current validation checks and offer easy extensibility for future
// validation requirements.
func IsValidInput(webhookMessage MessageCard, webhookURL string) (bool, error) {
// validate url
if valid, err := IsValidWebhookURL(webhookURL); !valid {
return false, err
}
// validate message
if valid, err := IsValidMessageCard(webhookMessage); !valid {
return false, err
}
return true, nil
}
// IsValidWebhookURL performs validation checks on the webhook URL used to
// submit messages to Microsoft Teams.
func IsValidWebhookURL(webhookURL string) (bool, error) {
switch {
case strings.HasPrefix(webhookURL, WebhookURLOfficecomPrefix):
case strings.HasPrefix(webhookURL, WebhookURLOffice365Prefix):
default:
u, err := url.Parse(webhookURL)
if err != nil {
return false, fmt.Errorf(
"unable to parse webhook URL %q: %v",
webhookURL,
err,
)
}
userProvidedWebhookURLPrefix := u.Scheme + "://" + u.Host
return false, fmt.Errorf(
"webhook URL does not contain expected prefix; got %q, expected one of %q or %q",
userProvidedWebhookURLPrefix,
WebhookURLOfficecomPrefix,
WebhookURLOffice365Prefix,
)
}
return true, nil
}
// IsValidMessageCard performs validation/checks for known issues with
// MessardCard values.
func IsValidMessageCard(webhookMessage MessageCard) (bool, error) {
if (webhookMessage.Text == "") && (webhookMessage.Summary == "") {
// This scenario results in:
// 400 Bad Request
// Summary or Text is required.
return false, fmt.Errorf("invalid message card: summary or text field is required")
}
return true, nil
}