gpushover.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Package gpushover is a Go wrapper for Pushover's notification API.
  2. //
  3. // Copyright © 2021 Jeffrey H. Johnson. <trnsz@pobox.com>
  4. // Copyright © 2021 José Manuel Díez. <j.diezlopez@protonmail.ch>
  5. // Copyright © 2021 Gridfinity, LLC. <admin@gridfinity.com>
  6. // Copyright © 2014 Damian Gryski. <damian@gryski.com>
  7. // Copyright © 2014 Adam Lazzarato.
  8. //
  9. // All Rights reserved.
  10. //
  11. // All use of this code is governed by the MIT license.
  12. // The complete license is available in the LICENSE file.
  13. package gpushover
  14. import (
  15. "errors"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "net/url"
  20. "time"
  21. json "github.com/json-iterator/go"
  22. gpushoverLegal "go4.org/legal"
  23. )
  24. const endpoint string = "https://api.pushover.net/1/messages.json"
  25. // ErrorPushover is the generic Pushover error
  26. var ErrorPushover = errors.New("ErrorPushover")
  27. // P defines the Pushover configuration
  28. type P struct {
  29. UserKey, AppKey string
  30. Client *http.Client
  31. }
  32. // Response defines the Pushover API response
  33. type Response struct {
  34. Status int
  35. Errors []interface{}
  36. Message string
  37. }
  38. // Notification defines the Pushover API request
  39. type Notification struct {
  40. Message, Title, URL, URLTitle, Sound, Device, Callback string
  41. Timestamp time.Time
  42. Priority, Retry, Expire int
  43. }
  44. func (n Notification) toValues(p P) url.Values {
  45. return url.Values{
  46. "user": {p.UserKey},
  47. "token": {p.AppKey},
  48. "message": {n.Message},
  49. "title": {n.Title},
  50. "url": {n.URL},
  51. "url_title": {n.URLTitle},
  52. "sound": {n.Sound},
  53. "device": {n.Device},
  54. "timestamp": {fmt.Sprintf("%d", n.Timestamp.Unix())},
  55. "priority": {fmt.Sprintf("%d", n.Priority)},
  56. "retry": {fmt.Sprintf("%d", n.Retry)},
  57. "expire": {fmt.Sprintf("%d", n.Expire)},
  58. "callback": {n.Callback},
  59. }
  60. }
  61. // Notify sends the Pushover notification
  62. func (p P) Notify(n Notification) (*Response, error) {
  63. client := p.Client
  64. if client == nil {
  65. client = http.DefaultClient
  66. }
  67. resp, err := client.PostForm(endpoint, n.toValues(p))
  68. if err != nil {
  69. return nil, err
  70. }
  71. if resp.StatusCode == 200 {
  72. return nil, nil
  73. }
  74. defer resp.Body.Close()
  75. body, err := ioutil.ReadAll(resp.Body)
  76. if err != nil {
  77. return nil, err
  78. }
  79. response := new(Response)
  80. err = json.Unmarshal(body, response)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return response, ErrorPushover
  85. }
  86. func init() {
  87. // Register licensing
  88. gpushoverLegal.RegisterLicense(
  89. "\nCopyright © 2021 Jeffrey H. Johnson <trnsz@pobox.com>.\nCopyright © 2021 José Manuel Díez. <j.diezlopez@protonmail.ch>\nCopyright © 2021 Gridfinity, LLC. <admin@gridfinity.com>\nCopyright © 2014 Damian Gryski. <damian@gryski.com>\nCopyright © 2014 Adam Lazzarato.\n\nPermission is hereby granted, free of charge, to any person obtaining a\ncopy of this software and associated documentation files (the \"Software\"),\nto deal in the Software without restriction, including, without limitation,\nthe rights to use, copy, modify, merge, publish, distribute, sub-license,\nand/or sell copies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\nOR IMPLIED, INCLUDING, BUT NOT LIMITED, TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\nDAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR\nOTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH, THE SOFTWARE, OR\nTHE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
  90. )
  91. }