hipchat_agent.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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,
  48. "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?
  49. errors.add(:base,
  50. "you need to specify a room_name or a room_name_path") if options['room_name'].blank? && options['room_name_path'].blank?
  51. end
  52. def working?
  53. (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)
  54. end
  55. def receive(incoming_events)
  56. incoming_events.each do |event|
  57. mo = interpolated(event)
  58. client[mo[:room_name]].send(
  59. mo[:username][0..14],
  60. mo[:message],
  61. notify: boolify(mo[:notify]),
  62. color: mo[:color],
  63. message_format: mo[:format].presence || 'html'
  64. )
  65. end
  66. end
  67. private
  68. def client
  69. @client ||= HipChat::Client.new(interpolated[:auth_token].presence || credential('hipchat_auth_token'))
  70. end
  71. end
  72. end