slack_agent.rb 3.1 KB

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