/
pushbullet-helper.go
99 lines (81 loc) · 2.06 KB
/
pushbullet-helper.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
/*
My pushbullet-go helper class,
for reading access token from a file
and sending pushes easily.
last update: 2015.07.29.
*/
package pbhelper
import (
"fmt"
"github.com/mitsuse/pushbullet-go"
"github.com/mitsuse/pushbullet-go/requests"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const (
AccessTokenFilename = ".pushbullet.token"
)
// read access token from file
func readAccessToken() (token string, err error) {
confDir := filepath.Dir(os.Args[0])
tokenFile := filepath.Join(confDir, AccessTokenFilename)
if _, err := os.Stat(tokenFile); err != nil {
fmt.Printf("Access token file does not exist: %s\n", tokenFile)
return "", err
}
buf, err := ioutil.ReadFile(tokenFile)
if err != nil {
fmt.Println("Failed to read access token file:", err)
return "", err
}
return strings.TrimSpace(string(buf)), nil
}
// send push(note)
func SendNote(title string, message string) bool {
return SendNoteToChannel("", title, message)
}
// send push(note) to channel
func SendNoteToChannel(channelTag string, title string, message string) bool {
token, err := readAccessToken()
if err != nil {
return false
}
pb := pushbullet.New(token)
note := requests.NewNote()
if channelTag != "" {
note.Push.ChannelTag = channelTag // set channel tag when provided
}
note.Title = title
note.Body = message
if _, err := pb.PostPushesNote(note); err != nil {
fmt.Println("Push failed:", err)
return false
}
return true
}
// send push(link)
func SendLink(title string, message string, url string) bool {
return SendLinkToChannel("", title, message, url)
}
// send push(link) to channel
func SendLinkToChannel(channelTag string, title string, message string, url string) bool {
token, err := readAccessToken()
if err != nil {
return false
}
pb := pushbullet.New(token)
link := requests.NewLink()
if channelTag != "" {
link.Push.ChannelTag = channelTag // set channel tag when provided
}
link.Title = title
link.Body = message
link.Url = url
if _, err := pb.PostPushesLink(link); err != nil {
fmt.Println("Push failed:", err)
return false
}
return true
}