-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-send.go
208 lines (181 loc) · 5.33 KB
/
http-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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2020 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Serves Health Checks
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/blues/note-go/note"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
// AlertMessage is the format of a message coming in from the route
type AlertMessage struct {
SMS string `json:"sms"`
Email string `json:"email"`
Text string `json:"text"`
Body string `json:"body"`
Minutes uint32 `json:"minutes"`
Event note.Event `json:"event"`
}
// We retain an in-memory array of future messages to suppress
type suppressMessage struct {
sms string
email string
expires time.Time
text string
}
// In-memory array, plus integrity protection
var smLock sync.RWMutex
var suppressMessages []suppressMessage
// Ping handler
func inboundWebSendHandler(httpRsp http.ResponseWriter, httpReq *http.Request) {
// Get the body if supplied
alertJSON, err := ioutil.ReadAll(httpReq.Body)
if err != nil {
alertJSON = []byte("{}")
}
_ = alertJSON
// Trace
fmt.Printf("%s\n", alertJSON)
// Debug
var alert AlertMessage
err = note.JSONUnmarshal(alertJSON, &alert)
if err != nil {
fmt.Printf("%s\n", err)
httpRsp.WriteHeader(http.StatusBadRequest)
httpRsp.Write([]byte(fmt.Sprintf("%s", err)))
return
}
// Send twilio SMS messages
// https://www.twilio.com/blog/2014/06/sending-sms-from-your-go-app.html
smsRecipients := strings.Split(alert.SMS, ",")
for _, toSMS := range smsRecipients {
// Skip blank
toSMS = strings.TrimSpace(toSMS)
if toSMS == "" {
continue
}
if !strings.HasPrefix(toSMS, "+") {
toSMS = "+" + toSMS
}
// Ensure that we don't send duplicates
if alert.Minutes > 0 {
suppress, expiresSecs := shouldBeSuppressed(toSMS, "", alert.Text, alert.Minutes)
if suppress {
fmt.Printf("SMS to %s expires in %d mins\n", toSMS, (expiresSecs/60)+1)
continue
}
}
// Send the SMS
accountSid := Config.TwilioSID
authToken := Config.TwilioSAK
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json"
v := url.Values{}
v.Set("To", toSMS)
v.Set("From", Config.TwilioSMS)
if alert.Text != "" {
v.Set("Body", alert.Text)
} else {
v.Set("Body", alert.Body)
}
rb := *strings.NewReader(v.Encode())
client := &http.Client{}
req, _ := http.NewRequest("POST", urlStr, &rb)
req.SetBasicAuth(accountSid, authToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, _ := client.Do(req)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("send: %s (%s): %s\n", toSMS, resp.Status, bodyBytes)
} else {
fmt.Printf("send: %s: %s\n", toSMS, resp.Status)
}
}
// Send twilio/sendgrid Email messages
// https://docs.sendgrid.com/for-developers/sending-email/v3-go-code-example
emailRecipients := strings.Split(alert.Email, ",")
for _, toEmail := range emailRecipients {
// Skip blank
toEmail = strings.TrimSpace(toEmail)
if toEmail == "" {
continue
}
// Ensure that we don't send duplicates
if alert.Minutes > 0 {
suppress, expiresSecs := shouldBeSuppressed("", toEmail, alert.Text, alert.Minutes)
if suppress {
fmt.Printf("message to %s expires in %d mins\n", toEmail, (expiresSecs/60)+1)
continue
}
}
// Send the email
from := mail.NewEmail(Config.TwilioFrom, Config.TwilioEmail)
fmt.Printf("%s %s %v\n", Config.TwilioFrom, Config.TwilioEmail, from)
subject := alert.Text
to := mail.NewEmail("", toEmail)
if subject == "" {
subject = "(no alert text specified)"
}
plainTextContent := alert.Body
if plainTextContent == "" {
plainTextContent = subject
}
htmlContent := ""
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
client := sendgrid.NewSendClient(Config.TwilioSendgridAPIKey)
response, err := client.Send(message)
if err != nil {
fmt.Printf("send email to %s: %s\n", toEmail, err)
} else {
fmt.Printf("send email to %s: %d %s %s\n", toEmail, response.StatusCode, response.Body, response.Headers)
}
}
}
// See if a message should be suppressed
func shouldBeSuppressed(toSMS string, toEmail string, text string, minutes uint32) (suppress bool, expiresSecs int64) {
// Rebuild the list of messages to be suppressed
smLock.Lock()
newSM := []suppressMessage{}
// See if we can find an unexpired entry, and garbage collect
now := time.Now()
expires := time.Now()
for _, sm := range suppressMessages {
if now.Before(sm.expires) {
newSM = append(newSM, sm)
if sm.text == text {
if sm.expires.After(expires) {
expires = sm.expires
}
if sm.sms != "" && sm.sms == toSMS {
suppress = true
}
if sm.email != "" && sm.email == toEmail {
suppress = true
}
}
}
}
// If wwe shouldn't suppress, suppress future texts
if !suppress {
var sm suppressMessage
sm.sms = toSMS
sm.email = toEmail
sm.expires = now.Add(time.Minute * time.Duration(minutes))
sm.text = text
newSM = append(newSM, sm)
} else {
expiresSecs = expires.Unix() - now.Unix()
}
// Update the list and exit
suppressMessages = newSM
smLock.Unlock()
return
}