pushover_agent.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. module Agents
  2. class PushoverAgent < Agent
  3. can_dry_run!
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. no_bulk_receive!
  7. API_URL = 'https://api.pushover.net/1/messages.json'
  8. description <<-MD
  9. The Pushover Agent receives and collects events and sends them via push notification to a user/group.
  10. **You need a Pushover API Token:** [https://pushover.net/apps/build](https://pushover.net/apps/build)
  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. The following options are all [Liquid](https://github.com/huginn/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.
  15. Pushover API has a `512` Character Limit including `title`. `message` will be truncated.
  16. * `message` - your message (required)
  17. * `device` - your user's device name to send the message directly to that device, rather than all of the user's devices
  18. * `title` or `subject` - your notification's title
  19. * `url` - a supplementary URL to show with your message - `512` Character Limit
  20. * `url_title` - a title for your supplementary URL, otherwise just the URL is shown - `100` Character Limit
  21. * `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.
  22. * `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)
  23. * `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)
  24. * `retry` - Required for emergency priority - Specifies how often (in seconds) the Pushover servers will send the same notification to the user. Minimum value: `30`
  25. * `expire` - Required for emergency priority - Specifies how many seconds your notification will continue to be retried for (every retry seconds). Maximum value: `86400`
  26. * `html` - set to `true` to have Pushover's apps display the `message` content as HTML
  27. MD
  28. def default_options
  29. {
  30. 'token' => '',
  31. 'user' => '',
  32. 'message' => '{{ message }}',
  33. 'device' => '{{ device }}',
  34. 'title' => '{{ title }}',
  35. 'url' => '{{ url }}',
  36. 'url_title' => '{{ url_title }}',
  37. 'priority' => '{{ priority }}',
  38. 'timestamp' => '{{ timestamp }}',
  39. 'sound' => '{{ sound }}',
  40. 'retry' => '{{ retry }}',
  41. 'expire' => '{{ expire }}',
  42. 'html' => 'false',
  43. 'expected_receive_period_in_days' => '1'
  44. }
  45. end
  46. def validate_options
  47. unless options['token'].present? && options['user'].present? && options['expected_receive_period_in_days'].present?
  48. errors.add(:base, 'token, user, and expected_receive_period_in_days are all required.')
  49. end
  50. end
  51. def receive(incoming_events)
  52. incoming_events.each do |event|
  53. interpolate_with(event) do
  54. post_params = {}
  55. # required parameters
  56. %w[
  57. token
  58. user
  59. message
  60. ].all? { |key|
  61. if value = String.try_convert(interpolated[key].presence)
  62. post_params[key] = value
  63. end
  64. } or next
  65. # optional parameters
  66. %w[
  67. device
  68. title
  69. url
  70. url_title
  71. priority
  72. timestamp
  73. sound
  74. retry
  75. expire
  76. ].each do |key|
  77. if value = String.try_convert(interpolated[key].presence)
  78. case key
  79. when 'url'
  80. value.slice!(512..-1)
  81. when 'url_title'
  82. value.slice!(100..-1)
  83. end
  84. post_params[key] = value
  85. end
  86. end
  87. # html is special because String.try_convert(true) gives nil (not even "nil", just nil)
  88. if value = interpolated['html'].presence
  89. post_params['html'] =
  90. case value.to_s
  91. when 'true', '1'
  92. '1'
  93. else
  94. '0'
  95. end
  96. end
  97. send_notification(post_params)
  98. end
  99. end
  100. end
  101. def working?
  102. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  103. end
  104. def send_notification(post_params)
  105. response = HTTParty.post(API_URL, query: post_params)
  106. puts response
  107. log "Sent the following notification: \"#{post_params.except('token').inspect}\""
  108. end
  109. end
  110. end