-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-message.go
134 lines (113 loc) · 3.37 KB
/
cloud-message.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
package automate
import (
"birnenlabs.com/go/lib/conf"
"bytes"
"fmt"
"github.com/golang/glog"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
const (
payloadFormat = `
{
"secret": "%v",
"to": "%v",
"device": null,
"priority": "normal",
"payload": "%v"
}`
url = "https://llamalab.com/automate/cloud/message"
allowedCharacters = `[^a-zA-Z0-9 ~!@#$%^&*()_+=\[\]{}|\\-]+`
pUrgent = 1
pNormal = 0
)
type CloudMessage struct {
// Secret and DefaultTo can be stored in config. Secret is required.
// https://llamalab.com/automate/cloud/ - generate secret.
Secret string
DefaultTo string
// appName can be set to use formatted version
appName string
}
func Create() (*CloudMessage, error) {
var result CloudMessage
err := conf.LoadConfigFromJson("cloud-message", &result)
if err != nil {
return nil, err
}
if len(result.Secret) == 0 {
return nil, fmt.Errorf("Secret has to be set.")
}
return &result, nil
}
// When appName is set, the formatted payload will be used:
// $from [$hostname]|$priority|$message
func (c *CloudMessage) UseFormattedPayload(appName string) {
if c != nil {
c.appName = appName
}
}
// All the methods of CloudMessage should support nil pointer!
// Sends message to the default recipient.
func (c *CloudMessage) Send(msg string) {
c.send(pNormal, "", msg)
}
// Sends message to the default recipient with priority set to urgent.
// Note: Formatted payload with priority is supported only if UseFormattedPayload was called.
func (c *CloudMessage) SendUrgent(msg string) {
c.send(pUrgent, "", msg)
}
// Sends message to the custom recipient.
func (c *CloudMessage) SendTo(to string, msg string) {
c.send(pNormal, to, msg)
}
// Sends message to the custom recipient with priority set to urgent.
// Note: Formatted payload with priority is supported only if UseFormattedPayload was called.
func (c *CloudMessage) SendUrgentTo(to string, msg string) {
c.send(pUrgent, to, msg)
}
func (c *CloudMessage) send(priority int, to string, msg string) {
if c == nil {
glog.Errorf("Cloud message not initialized! NOT SENT: %q.", msg)
return
}
// Use DefaultTo if not set.
if len(to) == 0 {
to = c.DefaultTo
}
// Use formatted payload when appName is set.
if len(c.appName) > 0 {
msg = createFormattedPayload(c.appName, msg, priority)
}
// Replace newlines and remove non printable characters
payload := strings.Replace(msg, "\n", `\n`, -1)
reg, err := regexp.Compile(allowedCharacters)
if err != nil {
glog.Errorf("Could not send cloud message %q: %v.", msg, err)
return
}
payload = reg.ReplaceAllString(payload, " ")
var jsonStr = []byte(fmt.Sprintf(payloadFormat, c.Secret, to, payload))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
glog.Errorf("Could not send cloud message %q: %v.", msg, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := ioutil.ReadAll(resp.Body)
glog.Errorf("Could not send cloud message %q, response:\n%v", msg, string(body))
}
}
func createFormattedPayload(from string, msg string, priority int) string {
hostname, _ := os.Hostname()
from = strings.Replace(from, "|", "", -1)
msg = strings.Replace(msg, "|", "", -1)
return fmt.Sprintf("%v [%v]|%v|%v", from, hostname, priority, msg)
}