slack_agent.rb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. module Agents
  2. class SlackAgent < Agent
  3. DEFAULT_USERNAME = 'Huginn'
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. gem_dependency_check { defined?(Slack) }
  7. description <<-MD
  8. #{'## Include `slack-notifier` in your Gemfile to use this Agent!' if dependencies_missing?}
  9. The SlackAgent lets you receive events and send notifications to [Slack](https://slack.com/).
  10. To get started, you will first need to setup an incoming webhook.
  11. Go to, <code>https://<em>your_team_name</em>.slack.com/services/new/incoming-webhook</code>,
  12. choose a default channel and add the integration.
  13. Your webhook URL will look like: <code>https://hooks.slack.com/services/<em>random1</em>/<em>random2</em>/<em>token</em></code>
  14. Once the webhook has been setup it can be used to post to other channels or ping team members.
  15. To send a private message to team-mate, assign his username as `@username` to the channel option.
  16. To communicate with a different webhook on slack, assign your custom webhook name to the webhook option.
  17. Messages can also be formatted using [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid).
  18. Finally, you can set a custom icon for this webhook in `icon`, either as [emoji](http://unicodey.com/emoji-data/table.htm) or an URL to an image.
  19. Leaving this field blank will use the default icon for a webhook.
  20. MD
  21. def default_options
  22. {
  23. 'webhook_url' => 'https://hooks.slack.com/services/...',
  24. 'channel' => '#general',
  25. 'username' => DEFAULT_USERNAME,
  26. 'message' => "Hey there, It's Huginn",
  27. 'icon' => '',
  28. }
  29. end
  30. def validate_options
  31. unless options['webhook_url'].present? ||
  32. (options['auth_token'].present? && options['team_name'].present?) # compatibility
  33. errors.add(:base, "webhook_url is required")
  34. end
  35. errors.add(:base, "channel is required") unless options['channel'].present?
  36. end
  37. def working?
  38. received_event_without_error?
  39. end
  40. def webhook_url
  41. case
  42. when url = interpolated[:webhook_url].presence
  43. url
  44. when (team = interpolated[:team_name].presence) && (token = interpolated[:auth_token])
  45. webhook = interpolated[:webhook].presence || 'incoming-webhook'
  46. # old style webhook URL
  47. "https://#{Rack::Utils.escape_path(team)}.slack.com/services/hooks/#{Rack::Utils.escape_path(webhook)}?token=#{Rack::Utils.escape(token)}"
  48. end
  49. end
  50. def username
  51. interpolated[:username].presence || DEFAULT_USERNAME
  52. end
  53. def slack_notifier
  54. @slack_notifier ||= Slack::Notifier.new(webhook_url, username: username)
  55. end
  56. def receive(incoming_events)
  57. incoming_events.each do |event|
  58. opts = interpolated(event)
  59. if /^:/.match(opts[:icon])
  60. slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_emoji: opts[:icon]
  61. else
  62. slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_url: opts[:icon]
  63. end
  64. end
  65. end
  66. end
  67. end