pushover_agent.rb 4.8 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 notifications'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` - Requred for emergency priority - Specifies how often (in seconds) the Pushover servers will send the same notification to the user. Minimum value: `30`
  21. * `expire` - Requred 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. message = (event.payload['message'].presence || event.payload['text'].presence || options['message']).to_s
  49. if message.present?
  50. post_params = {
  51. 'token' => options['token'],
  52. 'user' => options['user'],
  53. 'message' => message
  54. }
  55. post_params['device'] = event.payload['device'].presence || options['device']
  56. post_params['title'] = event.payload['title'].presence || event.payload['subject'].presence || options['title']
  57. url = (event.payload['url'].presence || options['url'] || '').to_s
  58. url = url.slice 0..512
  59. post_params['url'] = url
  60. url_title = (event.payload['url_title'].presence || options['url_title']).to_s
  61. url_title = url_title.slice 0..100
  62. post_params['url_title'] = url_title
  63. post_params['priority'] = (event.payload['priority'].presence || options['priority']).to_i
  64. if event.payload.has_key? 'timestamp'
  65. post_params['timestamp'] = (event.payload['timestamp']).to_s
  66. end
  67. post_params['sound'] = (event.payload['sound'].presence || options['sound']).to_s
  68. post_params['retry'] = (event.payload['retry'].presence || options['retry']).to_i
  69. post_params['expire'] = (event.payload['expire'].presence || options['expire']).to_i
  70. send_notification(post_params)
  71. end
  72. end
  73. end
  74. def working?
  75. last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  76. end
  77. def send_notification(post_params)
  78. response = HTTParty.post(API_URL, :query => post_params)
  79. puts response
  80. end
  81. end
  82. end