Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/fluent/plugin/out_slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def desc(description)
desc "Include messages to the fallback attributes"
config_param :verbose_fallback, :bool, default: false

desc "Escape special symbols in message"
config_param :escape_symbols, :bool, default: true

# for test
attr_reader :slack, :time_format, :localtime, :timef, :mrkdwn_in, :post_message_opts

Expand Down
22 changes: 13 additions & 9 deletions lib/fluent/plugin/slack_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def proxy_class
@proxy_class ||= Net::HTTP
end

def post(endpoint, params)
def post(endpoint, params, opts = {})
http = proxy_class.new(endpoint.host, endpoint.port)
http.use_ssl = (endpoint.scheme == 'https')
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Expand All @@ -55,15 +55,15 @@ def post(endpoint, params)
req['Host'] = endpoint.host
req['Accept'] = 'application/json; charset=utf-8'
req['User-Agent'] = 'fluent-plugin-slack'
req.body = encode_body(params)
req.body = encode_body(params, opts)

res = http.request(req)
response_check(res, params)
end

private

def encode_body(params)
def encode_body(params, opts)
raise NotImplementedError
end

Expand Down Expand Up @@ -142,9 +142,13 @@ def post_message(params = {}, opts = {})

private

def encode_body(params = {})
# https://api.slack.com/docs/formatting
to_json_with_scrub!(params).gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')
def encode_body(params = {}, opts = {})
if opts[:escape_symbols]
# https://api.slack.com/docs/formatting
return to_json_with_scrub!(params).gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')
end

to_json_with_scrub!(params)
end

def response_check(res, params)
Expand Down Expand Up @@ -180,7 +184,7 @@ def slackbot_endpoint(params)
endpoint.dup.tap {|e| e.query += "&channel=#{URI.encode(params[:channel])}" }
end

def encode_body(params = {})
def encode_body(params = {}, opts = {})
return params[:text]if params[:text]
unless params[:attachments]
raise ArgumentError, 'params[:text] or params[:attachments] is required'
Expand Down Expand Up @@ -244,7 +248,7 @@ def channels_create_endpoint
def post_message(params = {}, opts = {})
with_channels_create(params, opts) do
log.info { "out_slack: post_message #{filter_params(params)}" }
post(post_message_endpoint, params)
post(post_message_endpoint, params, opts)
end
end

Expand All @@ -263,7 +267,7 @@ def channels_create(params = {}, opts = {})

private

def encode_body(params = {})
def encode_body(params = {}, opts = {})
body = params.dup
if params[:attachments]
body[:attachments] = to_json_with_scrub!(params[:attachments])
Expand Down