Няма описание http://j1x-huginn.herokuapp.com

slack_agent.rb 3.1KB

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