pushover_agent.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. module Agents
  2. class PushoverAgent < Agent
  3. cannot_be_scheduled!
  4. cannot_create_events!
  5. API_URL = 'https://api.pushover.net/1/messages.json'
  6. description <<-MD
  7. The PushoverAgent receives and collects events and sends them via push notification to a user/group.
  8. **You need a Pushover API Token:** [https://pushover.net/apps/build](https://pushover.net/apps/build)
  9. **You must provide** a `message` or `text` key that will contain the body of the notification. This can come from an event or be set as a default. Pushover API has a `512` Character Limit including `title`. `message` will be truncated.
  10. * `token`: your application's API token
  11. * `user`: the user or group key (not e-mail address).
  12. * `expected_receive_period_in_days`: is maximum number of days that you would expect to pass between events being received by this agent.
  13. Your event can provide any of the following optional parameters or you can provide defaults:
  14. * `device` - your user's device name to send the message directly to that device, rather than all of the user's devices
  15. * `title` or `subject` - your notification's title
  16. * `url` - a supplementary URL to show with your message - `512` Character Limit
  17. * `url_title` - a title for your supplementary URL, otherwise just the URL is shown - `100` Character Limit
  18. * `priority` - send as `-1` to always send as a quiet notification, `0` is default, `1` to display as high-priority and bypass the user's quiet hours, or `2` for emergency priority: [Please read Pushover Docs on Emergency Priority](https://pushover.net/api#priority)
  19. * `sound` - the name of one of the sounds supported by device clients to override the user's default sound choice. [See PushOver docs for sound options.](https://pushover.net/api#sounds)
  20. * `retry` - Required for emergency priority - Specifies how often (in seconds) the Pushover servers will send the same notification to the user. Minimum value: `30`
  21. * `expire` - Required for emergency priority - Specifies how many seconds your notification will continue to be retried for (every retry seconds). Maximum value: `86400`
  22. Your event can also pass along a timestamp parameter:
  23. * `timestamp` - a [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time) of your message's date and time to display to the user, rather than the time your message is received by the Pushover API.
  24. MD
  25. def default_options
  26. {
  27. 'token' => '',
  28. 'user' => '',
  29. 'message' => 'a default message',
  30. 'device' => '',
  31. 'title' => '',
  32. 'url' => '',
  33. 'url_title' => '',
  34. 'priority' => '0',
  35. 'sound' => 'pushover',
  36. 'retry' => '0',
  37. 'expire' => '0',
  38. 'expected_receive_period_in_days' => '1'
  39. }
  40. end
  41. def validate_options
  42. unless options['token'].present? && options['user'].present? && options['expected_receive_period_in_days'].present?
  43. errors.add(:base, 'token, user, and expected_receive_period_in_days are all required.')
  44. end
  45. end
  46. def receive(incoming_events)
  47. incoming_events.each do |event|
  48. payload_interpolated = interpolated(event)
  49. message = (event.payload['message'].presence || event.payload['text'].presence || payload_interpolated['message']).to_s
  50. if message.present?
  51. post_params = {
  52. 'token' => payload_interpolated['token'],
  53. 'user' => payload_interpolated['user'],
  54. 'message' => message
  55. }
  56. post_params['device'] = event.payload['device'].presence || payload_interpolated['device']
  57. post_params['title'] = event.payload['title'].presence || event.payload['subject'].presence || payload_interpolated['title']
  58. url = (event.payload['url'].presence || payload_interpolated['url'] || '').to_s
  59. url = url.slice 0..512
  60. post_params['url'] = url
  61. url_title = (event.payload['url_title'].presence || payload_interpolated['url_title']).to_s
  62. url_title = url_title.slice 0..100
  63. post_params['url_title'] = url_title
  64. post_params['priority'] = (event.payload['priority'].presence || payload_interpolated['priority']).to_i
  65. if event.payload.has_key? 'timestamp'
  66. post_params['timestamp'] = (event.payload['timestamp']).to_s
  67. end
  68. post_params['sound'] = (event.payload['sound'].presence || payload_interpolated['sound']).to_s
  69. post_params['retry'] = (event.payload['retry'].presence || payload_interpolated['retry']).to_i
  70. post_params['expire'] = (event.payload['expire'].presence || payload_interpolated['expire']).to_i
  71. send_notification(post_params)
  72. end
  73. end
  74. end
  75. def working?
  76. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  77. end
  78. def send_notification(post_params)
  79. response = HTTParty.post(API_URL, :query => post_params)
  80. puts response
  81. end
  82. end
  83. end