-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (59 loc) · 1.74 KB
/
main.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
/*
* Copyright (c) Elliot Peele <[email protected]>
*
* This program is distributed under the terms of the MIT License as found
* in a file called LICENSE. If it is not present, the license
* is always available at http://www.opensource.org/licenses/mit-license.php.
*
* This program is distributed in the hope that it will be useful, but
* without any warrenty; without even the implied warranty of merchantability
* or fitness for a particular purpose. See the MIT License for full details.
*/
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"net/url"
)
const API_MESSAGE = "https://api.pushover.net/1/messages.json"
type ApiResponse struct {
Status int `json:"status"`
Request string `json:"request"`
User string `json:"user,omitempty"`
Errors []string `json:"errors,omitempty"`
}
var (
token = flag.String("token", "", "Pushover API Token")
user = flag.String("user", "", "Pushover User Token")
title = flag.String("title", "", "Message Title")
message = flag.String("message", "", "Messge Content")
)
func init() {
flag.Parse()
}
func main() {
if *token == "" || *user == "" || *title == "" || *message == "" {
log.Fatal("Must set token, user, title, and message.")
}
values := url.Values{}
values.Set("token", *token)
values.Set("user", *user)
values.Set("title", *title)
values.Set("message", *message)
resp, err := http.PostForm(API_MESSAGE, values)
if err != nil {
log.Fatalf("Error making request: %s", err)
}
var out ApiResponse
dec := json.NewDecoder(resp.Body)
if err = dec.Decode(&out); err != nil {
log.Fatalf("Error parsing response: %s", err)
}
if out.Status == 1 {
log.Print("sent successfully")
} else {
log.Fatalf("error sending message: %s", out.Errors[0])
}
}