From 6a38bdac969592030a6cdbfbb279a78beab6c690 Mon Sep 17 00:00:00 2001 From: Vladimir Dimitrov Date: Tue, 3 Sep 2019 17:25:07 +0800 Subject: [PATCH] Added escape_symbols config param --- lib/fluent/plugin/out_slack.rb | 3 +++ lib/fluent/plugin/slack_client.rb | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/fluent/plugin/out_slack.rb b/lib/fluent/plugin/out_slack.rb index 1c52223..bf9d489 100644 --- a/lib/fluent/plugin/out_slack.rb +++ b/lib/fluent/plugin/out_slack.rb @@ -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 diff --git a/lib/fluent/plugin/slack_client.rb b/lib/fluent/plugin/slack_client.rb index ed9734d..caafb69 100644 --- a/lib/fluent/plugin/slack_client.rb +++ b/lib/fluent/plugin/slack_client.rb @@ -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 @@ -55,7 +55,7 @@ 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) @@ -63,7 +63,7 @@ def post(endpoint, params) private - def encode_body(params) + def encode_body(params, opts) raise NotImplementedError end @@ -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(/&/, '&').gsub(//, '>') + def encode_body(params = {}, opts = {}) + if opts[:escape_symbols] + # https://api.slack.com/docs/formatting + return to_json_with_scrub!(params).gsub(/&/, '&').gsub(//, '>') + end + + to_json_with_scrub!(params) end def response_check(res, params) @@ -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' @@ -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 @@ -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])