Enable attachments in the Slack agent.

ssube 9 years ago
parent
commit
5eb2063faa
1 changed files with 13 additions and 4 deletions
  1. 13 4
      app/models/agents/slack_agent.rb

+ 13 - 4
app/models/agents/slack_agent.rb

@@ -1,6 +1,7 @@
1 1
 module Agents
2 2
   class SlackAgent < Agent
3 3
     DEFAULT_USERNAME = 'Huginn'
4
+    ALLOWED_PARAMS = ['channel', 'username', 'unfurl_links', 'attachments']
4 5
 
5 6
     cannot_be_scheduled!
6 7
     cannot_create_events!
@@ -65,14 +66,22 @@ module Agents
65 66
       @slack_notifier ||= Slack::Notifier.new(webhook_url, username: username)
66 67
     end
67 68
 
69
+    def filter_options(opts)
70
+      opts.select { |key, value| ALLOWED_PARAMS.include? key }.symbolize_keys
71
+    end
72
+
68 73
     def receive(incoming_events)
69 74
       incoming_events.each do |event|
70 75
         opts = interpolated(event)
71
-        if /^:/.match(opts[:icon])
72
-          slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_emoji: opts[:icon], unfurl_links: opts[:unfurl_links]
73
-        else
74
-          slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_url: opts[:icon], unfurl_links: opts[:unfurl_links]
76
+        slack_opts = filter_options(opts)
77
+        if opts[:icon].to_s != ''
78
+          if /^:/.match(opts[:icon])
79
+            slack_opts[:icon_emoji] = opts[:icon]
80
+          else
81
+            slack_opts[:icon_url] = opts[:icon]
82
+          end
75 83
         end
84
+        slack_notifier.ping opts[:message], slack_opts
76 85
       end
77 86
     end
78 87
   end