Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit bd23b50

Browse files
author
Hongze Xia
committed
add pushover client
1 parent b9ab10e commit bd23b50

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

pushover/client.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package pushover
2+
3+
import (
4+
"io/ioutil"
5+
"log"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
type (
11+
// Client is the Pushover client
12+
Client struct {
13+
AppToken string
14+
User string
15+
}
16+
17+
// Message is the request body for Pushover
18+
// Not all fields have been implemented
19+
// Please refer to https://pushover.net/api
20+
Message struct {
21+
Msg string
22+
Device string
23+
Title string
24+
URL string
25+
Priority int
26+
}
27+
)
28+
29+
var msgAPI = "https://api.pushover.net/1/messages.json"
30+
31+
// Send sends a message to the queue
32+
func (c *Client) Send(msg *Message) {
33+
if msg.Msg == "" {
34+
log.Printf("can't send an empty message")
35+
return
36+
}
37+
form := url.Values{
38+
"token": []string{c.AppToken},
39+
"user": []string{c.User},
40+
"message": []string{msg.Msg},
41+
}
42+
if msg.Device != "" {
43+
form.Add("device", msg.Device)
44+
}
45+
if msg.Title != "" {
46+
form.Add("title", msg.Title)
47+
}
48+
if msg.URL != "" {
49+
form.Add("url", msg.URL)
50+
}
51+
if msg.Priority != 0 {
52+
form.Add("priority", string(msg.Priority))
53+
}
54+
resp, err := http.PostForm(msgAPI, form)
55+
if err != nil {
56+
log.Printf("failed sending message %v", err)
57+
return
58+
}
59+
defer resp.Body.Close()
60+
if resp.StatusCode == http.StatusOK {
61+
body, err := ioutil.ReadAll(resp.Body)
62+
if err != nil {
63+
log.Printf("failed reading resp body %v", err)
64+
return
65+
}
66+
log.Printf("resp: %s", body)
67+
}
68+
}

0 commit comments

Comments
 (0)