11 const apiurl = "https://api.pushover.net/1/messages.json"
12 const MaxMsgLength = 1024
13 const MaxTitleLength = 250
15 // Message represents a message in the Pushover Message API.
24 type response struct {
32 func (e errors) Error() string {
33 return strings.Join(e, ", ")
36 func (m *Message) validate() error {
37 nchar := strings.Count(m.Message, "")
38 if nchar > MaxMsgLength {
39 return fmt.Errorf("%d character message too long, allowed %d characters", nchar, MaxMsgLength)
41 nchar = strings.Count(m.Title, "")
42 if nchar > MaxTitleLength {
43 return fmt.Errorf("%d-character title too long, allowed %d characters", nchar, MaxTitleLength)
48 // Push sends the Message m to Pushover.
49 func Push(m Message) error {
50 if err := m.validate(); err != nil {
54 req.Add("token", m.Token)
55 req.Add("user", m.User)
56 req.Add("title", m.Title)
57 req.Add("message", m.Message)
58 resp, err := http.PostForm(apiurl, req)
62 defer resp.Body.Close()
63 if resp.StatusCode == http.StatusOK {
68 if err := json.NewDecoder(resp.Body).Decode(&presp); err != nil {
69 return fmt.Errorf("decode error response: %v", err)