hipchat_agent.rb 2.0KB

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