pushover_agent.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * `ttl` - set to a Time to Live in seconds
  27. * `html` - set to `true` to have Pushover's apps display the `message` content as HTML
  28. * `monospace` - set to `true` to have Pushover's apps display the `message` content with a monospace font
  29. MD
  30. def default_options
  31. {
  32. 'token' => '',
  33. 'user' => '',
  34. 'message' => '{{ message }}',
  35. 'device' => '{{ device }}',
  36. 'title' => '{{ title }}',
  37. 'url' => '{{ url }}',
  38. 'url_title' => '{{ url_title }}',
  39. 'priority' => '{{ priority }}',
  40. 'timestamp' => '{{ timestamp }}',
  41. 'sound' => '{{ sound }}',
  42. 'retry' => '{{ retry }}',
  43. 'expire' => '{{ expire }}',
  44. 'ttl' => '{{ ttl }}',
  45. 'html' => 'false',
  46. 'monospace' => 'false',
  47. 'expected_receive_period_in_days' => '1'
  48. }
  49. end
  50. def validate_options
  51. unless options['token'].present? && options['user'].present? && options['expected_receive_period_in_days'].present?
  52. errors.add(:base, 'token, user, and expected_receive_period_in_days are all required.')
  53. end
  54. end
  55. def receive(incoming_events)
  56. incoming_events.each do |event|
  57. interpolate_with(event) do
  58. post_params = {}
  59. # required parameters
  60. %w[
  61. token
  62. user
  63. message
  64. ].all? { |key|
  65. if value = String.try_convert(interpolated[key].presence)
  66. post_params[key] = value
  67. end
  68. } or next
  69. # optional parameters
  70. %w[
  71. device
  72. title
  73. url
  74. url_title
  75. priority
  76. timestamp
  77. sound
  78. retry
  79. expire
  80. ttl
  81. ].each do |key|
  82. value = String.try_convert(interpolated[key].presence) or next
  83. case key
  84. when 'url'
  85. value.slice!(512..-1)
  86. when 'url_title'
  87. value.slice!(100..-1)
  88. end
  89. post_params[key] = value
  90. end
  91. # boolean parameters
  92. %w[
  93. html
  94. monospace
  95. ].each do |key|
  96. if value = interpolated[key].presence
  97. post_params[key] =
  98. case value.to_s
  99. when 'true', '1'
  100. '1'
  101. else
  102. '0'
  103. end
  104. end
  105. end
  106. send_notification(post_params)
  107. end
  108. end
  109. end
  110. def working?
  111. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  112. end
  113. def send_notification(post_params)
  114. response = HTTParty.post(API_URL, query: post_params)
  115. puts response
  116. log "Sent the following notification: \"#{post_params.except('token').inspect}\""
  117. end
  118. end
  119. end