Class: LogStash::Outputs::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/slack.rb

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/logstash/outputs/slack.rb', line 40

def receive(event)
  return unless output?(event)

  payload_json = Hash.new
  payload_json['text'] = event.sprintf(@format)

  if not @channel.nil?
    payload_json['channel'] = event.sprintf(@channel)
  end

  if not @username.nil?
    payload_json['username'] = event.sprintf(@username)
  end

  if not @icon_emoji.nil?
    payload_json['icon_emoji'] = @icon_emoji
  end

  if not @icon_url.nil?
    payload_json['icon_url'] = @icon_url
  end

  if @attachments and @attachments.any?
    payload_json['attachments'] = @attachments.map { |x| JSON.parse(event.sprintf(JSON.dump(x))) }
  end
  if event.include?('attachments') and event.get('attachments').is_a?(Array)
    if event.get('attachments').any?
      # need to convert possibly from Java objects to Ruby Array, because
      # JSON dumps does not work well with Java ArrayLists, etc.
      rubified = JSON.parse(event.to_json())
      payload_json['attachments'] = rubified['attachments']
    else
      payload_json.delete('attachments')
    end
  end

  begin
    RestClient.post(
      @url,
      "payload=#{CGI.escape(JSON.dump(payload_json))}",
      :accept => "application/json",
      :'User-Agent' => "logstash-output-slack",
      :content_type => @content_type) { |response, request, result, &block|
        if response.code != 200
          @logger.warn("Got a #{response.code} response: #{response}")
        end
      }
  rescue Exception => e
    @logger.warn("Unhandled exception", :exception => e,
                 :stacktrace => e.backtrace)
  end
end

#registerObject



31
32
33
34
35
36
37
# File 'lib/logstash/outputs/slack.rb', line 31

def register
  require 'rest-client'
  require 'cgi'
  require 'json'

  @content_type = "application/x-www-form-urlencoded"
end