pushover_agent.rb 5.0 KB

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