/
pushbullet_json.go
72 lines (61 loc) · 1.83 KB
/
pushbullet_json.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
package pushbullet
import (
"regexp"
)
// PushRequest ...
type PushRequest struct {
Type string `json:"type"`
Title string `json:"title"`
Body string `json:"body"`
Email string `json:"email"`
ChannelTag string `json:"channel_tag"`
DeviceIden string `json:"device_iden"`
}
type PushResponse struct {
Active bool `json:"active"`
Body string `json:"body"`
Created float64 `json:"created"`
Direction string `json:"direction"`
Dismissed bool `json:"dismissed"`
Iden string `json:"iden"`
Modified float64 `json:"modified"`
ReceiverEmail string `json:"receiver_email"`
ReceiverEmailNormalized string `json:"receiver_email_normalized"`
ReceiverIden string `json:"receiver_iden"`
SenderEmail string `json:"sender_email"`
SenderEmailNormalized string `json:"sender_email_normalized"`
SenderIden string `json:"sender_iden"`
SenderName string `json:"sender_name"`
Title string `json:"title"`
Type string `json:"type"`
}
type ErrorResponse struct {
ErrorData struct {
Cat string `json:"cat"`
Message string `json:"message"`
Type string `json:"type"`
} `json:"error"`
}
var emailPattern = regexp.MustCompile(`.*@.*\..*`)
func (err *ErrorResponse) Error() string {
return err.ErrorData.Message
}
func (p *PushRequest) SetTarget(target string) {
if emailPattern.MatchString(target) {
p.Email = target
return
}
if len(target) > 0 && string(target[0]) == "#" {
p.ChannelTag = target[1:]
return
}
p.DeviceIden = target
}
// NewNotePush creates a new push request
func NewNotePush(message, title string) *PushRequest {
return &PushRequest{
Type: "note",
Title: title,
Body: message,
}
}