hipchat_agent.rb 3.2 KB

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