hipchat_agent.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. module Agents
  2. class HipchatAgent < Agent
  3. include FormConfigurable
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. gem_dependency_check { defined?(HipChat) }
  7. description <<-MD
  8. #{'## Include `hipchat` in your Gemfile to use this Agent!' if dependencies_missing?}
  9. The HipchatAgent sends messages to a Hipchat Room
  10. To authenticate you need to set the `auth_token`, you can get one at your Hipchat Group Admin page which you can find here:
  11. `https://`yoursubdomain`.hipchat.com/admin/api`
  12. Change the `room_name` to the name of the room you want to send notifications to.
  13. You can provide a `username` and a `message`. If you want to use mentions change `format` to "text" ([details](https://www.hipchat.com/docs/api/method/rooms/message)).
  14. If you want your message to notify the room members change `notify` to "True".
  15. Modify the background color of your message via the `color` attribute (one of "yellow", "red", "green", "purple", "gray", or "random")
  16. Have a look at the [Wiki](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) to learn more about liquid templating.
  17. MD
  18. def default_options
  19. {
  20. 'auth_token' => '',
  21. 'room_name' => '',
  22. 'username' => "Huginn",
  23. 'message' => "Hello from Huginn!",
  24. 'notify' => false,
  25. 'color' => 'yellow',
  26. 'format' => 'html'
  27. }
  28. end
  29. form_configurable :auth_token, roles: :validatable
  30. form_configurable :room_name, roles: :completable
  31. form_configurable :username
  32. form_configurable :message, type: :text
  33. form_configurable :notify, type: :boolean
  34. form_configurable :color, type: :array, values: ['yellow', 'red', 'green', 'purple', 'gray', 'random']
  35. form_configurable :format, type: :array, values: ['html', 'text']
  36. def validate_auth_token
  37. client.rooms
  38. true
  39. rescue HipChat::UnknownResponseCode
  40. return false
  41. end
  42. def complete_room_name
  43. client.rooms.collect { |room| {text: room.name, id: room.name} }
  44. end
  45. def validate_options
  46. 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?
  47. 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?
  48. end
  49. def working?
  50. (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)
  51. end
  52. def receive(incoming_events)
  53. incoming_events.each do |event|
  54. mo = interpolated(event)
  55. client[mo[:room_name]].send(mo[:username][0..14], mo[:message],
  56. notify: boolify(mo[:notify]),
  57. color: mo[:color],
  58. message_format: mo[:format].presence || 'html'
  59. )
  60. end
  61. end
  62. private
  63. def client
  64. @client ||= HipChat::Client.new(interpolated[:auth_token].presence || credential('hipchat_auth_token'))
  65. end
  66. end
  67. end