├── .travis.yml ├── Rakefile ├── spec ├── spec_helper.rb └── outputs │ └── slack_spec.rb ├── .gitignore ├── NOTICE.TXT ├── .github ├── PULL_REQUEST_TEMPLATE.md ├── ISSUE_TEMPLATE.md └── CONTRIBUTING.md ├── Gemfile ├── CHANGELOG.md ├── logstash-output-slack.gemspec ├── README.md ├── lib └── logstash │ └── outputs │ └── slack.rb └── docs └── index.asciidoc /.travis.yml: -------------------------------------------------------------------------------- 1 | import: 2 | - logstash-plugins/.ci:travis/travis.yml@1.x -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | @files=[] 2 | 3 | task :default do 4 | system("rake -T") 5 | end 6 | 7 | require "logstash/devutils/rake" 8 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "logstash/devutils/rspec/spec_helper" 2 | require "logstash/outputs/slack" 3 | require 'webmock/rspec' 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | Gemfile.lock 3 | .ruby-version 4 | .bundle 5 | vendor 6 | lib/com 7 | lib/logstash-output-slack_jars.rb 8 | lib/org 9 | -------------------------------------------------------------------------------- /NOTICE.TXT: -------------------------------------------------------------------------------- 1 | Elasticsearch 2 | Copyright 2012-2015 Elasticsearch 3 | 4 | This product includes software developed by The Apache Software 5 | Foundation (http://www.apache.org/). -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thanks for contributing to Logstash! If you haven't already signed our CLA, here's a handy link: https://www.elastic.co/contributor-agreement/ 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash" 6 | use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1" 7 | 8 | if Dir.exist?(logstash_path) && use_logstash_source 9 | gem 'logstash-core', :path => "#{logstash_path}/logstash-core" 10 | gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api" 11 | end 12 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please post all product and debugging questions on our [forum](https://discuss.elastic.co/c/logstash). Your questions will reach our wider community members there, and if we confirm that there is a bug, then we can open a new issue here. 2 | 3 | For all general issues, please provide the following details for fast resolution: 4 | 5 | - Version: 6 | - Operating System: 7 | - Config File (if you have sensitive info, please remove it): 8 | - Sample Data: 9 | - Steps to Reproduce: 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 2.2.0 2 | - Dependencies: Remove constraint on public_suffix and enforce ruby >= 2.x 3 | 4 | ## 2.1.1 5 | - Docs: Set the default_codec doc attribute. 6 | 7 | ## 2.1.0 8 | - allow interpolation of event fields in url [#11](https://github.com/logstash-plugins/logstash-output-slack/pull/11) 9 | 10 | ## 2.0.2 11 | - Enable event based data in attachments 12 | 13 | ## 2.0.1 14 | - Update codec dependencies 15 | 16 | ## 2.0.0 17 | - Breaking: Updated plugin to use new Java Event APIs 18 | - relax logstash-core-plugin-api constrains 19 | - update .travis.yml 20 | 21 | ## 0.1.1 (https://github.com/cyli/logstash-output-slack/releases/tag/v0.1.1): 22 | - Added variable expansion to usernames and channel names ([#6](https://github.com/cyli/logstash-output-slack/pull/6)) 23 | - Fixed bug when reporting malformed requests ([#3](https://github.com/cyli/logstash-output-slack/pull/3)) 24 | - Test fixes since newer versions of logstash-core expects the values in 25 | the `add_field` hash to not be integers. 26 | ## 0.1.0(https://github.com/cyli/logstash-output-slack/releases/tag/v0.1.0): 27 | - initial version containing basic slack functionality 28 | -------------------------------------------------------------------------------- /logstash-output-slack.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = 'logstash-output-slack' 3 | s.version = '2.2.0' 4 | s.licenses = ['MIT','Apache License (2.0)'] 5 | s.summary = "Write events to Slack" 6 | s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program" 7 | s.authors = ["Ying Li"] 8 | s.email = 'cyli@ying.com' 9 | s.homepage = "https://github.com/cyli/logstash-output-slack" 10 | s.require_paths = ["lib"] 11 | 12 | # Files 13 | s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "docs/**/*"] 14 | 15 | # Tests 16 | s.test_files = s.files.grep(%r{^(test|spec|features)/}) 17 | 18 | # Special flag to let us know this is actually a logstash plugin 19 | s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" } 20 | 21 | s.required_ruby_version = '~> 2' 22 | 23 | # Gem dependencies 24 | 25 | s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" 26 | 27 | s.add_runtime_dependency "logstash-codec-plain" 28 | s.add_runtime_dependency "rest-client", '~> 1.8', ">= 1.8.0" 29 | s.add_development_dependency "logstash-devutils" 30 | s.add_development_dependency "logstash-filter-json" 31 | s.add_development_dependency "logstash-input-generator" 32 | s.add_development_dependency "webmock", "~> 1.22", ">= 1.21.0" 33 | end 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.com/logstash-plugins/logstash-output-slack.svg?branch=master)](https://travis-ci.com/logstash-plugins/logstash-output-slack) 2 | 3 | Reviews of the code/contributions are very welcome (particularly with testing!), since I don't really know Ruby. 4 | 5 | ## Logstash Slack Output Plugin 6 | 7 | [![Build 8 | Status](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Outputs/job/logstash-plugin-output-slack-unit/badge/icon)](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Outputs/job/logstash-plugin-output-slack-unit/) 9 | 10 | This is a plugin for [Logstash](https://github.com/elasticsearch/logstash) that pushes log events to [Slack](www.slack.com) using their [incoming webhooks API](https://api.slack.com/incoming-webhooks). 11 | 12 | It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way. 13 | 14 | ======= 15 | Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/). 16 | 17 | - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive 18 | - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide. 19 | 20 | ## Need Help? 21 | 22 | Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum. 23 | 24 | ## Usage 25 | 26 | ``` 27 | input { 28 | ... 29 | } 30 | 31 | filters { 32 | ... 33 | } 34 | 35 | output { 36 | ... 37 | slack { 38 | url => 39 | channel => [channel-name - optional] 40 | username => [slack username - optional] 41 | icon_emoji => [emoji, something like ":simple_smile:" - optional] 42 | icon_url => [icon url, would be overriden by icon_emoji - optional] 43 | format => [default is "%{message}", but used to format the text - optional] 44 | attachments => [an array of attachment maps as specified by the slack API - optional; if there is an "attachments" field in the event map and it is valid, it will override what is configured here, even if it's empty] 45 | } 46 | } 47 | ``` 48 | 49 | ## Contributing 50 | 51 | All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin. 52 | 53 | Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here. 54 | 55 | It is more important to the community that you are able to contribute. 56 | 57 | For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file. 58 | -------------------------------------------------------------------------------- /lib/logstash/outputs/slack.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require "logstash/outputs/base" 3 | require "logstash/namespace" 4 | 5 | class LogStash::Outputs::Slack < LogStash::Outputs::Base 6 | config_name "slack" 7 | milestone 1 8 | 9 | # The incoming webhook URI needed to post a message 10 | config :url, :validate => :string, :required => true 11 | 12 | # The text to post in slack 13 | config :format, :validate => :string, :default => "%{message}" 14 | 15 | # The channel to post to 16 | config :channel, :validate => :string 17 | 18 | # The username to use for posting 19 | config :username, :validate => :string 20 | 21 | # Emoji icon to use 22 | config :icon_emoji, :validate => :string 23 | 24 | # Icon URL to use 25 | config :icon_url, :validate => :string 26 | 27 | # Attachments array as described https://api.slack.com/docs/attachments 28 | config :attachments, :validate => :array 29 | 30 | public 31 | def register 32 | require 'rest-client' 33 | require 'cgi' 34 | require 'json' 35 | 36 | @content_type = "application/x-www-form-urlencoded" 37 | end # def register 38 | 39 | public 40 | def receive(event) 41 | return unless output?(event) 42 | 43 | payload_json = Hash.new 44 | payload_json['text'] = event.sprintf(@format) 45 | url = event.sprintf(@url) 46 | 47 | if not @channel.nil? 48 | payload_json['channel'] = event.sprintf(@channel) 49 | end 50 | 51 | if not @username.nil? 52 | payload_json['username'] = event.sprintf(@username) 53 | end 54 | 55 | if not @icon_emoji.nil? 56 | payload_json['icon_emoji'] = @icon_emoji 57 | end 58 | 59 | if not @icon_url.nil? 60 | payload_json['icon_url'] = @icon_url 61 | end 62 | 63 | if @attachments and @attachments.any? 64 | payload_json['attachments'] = @attachments.map { |x| JSON.parse(event.sprintf(JSON.dump(x))) } 65 | end 66 | if event.include?('attachments') and event.get('attachments').is_a?(Array) 67 | if event.get('attachments').any? 68 | # need to convert possibly from Java objects to Ruby Array, because 69 | # JSON dumps does not work well with Java ArrayLists, etc. 70 | rubified = JSON.parse(event.to_json()) 71 | payload_json['attachments'] = rubified['attachments'] 72 | else 73 | payload_json.delete('attachments') 74 | end 75 | end 76 | 77 | begin 78 | RestClient.post( 79 | url, 80 | "payload=#{CGI.escape(JSON.dump(payload_json))}", 81 | :accept => "application/json", 82 | :'User-Agent' => "logstash-output-slack", 83 | :content_type => @content_type) { |response, request, result, &block| 84 | if response.code != 200 85 | @logger.warn("Got a #{response.code} response: #{response}") 86 | end 87 | } 88 | rescue Exception => e 89 | @logger.warn("Unhandled exception", :exception => e, 90 | :stacktrace => e.backtrace) 91 | end 92 | end # def receive 93 | end # class LogStash::Outputs::Slack 94 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Logstash 2 | 3 | All contributions are welcome: ideas, patches, documentation, bug reports, 4 | complaints, etc! 5 | 6 | Programming is not a required skill, and there are many ways to help out! 7 | It is more important to us that you are able to contribute. 8 | 9 | That said, some basic guidelines, which you are free to ignore :) 10 | 11 | ## Want to learn? 12 | 13 | Want to lurk about and see what others are doing with Logstash? 14 | 15 | * The irc channel (#logstash on irc.freenode.org) is a good place for this 16 | * The [forum](https://discuss.elastic.co/c/logstash) is also 17 | great for learning from others. 18 | 19 | ## Got Questions? 20 | 21 | Have a problem you want Logstash to solve for you? 22 | 23 | * You can ask a question in the [forum](https://discuss.elastic.co/c/logstash) 24 | * Alternately, you are welcome to join the IRC channel #logstash on 25 | irc.freenode.org and ask for help there! 26 | 27 | ## Have an Idea or Feature Request? 28 | 29 | * File a ticket on [GitHub](https://github.com/elastic/logstash/issues). Please remember that GitHub is used only for issues and feature requests. If you have a general question, the [forum](https://discuss.elastic.co/c/logstash) or IRC would be the best place to ask. 30 | 31 | ## Something Not Working? Found a Bug? 32 | 33 | If you think you found a bug, it probably is a bug. 34 | 35 | * If it is a general Logstash or a pipeline issue, file it in [Logstash GitHub](https://github.com/elasticsearch/logstash/issues) 36 | * If it is specific to a plugin, please file it in the respective repository under [logstash-plugins](https://github.com/logstash-plugins) 37 | * or ask the [forum](https://discuss.elastic.co/c/logstash). 38 | 39 | # Contributing Documentation and Code Changes 40 | 41 | If you have a bugfix or new feature that you would like to contribute to 42 | logstash, and you think it will take more than a few minutes to produce the fix 43 | (ie; write code), it is worth discussing the change with the Logstash users and developers first! You can reach us via [GitHub](https://github.com/elastic/logstash/issues), the [forum](https://discuss.elastic.co/c/logstash), or via IRC (#logstash on freenode irc) 44 | Please note that Pull Requests without tests will not be merged. If you would like to contribute but do not have experience with writing tests, please ping us on IRC/forum or create a PR and ask our help. 45 | 46 | ## Contributing to plugins 47 | 48 | Check our [documentation](https://www.elastic.co/guide/en/logstash/current/contributing-to-logstash.html) on how to contribute to plugins or write your own! It is super easy! 49 | 50 | ## Contribution Steps 51 | 52 | 1. Test your changes! [Run](https://github.com/elastic/logstash#testing) the test suite 53 | 2. Please make sure you have signed our [Contributor License 54 | Agreement](https://www.elastic.co/contributor-agreement/). We are not 55 | asking you to assign copyright to us, but to give us the right to distribute 56 | your code without restriction. We ask this of all contributors in order to 57 | assure our users of the origin and continuing existence of the code. You 58 | only need to sign the CLA once. 59 | 3. Send a pull request! Push your changes to your fork of the repository and 60 | [submit a pull 61 | request](https://help.github.com/articles/using-pull-requests). In the pull 62 | request, describe what your changes do and mention any bugs/issues related 63 | to the pull request. 64 | 65 | 66 | -------------------------------------------------------------------------------- /docs/index.asciidoc: -------------------------------------------------------------------------------- 1 | :plugin: slack 2 | :type: output 3 | :default_codec: plain 4 | 5 | /////////////////////////////////////////// 6 | START - GENERATED VARIABLES, DO NOT EDIT! 7 | /////////////////////////////////////////// 8 | :version: %VERSION% 9 | :release_date: %RELEASE_DATE% 10 | :changelog_url: %CHANGELOG_URL% 11 | :include_path: ../../../../logstash/docs/include 12 | /////////////////////////////////////////// 13 | END - GENERATED VARIABLES, DO NOT EDIT! 14 | /////////////////////////////////////////// 15 | 16 | [id="plugins-{type}s-{plugin}"] 17 | 18 | === Slack output plugin 19 | 20 | include::{include_path}/plugin_header.asciidoc[] 21 | 22 | ==== Description 23 | 24 | Write events to Slack. 25 | 26 | [id="plugins-{type}s-{plugin}-options"] 27 | ==== Slack Output Configuration Options 28 | 29 | This plugin supports the following configuration options plus the <> described later. 30 | 31 | [cols="<,<,<",options="header",] 32 | |======================================================================= 33 | |Setting |Input type|Required 34 | | <> |<>|No 35 | | <> |<>|No 36 | | <> |<>|No 37 | | <> |<>|No 38 | | <> |<>|No 39 | | <> |<>|Yes 40 | | <> |<>|No 41 | |======================================================================= 42 | 43 | Also see <> for a list of options supported by all 44 | output plugins. 45 | 46 |   47 | 48 | [id="plugins-{type}s-{plugin}-attachments"] 49 | ===== `attachments` 50 | 51 | * Value type is <> 52 | * There is no default value for this setting. 53 | 54 | Attachments array as described https://api.slack.com/docs/attachments 55 | 56 | [id="plugins-{type}s-{plugin}-channel"] 57 | ===== `channel` 58 | 59 | * Value type is <> 60 | * There is no default value for this setting. 61 | 62 | The channel to post to 63 | 64 | [id="plugins-{type}s-{plugin}-format"] 65 | ===== `format` 66 | 67 | * Value type is <> 68 | * Default value is `"%{message}"` 69 | 70 | The text to post in slack 71 | 72 | [id="plugins-{type}s-{plugin}-icon_emoji"] 73 | ===== `icon_emoji` 74 | 75 | * Value type is <> 76 | * There is no default value for this setting. 77 | 78 | Emoji icon to use 79 | 80 | [id="plugins-{type}s-{plugin}-icon_url"] 81 | ===== `icon_url` 82 | 83 | * Value type is <> 84 | * There is no default value for this setting. 85 | 86 | Icon URL to use 87 | 88 | [id="plugins-{type}s-{plugin}-url"] 89 | ===== `url` 90 | 91 | * This is a required setting. 92 | * Value type is <> 93 | * There is no default value for this setting. 94 | 95 | The incoming webhook URI needed to post a message 96 | 97 | [id="plugins-{type}s-{plugin}-username"] 98 | ===== `username` 99 | 100 | * Value type is <> 101 | * There is no default value for this setting. 102 | 103 | The username to use for posting 104 | 105 | 106 | 107 | [id="plugins-{type}s-{plugin}-common-options"] 108 | include::{include_path}/{type}.asciidoc[] 109 | 110 | :default_codec!: -------------------------------------------------------------------------------- /spec/outputs/slack_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative "../spec_helper" 2 | 3 | describe LogStash::Outputs::Slack do 4 | 5 | subject { described_class.new(config) } 6 | let(:config) { { } } 7 | let(:event) { LogStash::Event.new(data) } 8 | let(:data) { {} } 9 | 10 | # Actually do most of the boiler plate by stubbing out the request, running 11 | # the logstash pipeline, and asserting that a request was made with the 12 | # expected JSON. 13 | def test_one_event(event, expected_json, expected_url = "http://requestb.in/r9lkbzr9") 14 | stub_request(:post, "requestb.in"). 15 | to_return(:body => "", :status => 200, 16 | :headers => { 'Content-Length' => 0 }) 17 | 18 | subject.receive(event) 19 | 20 | expect(a_request(:post, expected_url). 21 | with(:body => "payload=#{CGI.escape(JSON.dump(expected_json))}", 22 | :headers => { 23 | 'Content-Type' => 'application/x-www-form-urlencoded', 24 | 'Accept'=> 'application/json', 25 | 'User-Agent' => 'logstash-output-slack' 26 | })). 27 | to have_been_made.once 28 | end 29 | 30 | before(:each) do 31 | subject.register 32 | WebMock.disable_net_connect! 33 | end 34 | 35 | after(:each) do 36 | WebMock.reset! 37 | WebMock.allow_net_connect! 38 | end 39 | 40 | context "passes the right payload to slack" do 41 | context "simple" do 42 | let(:data) { { "message" => "This message should show in slack" } } 43 | let(:config) { { "url" => "http://requestb.in/r9lkbzr9" } } 44 | 45 | it "uses all default values" do 46 | expected_json = { :text => "This message should show in slack" } 47 | test_one_event(event, expected_json) 48 | end 49 | end 50 | 51 | context "with multiple interpolations" do 52 | let(:data) { { 53 | "message" => "This message should show in slack", 54 | "x" => "3", 55 | "channelname" => "mychannel", 56 | "username" => "slackbot" 57 | } } 58 | 59 | let(:config) { { 60 | "url" => "http://requestb.in/r9lkbzr9", 61 | "format" => "%{message} %{x}", 62 | "channel" => "%{channelname}", 63 | "username" => "%{username}", 64 | "icon_emoji" => ":chart_with_upwards_trend:", 65 | "icon_url" => "http://lorempixel.com/48/48", 66 | } } 67 | 68 | it "uses and formats all provided values" do 69 | expected_json = { 70 | :text => "This message should show in slack 3", 71 | :channel => "mychannel", 72 | :username => "slackbot", 73 | :icon_emoji => ":chart_with_upwards_trend:", 74 | :icon_url => "http://lorempixel.com/48/48" 75 | } 76 | test_one_event(event, expected_json) 77 | end 78 | end 79 | 80 | context "if the message contains no interpolations" do 81 | 82 | let(:data) { { 83 | "message" => "This message should show in slack" 84 | } } 85 | 86 | let(:config) { { 87 | "url" => "http://requestb.in/r9lkbzr9", 88 | "format" => "Unformatted message", 89 | "channel" => "mychannel", 90 | "username" => "slackbot", 91 | "icon_emoji" => ":chart_with_upwards_trend:", 92 | "icon_url" => "http://lorempixel.com/48/48" 93 | } } 94 | 95 | it "uses and formats all provided values" do 96 | expected_json = { 97 | :text => "Unformatted message", 98 | :channel => "mychannel", 99 | :username => "slackbot", 100 | :icon_emoji => ":chart_with_upwards_trend:", 101 | :icon_url => "http://lorempixel.com/48/48" 102 | } 103 | test_one_event(event, expected_json) 104 | end 105 | end 106 | 107 | describe "default attachements" do 108 | let(:config) { { 109 | "url" => "http://requestb.in/r9lkbzr9", 110 | "attachments" => [{ "image_url" => "http://example.com/image.png" }] 111 | } } 112 | 113 | context "when none are in the event" do 114 | let(:data) { { "message" => "This message should show in slack" } } 115 | it "uses the default" do 116 | expected_json = { 117 | :text => "This message should show in slack", 118 | :attachments => [{:image_url => "http://example.com/image.png"}] 119 | } 120 | test_one_event(event, expected_json) 121 | end 122 | end 123 | 124 | context "when using multiple attachments" do 125 | let(:data) { { "message" => "This message should show in slack" } } 126 | let(:config) { { 127 | "url" => "http://requestb.in/r9lkbzr9", 128 | "attachments" => [ 129 | {"image_url" => "http://example.com/image1.png"}, 130 | {"image_url" => "http://example.com/image2.png"} 131 | ] 132 | } } 133 | it "sends them" do 134 | expected_json = { 135 | :text => "This message should show in slack", 136 | :attachments => [{:image_url => "http://example.com/image1.png"}, 137 | {:image_url => "http://example.com/image2.png"}] 138 | } 139 | test_one_event(event, expected_json) 140 | end 141 | end 142 | end 143 | 144 | context "empty default attachments" do 145 | let(:data) { { 146 | "message" => "This message should show in slack" 147 | }} 148 | 149 | let(:config) { { 150 | "url" => "http://requestb.in/r9lkbzr9", 151 | "attachments" => [] 152 | }} 153 | 154 | it "are ignored" do 155 | expected_json = { 156 | :text => "This message should show in slack" 157 | } 158 | test_one_event(event, expected_json) 159 | end 160 | end 161 | 162 | context "when both event attachments and default attachments are set" do 163 | 164 | let(:data) { { 165 | "message" => "This message should show in slack", 166 | "attachments" => [{"thumb_url" => "http://other.com/thumb.png"}] 167 | } } 168 | 169 | let(:config) { { 170 | "url" => "http://requestb.in/r9lkbzr9", 171 | "attachments" => [ 172 | {"image_url" => "http://example.com/image1.png"}, 173 | {"image_url" => "http://example.com/image2.png"} 174 | ] 175 | } } 176 | 177 | it "event attachements prevail" do 178 | expected_json = { 179 | :text => "This message should show in slack", 180 | :attachments => [{:thumb_url => "http://other.com/thumb.png"}] 181 | } 182 | test_one_event(event, expected_json) 183 | end 184 | end 185 | 186 | context "if event attachments empty" do 187 | 188 | let(:data) { { 189 | "message" => "This message should show in slack", 190 | "attachments" => [] 191 | } } 192 | 193 | let(:config) { { 194 | "url" => "http://requestb.in/r9lkbzr9", 195 | "attachments" => [ 196 | {"image_url" => "http://example.com/image1.png"}, 197 | {"image_url" => "http://example.com/image2.png"} 198 | ] 199 | } } 200 | 201 | it "erases default attachments" do 202 | expected_json = { 203 | :text => "This message should show in slack" 204 | } 205 | test_one_event(event, expected_json) 206 | end 207 | end 208 | 209 | context "when event attachements field isn't an array" do 210 | let(:data) { { 211 | "message" => "This message should show in slack", 212 | "attachments" => "baddata" 213 | } } 214 | 215 | let(:config) { { 216 | "url" => "http://requestb.in/r9lkbzr9", 217 | "attachments" => [ 218 | {"image_url" => "http://example.com/image.png"} 219 | ] 220 | } } 221 | 222 | 223 | it "is ignored" do 224 | expected_json = { 225 | :text => "This message should show in slack", 226 | :attachments => [{:image_url => "http://example.com/image.png"}] 227 | } 228 | test_one_event(event, expected_json) 229 | end 230 | end 231 | end 232 | 233 | describe "interpolation in url field" do 234 | let(:expected_url) { "http://incoming-webhook.example.com" } 235 | 236 | let(:event) { 237 | event = LogStash::Event.new("message" => "This message should show in slack") 238 | event.set("[@metadata][slack_url]", "#{expected_url}") 239 | event 240 | } 241 | 242 | let(:config) { { 243 | "url" => "%{[@metadata][slack_url]}", 244 | "attachments" => [ 245 | {"image_url" => "http://example.com/image.png"} 246 | ] 247 | } } 248 | 249 | it "allows interpolation in url field" do 250 | expected_json = { 251 | :text => "This message should show in slack", 252 | :attachments => [{:image_url => "http://example.com/image.png"}] 253 | } 254 | test_one_event(event, expected_json, expected_url) 255 | end 256 | end 257 | end 258 | --------------------------------------------------------------------------------