hipchat_agent.rb 1.9 KB

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