forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
communication.go
106 lines (91 loc) · 2.03 KB
/
communication.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
package core
import (
"bytes"
"fmt"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"net/http"
"time"
)
type Communication types.Communication
func LoadDefaultCommunications() {
emailer := SelectCommunication(1)
if emailer.Enabled {
//LoadMailer(emailer)
//go EmailerQueue()
}
}
func LoadComms() {
for _, c := range CoreApp.Communications {
if c.Enabled {
}
}
}
func Run(c *Communication) {
//sample := &Email{
// To: "info@socialeck.com",
// Subject: "Test Email from Statup",
//}
//AddEmail(sample)
}
func SelectAllCommunications() ([]*Communication, error) {
var c []*Communication
col := DbSession.Collection("communication").Find()
err := col.All(&c)
CoreApp.Communications = c
return c, err
}
func Create(c *Communication) (int64, error) {
c.CreatedAt = time.Now()
uuid, err := DbSession.Collection("communication").Insert(c)
if err != nil {
utils.Log(3, err)
}
if uuid == nil {
utils.Log(2, err)
return 0, err
}
c.Id = uuid.(int64)
if CoreApp != nil {
CoreApp.Communications = append(CoreApp.Communications, c)
}
return uuid.(int64), err
}
func Disable(c *Communication) {
c.Enabled = false
Update(c)
}
func Enable(c *Communication) {
c.Enabled = true
Update(c)
}
func Update(c *Communication) *Communication {
col := DbSession.Collection("communication").Find("id", c.Id)
col.Update(c)
return c
}
func SelectCommunication(id int64) *Communication {
for _, c := range CoreApp.Communications {
if c.Id == id {
return c
}
}
return nil
}
func SendSlackMessage(msg string) error {
fullMessage := fmt.Sprintf("{\"text\":\"%v\"}", msg)
utils.Log(1, fmt.Sprintf("Sending JSON to Slack Webhook: %v", fullMessage))
slack := SelectCommunication(2)
if slack == nil {
utils.Log(3, fmt.Sprintf("Slack communication database entry was not found."))
return nil
}
client := http.Client{
Timeout: 15 * time.Second,
}
_, err := client.Post(slack.Host, "application/json", bytes.NewBuffer([]byte(fullMessage)))
if err != nil {
utils.Log(3, err)
}
return err
}