-
Notifications
You must be signed in to change notification settings - Fork 3
/
message.go
145 lines (124 loc) · 3.57 KB
/
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
135
136
137
138
139
140
141
142
143
144
145
package gotfy
import (
"encoding/json"
"fmt"
"net/url"
"time"
)
// Message is a struct you can create from TopicPublisher that
// will publish a message to the specified topic. This method does not allow
// for attaching files to the notification, but it can post a link to an attachment
type Message struct {
Topic string `json:"topic"` // Target topic name
Message string `json:"message,omitempty"` // Message body; set to triggered if empty or not passed
Title string `json:"title,omitempty"` // Message title
Tags []string `json:"tags,omitempty"` // List of tags that may or not map to emojis
Priority Priority `json:"priority,omitempty"` // Message priority with 1=min, 3=default and 5=max
Actions []ActionButton `json:"actions,omitempty"` // Custom user action buttons for notifications
ClickURL *url.URL `json:"click,omitempty"` // Website opened when notification is clicked
IconURL *url.URL `json:"icon,omitempty"` // URL to use as notification icon
Delay time.Duration `json:"delay,omitempty"` // Duration to delay delivery
Email string `json:"email,omitempty"` // E-mail address for e-mail notifications
Call string `json:"call,omitempty"` // Phone number to use for voice call
AttachURLFilename string `json:"filename,omitempty"` // File name of the attachment
AttachURL *url.URL `json:"attachurl,omitempty"` // URL of an attachment
}
func (m *Message) MarshalJSON() ([]byte, error) {
buf, err := json.Marshal(m.Topic)
if err != nil {
return nil, err
}
buf = []byte(fmt.Sprintf(`{"topic":%s`, buf))
if x := m.Message; x != "" {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"message":%s`, mm)...)
}
if x := m.Title; x != "" {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"title":%s`, mm)...)
}
if x := m.Tags; len(x) > 0 {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"tags":%s`, mm)...)
}
if x := m.Priority; x > 0 {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"priority":%s`, mm)...)
}
if x := m.Actions; len(x) > 0 {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"actions":%s`, mm)...)
}
type urls struct {
name string
url *url.URL
}
for _, v := range []urls{
{"click", m.ClickURL},
{"attachurl", m.AttachURL},
{"icon", m.IconURL},
} {
mm, err := urlString(v.url)
if err != nil {
return nil, err
}
if mm == nil {
continue
}
buf = append(buf, fmt.Sprintf(`,"%s":%s`, v.name, mm)...)
}
if x := m.Delay; x > 0 {
mm, err := json.Marshal(x.String())
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"delay":%s`, mm)...)
}
if x := m.Email; x != `` {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"email":%s`, mm)...)
}
if x := m.Call; x != `` {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"call":%s`, mm)...)
}
if x := m.AttachURLFilename; x != `` {
mm, err := json.Marshal(x)
if err != nil {
return nil, err
}
buf = append(buf, fmt.Sprintf(`,"filename":%s`, mm)...)
}
return append(buf, '}'), nil
}
func urlString(u *url.URL) ([]byte, error) {
if u == nil {
return nil, nil
}
s := u.String()
if s == "" {
return nil, nil
}
return json.Marshal(s)
}