lib/fluent/plugin/out_slack.rb in fluent-plugin-slack-0.6.5 vs lib/fluent/plugin/out_slack.rb in fluent-plugin-slack-0.6.6

- old
+ new

@@ -84,11 +84,11 @@ config_param :auto_channels_create, :bool, default: false desc "https proxy url such as https://proxy.foo.bar:443" config_param :https_proxy, :string, default: nil desc "channel to send messages (without first '#')." - config_param :channel, :string + config_param :channel, :string, default: nil desc <<-DESC Keys used to format channel. %s will be replaced with value specified by channel_keys if this option is used. DESC config_param :channel_keys, default: nil do |val| @@ -128,15 +128,17 @@ end def configure(conf) conf['time_format'] ||= '%H:%M:%S' # old version compatiblity conf['localtime'] ||= true unless conf['utc'] - + super - @channel = URI.unescape(@channel) # old version compatibility - @channel = '#' + @channel unless @channel.start_with?('#') + if @channel + @channel = URI.unescape(@channel) # old version compatibility + @channel = '#' + @channel unless @channel.start_with?('#') + end if @webhook_url if @webhook_url.empty? raise Fluent::ConfigError.new("`webhook_url` is an empty string") end @@ -146,10 +148,14 @@ @slack = Fluent::SlackClient::IncomingWebhook.new(@webhook_url) elsif @slackbot_url if @slackbot_url.empty? raise Fluent::ConfigError.new("`slackbot_url` is an empty string") end + if @channel.nil? + raise Fluent::ConfigError.new("`channel` parameter required for Slackbot Remote Control") + end + if @username or @color or @icon_emoji or @icon_url log.warn "out_slack: `username`, `color`, `icon_emoji`, `icon_url` parameters are not available for Slackbot Remote Control" end unless @as_user.nil? log.warn "out_slack: `as_user` parameter are not available for Slackbot Remote Control" @@ -157,10 +163,14 @@ @slack = Fluent::SlackClient::Slackbot.new(@slackbot_url) elsif @token if @token.empty? raise Fluent::ConfigError.new("`token` is an empty string") end + if @channel.nil? + raise Fluent::ConfigError.new("`channel` parameter required for Slack WebApi") + end + @slack = Fluent::SlackClient::WebApi.new else raise Fluent::ConfigError.new("One of `webhook_url` or `slackbot_url`, or `token` is required") end @slack.log = log @@ -182,11 +192,11 @@ @title % (['1'] * @title_keys.length) rescue ArgumentError raise Fluent::ConfigError, "string specifier '%s' for `title` and `title_keys` specification mismatch" end end - if @channel_keys + if @channel && @channel_keys begin @channel % (['1'] * @channel_keys.length) rescue ArgumentError raise Fluent::ConfigError, "string specifier '%s' for `channel` and `channel_keys` specification mismatch" end @@ -286,17 +296,18 @@ fields.values.map { |f| "#{f.title} #{f.value}" }.join(' ') else fields.values.map(&:title).join(' ') end - { - channel: channel, + msg = { attachments: [{ :fallback => fallback_text, # fallback is the message shown on popup :fields => fields.values.map(&:to_h) }.merge(common_attachment)], - }.merge(common_payload) + } + msg.merge!(channel: channel) if channel + msg.merge!(common_payload) end end def build_color_payloads(chunk) messages = {} @@ -304,17 +315,18 @@ channel = build_channel(record) messages[channel] ||= '' messages[channel] << "#{build_message(record)}\n" end messages.map do |channel, text| - { - channel: channel, + msg = { attachments: [{ :fallback => text, :text => text, }.merge(common_attachment)], - }.merge(common_payload) + } + msg.merge!(channel: channel) if channel + msg.merge!(common_payload) end end def build_plain_payloads(chunk) messages = {} @@ -322,14 +334,13 @@ channel = build_channel(record) messages[channel] ||= '' messages[channel] << "#{build_message(record)}\n" end messages.map do |channel, text| - { - channel: channel, - text: text, - }.merge(common_payload) + msg = {text: text} + msg.merge!(channel: channel) if channel + msg.merge!(common_payload) end end def build_message(record) values = fetch_keys(record, @message_keys) @@ -342,9 +353,10 @@ values = fetch_keys(record, @title_keys) @title % values end def build_channel(record) + return nil if @channel.nil? return @channel unless @channel_keys values = fetch_keys(record, @channel_keys) @channel % values end