Keine Beschreibung http://j1x-huginn.herokuapp.com

hipchat_agent.rb 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. If you want to specify either of those attributes per event, you can provide a [JSONPath](http://goessner.net/articles/JsonPath/) for each of them (except the `auth_token`).
  14. MD
  15. def default_options
  16. {
  17. 'auth_token' => '',
  18. 'room_name' => '',
  19. 'room_name_path' => '',
  20. 'username' => "Huginn",
  21. 'username_path' => '',
  22. 'message' => "Hello from Huginn!",
  23. 'message_path' => '',
  24. 'notify' => false,
  25. 'notify_path' => '',
  26. 'color' => 'yellow',
  27. 'color_path' => '',
  28. }
  29. end
  30. def validate_options
  31. errors.add(:base, "you need to specify a hipchat auth_token") unless options['auth_token'].present?
  32. 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?
  33. end
  34. def working?
  35. (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)
  36. end
  37. def receive(incoming_events)
  38. client = HipChat::Client.new(options[:auth_token])
  39. incoming_events.each do |event|
  40. mo = merge_options event
  41. client[mo[:room_name]].send(mo[:username], mo[:message], :notify => mo[:notify].to_s == 'true' ? 1 : 0, :color => mo[:color])
  42. end
  43. end
  44. private
  45. def select_option(event, a)
  46. if options[a.to_s + '_path'].present?
  47. Utils.value_at(event.payload, options[a.to_s + '_path'])
  48. else
  49. options[a]
  50. end
  51. end
  52. def options_with_path
  53. [:room_name, :username, :message, :notify, :color]
  54. end
  55. def merge_options event
  56. options.select { |k, v| options_with_path.include? k}.tap do |merged_options|
  57. options_with_path.each do |a|
  58. merged_options[a] = select_option(event, a)
  59. end
  60. end
  61. end
  62. end
  63. end