pushover_agent.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * `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. The following options are all [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) templates whose evaluated values will be posted to the Pushover API. Only the `message` parameter is required, and if it is blank API call is omitted.
  14. Pushover API has a `512` Character Limit including `title`. `message` will be truncated.
  15. * `message` - your message (required)
  16. * `device` - your user's device name to send the message directly to that device, rather than all of the user's devices
  17. * `title` or `subject` - your notification's title
  18. * `url` - a supplementary URL to show with your message - `512` Character Limit
  19. * `url_title` - a title for your supplementary URL, otherwise just the URL is shown - `100` Character Limit
  20. * `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.
  21. * `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)
  22. * `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)
  23. * `retry` - Required for emergency priority - Specifies how often (in seconds) the Pushover servers will send the same notification to the user. Minimum value: `30`
  24. * `expire` - Required for emergency priority - Specifies how many seconds your notification will continue to be retried for (every retry seconds). Maximum value: `86400`
  25. MD
  26. def default_options
  27. {
  28. 'token' => '',
  29. 'user' => '',
  30. 'message' => '{{ message }}',
  31. 'device' => '{{ device }}',
  32. 'title' => '{{ title }}',
  33. 'url' => '{{ url }}',
  34. 'url_title' => '{{ url_title }}',
  35. 'priority' => '{{ priority }}',
  36. 'timestamp' => '{{ timestamp }}',
  37. 'sound' => '{{ sound }}',
  38. 'retry' => '{{ retry }}',
  39. 'expire' => '{{ expire }}',
  40. 'expected_receive_period_in_days' => '1'
  41. }
  42. end
  43. def validate_options
  44. unless options['token'].present? && options['user'].present? && options['expected_receive_period_in_days'].present?
  45. errors.add(:base, 'token, user, and expected_receive_period_in_days are all required.')
  46. end
  47. end
  48. def receive(incoming_events)
  49. incoming_events.each do |event|
  50. interpolate_with(event) do
  51. post_params = {}
  52. # required parameters
  53. %w[
  54. token
  55. user
  56. message
  57. ].all? { |key|
  58. if value = String.try_convert(interpolated[key].presence)
  59. post_params[key] = value
  60. end
  61. } or next
  62. # optional parameters
  63. %w[
  64. device
  65. title
  66. url
  67. url_title
  68. priority
  69. timestamp
  70. sound
  71. retry
  72. expire
  73. ].each do |key|
  74. if value = String.try_convert(interpolated[key].presence)
  75. case key
  76. when 'url'
  77. value.slice!(512..-1)
  78. when 'url_title'
  79. value.slice!(100..-1)
  80. end
  81. post_params[key] = value
  82. end
  83. end
  84. send_notification(post_params)
  85. end
  86. end
  87. end
  88. def working?
  89. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  90. end
  91. def send_notification(post_params)
  92. response = HTTParty.post(API_URL, query: post_params)
  93. puts response
  94. log "Sent notification with message \"#{post_params['message']}\" to \"#{post_params['user']}\""
  95. end
  96. end
  97. end