-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.go
102 lines (86 loc) · 2.15 KB
/
channels.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/smtp"
"strings"
"time"
)
// EmailConfig contains email data
type EmailConfig struct {
username string
password string
server string
port string
recipient string
subject string
message string
}
func PreparePayload(message string, msgField string, additionalData string) ([]byte, error) {
jayson := map[string]interface{}{
msgField: message,
}
// Required for valid json
additionalData = strings.ReplaceAll(additionalData, "'", "\"")
if additionalData != "" {
data := []byte(`` + additionalData + ``)
var f interface{}
if err := json.Unmarshal(data, &f); err != nil {
return []byte{}, err
}
m := f.(map[string]interface{})
for k, v := range m {
jayson[k] = v
}
}
js, err := json.Marshal(jayson)
if err != nil {
return []byte{}, err
}
return js, nil
}
// Email Send messages via email
func SendEmail(email EmailConfig) error {
// Set up authentication information.
auth := smtp.PlainAuth("", email.username, email.password, email.server)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
to := []string{email.recipient}
msg := []byte("To: " + email.recipient + "\r\n" +
"Subject: " + email.subject + "\r\n" +
"\r\n" +
email.message + "\r\n")
err := smtp.SendMail(email.server+":"+email.port, auth, email.username, to, msg)
if err != nil {
return err
}
return nil
}
// SendRequest Send the request to the webhook
func SendRequest(endpoint string, data []byte) error {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
}
var resp *http.Response
var err error
client := &http.Client{Transport: tr}
resp, err = client.Post(endpoint, "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, respErr := ioutil.ReadAll(resp.Body)
if respErr != nil {
return respErr
}
errorMessage := fmt.Sprintf("Response HTTP Status code: %d\nResponse HTTP Body: %s", resp.StatusCode, string(body))
return errors.New(errorMessage)
}
return nil
}