slack_agent.rb 3.1 KB

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