gpushover.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Package gpushover is a Go wrapper for Pushover's notification API.
  2. //
  3. // Copyright (c) 2021 José Manuel Díez. <j.diezlopez@protonmail.ch>
  4. // Copyright (c) 2021 Gridfinity, LLC. <admin@gridfinity.com>
  5. // Copyright (c) 2014 Damian Gryski. <damian@gryski.com>
  6. // Copyright (c) 2014 Adam Lazzarato.
  7. //
  8. // All Rights reserved.
  9. //
  10. // All use of this code is governed by the MIT license.
  11. // The complete license is available in the LICENSE file.
  12. package gpushover // import "go.gridfinity.dev/gpushover"
  13. import (
  14. "errors"
  15. "fmt"
  16. "io/ioutil"
  17. "net/http"
  18. "net/url"
  19. "time"
  20. json "github.com/json-iterator/go"
  21. gpushoverLegal "go4.org/legal"
  22. )
  23. const endpoint string = "https://api.pushover.net/1/messages.json"
  24. // ErrorPushover is the generic Pushover error
  25. var ErrorPushover = errors.New("ErrorPushover")
  26. // P defines the Pushover configuration
  27. type P struct {
  28. UserKey, AppKey string
  29. Client *http.Client
  30. }
  31. // Response defines the Pushover API response
  32. type Response struct {
  33. Status int
  34. Errors []interface{}
  35. Message string
  36. }
  37. // Notification defines the Pushover API request
  38. type Notification struct {
  39. Message, Title, URL, URLTitle, Sound, Device, Callback string
  40. Timestamp time.Time
  41. Priority, Retry, Expire int
  42. }
  43. func (n Notification) toValues(p P) url.Values {
  44. return url.Values{
  45. "user": {p.UserKey},
  46. "token": {p.AppKey},
  47. "message": {n.Message},
  48. "title": {n.Title},
  49. "url": {n.URL},
  50. "url_title": {n.URLTitle},
  51. "sound": {n.Sound},
  52. "device": {n.Device},
  53. "timestamp": {fmt.Sprintf("%d", n.Timestamp.Unix())},
  54. "priority": {fmt.Sprintf("%d", n.Priority)},
  55. "retry": {fmt.Sprintf("%d", n.Retry)},
  56. "expire": {fmt.Sprintf("%d", n.Expire)},
  57. "callback": {n.Callback},
  58. }
  59. }
  60. // Notify sends the Pushover notification
  61. func (p P) Notify(n Notification) (*Response, error) {
  62. client := p.Client
  63. if client == nil {
  64. client = http.DefaultClient
  65. }
  66. resp, err := client.PostForm(endpoint, n.toValues(p))
  67. if err != nil {
  68. return nil, err
  69. }
  70. if resp.StatusCode == 200 {
  71. return nil, nil
  72. }
  73. defer resp.Body.Close()
  74. body, err := ioutil.ReadAll(resp.Body)
  75. if err != nil {
  76. return nil, err
  77. }
  78. response := new(Response)
  79. err = json.Unmarshal(body, response)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return response, ErrorPushover
  84. }
  85. func init() {
  86. // Register licensing
  87. gpushoverLegal.RegisterLicense(
  88. "\nCopyright (c) 2021 José Manuel Díez. <j.diezlopez@protonmail.ch>\nCopyright (c) 2021 Gridfinity, LLC. <admin@gridfinity.com>\nCopyright (c) 2014 Damian Gryski. <damian@gryski.com>\nCopyright (c) 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",
  89. )
  90. }