hipchat_agent.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. module Agents
  2. class HipchatAgent < Agent
  3. cannot_be_scheduled!
  4. cannot_create_events!
  5. gem_dependency_check { defined?(HipChat) }
  6. description <<-MD
  7. #{'## Include `hipchat` in your Gemfile to use this Agent!' if dependencies_missing?}
  8. The HipchatAgent sends messages to a Hipchat Room
  9. To authenticate you need to set the `auth_token`, you can get one at your Hipchat Group Admin page which you can find here:
  10. `https://`yoursubdomain`.hipchat.com/admin/api`
  11. Change the `room_name` to the name of the room you want to send notifications to.
  12. You can provide a `username` and a `message`. When sending a HTML formatted message change `format` to "html".
  13. If you want your message to notify the room members change `notify` to "true".
  14. Modify the background color of your message via the `color` attribute (one of "yellow", "red", "green", "purple", "gray", or "random")
  15. Have a look at the [Wiki](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) to learn more about liquid templating.
  16. MD
  17. def default_options
  18. {
  19. 'auth_token' => '',
  20. 'room_name' => '',
  21. 'username' => "Huginn",
  22. 'message' => "Hello from Huginn!",
  23. 'notify' => false,
  24. 'color' => 'yellow',
  25. }
  26. end
  27. def validate_options
  28. errors.add(:base, "you need to specify a hipchat auth_token or provide a credential named hipchat_auth_token") unless options['auth_token'].present? || credential('hipchat_auth_token').present?
  29. errors.add(:base, "you need to specify a room_name or a room_name_path") if options['room_name'].blank? && options['room_name_path'].blank?
  30. end
  31. def working?
  32. (last_receive_at.present? && last_error_log_at.nil?) || (last_receive_at.present? && last_error_log_at.present? && last_receive_at > last_error_log_at)
  33. end
  34. def receive(incoming_events)
  35. incoming_events.each do |event|
  36. mo = interpolated(event)
  37. client[mo[:room_name]].send(mo[:username][0..14], mo[:message], :notify => boolify(mo[:notify]), :color => mo[:color])
  38. end
  39. end
  40. def client
  41. @client ||= HipChat::Client.new(interpolated[:auth_token] || credential('hipchat_auth_token'))
  42. end
  43. end
  44. end