1
0

pushover_agent.rb 5.0 KB

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