alerty-plugin-slack 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +6 -0
- data/alerty-plugin-slack.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example/slackbot.yml +10 -0
- data/example/webhook.yml +8 -0
- data/lib/alerty/plugin/slack.rb +82 -0
- data/lib/alerty/plugin/slack/slack_client.rb +283 -0
- data/lib/alerty/plugin/slack/version.rb +7 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f586824308ff49aa74e3b4b02e3bd272abd45333
|
4
|
+
data.tar.gz: aefff96c07fc41416f9561a1b77490e1221d9848
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65d0ccc18dde3c160b8fe684ab8c0e4921bfb0d6af9e0c81dfd5d261ad7259d68741a16c335cb4e5c39a639c7623d2f90629f2f7c0c71f291fd5e9f364409c2d
|
7
|
+
data.tar.gz: 79acd12fdb51d4794c938baf10c54711a48f8203571b1b9c2243bb34032c0ebab88cc337b63ebcafaad84609771c02237255e79032dd0dbeda51db5252119f7e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at sonots@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Naotoshi Seo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Alerty::Plugin::Slack
|
2
|
+
|
3
|
+
Slack plugin for [alerty](https://github.com/sonots/alerty).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'alerty-plugin-slack'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install alerty-plugin-slack
|
20
|
+
|
21
|
+
## Configuration
|
22
|
+
|
23
|
+
- type: must be `slack`
|
24
|
+
- webhook_url: Slack Webhook URL. Get from https://slack.com/apps/A0F7XDUAZ-incoming-webhooks
|
25
|
+
- slackbot_url: Slack SlackBot URL. Get from https://analytics-support.slack.com/apps/A0F81R8ET-slackbot
|
26
|
+
- webapi_token: Slack WebAPI token
|
27
|
+
- payload
|
28
|
+
- text: text message
|
29
|
+
- channel: channel name
|
30
|
+
- see https://api.slack.com/methods/chat.postMessage and https://api.slack.com/docs/message-attachments
|
31
|
+
|
32
|
+
## Placeholders
|
33
|
+
|
34
|
+
* `${hostname}`: hostname which rans the command
|
35
|
+
* `${command}`: a given command
|
36
|
+
* `${exitstatus}`: exitstatus of the executed command
|
37
|
+
* `${output}`: output of the executed command
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
```
|
42
|
+
bundle exec alerty -c example/slackbot.yml -- ls /this_is_a_test
|
43
|
+
```
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sonots/alerty-plugin-slack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'alerty/plugin/slack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "alerty-plugin-slack"
|
8
|
+
spec.version = Alerty::Plugin::Slack::VERSION
|
9
|
+
spec.authors = ["Naotoshi Seo"]
|
10
|
+
spec.email = ["sonots@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Slack plugin for alerty.}
|
13
|
+
spec.description = %q{Slack plugin for alerty.}
|
14
|
+
spec.homepage = "https://github.com/sonots/alerty-plugin-slack"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "alerty"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "alerty/plugin/slack"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
log_path: STDOUT
|
2
|
+
log_level: debug
|
3
|
+
plugins:
|
4
|
+
- type: slack
|
5
|
+
slackbot_url: https://xxxxxx.slack.com/services/hooks/slackbot?token=XXXXXXXXXXXXXXXXXXXXX
|
6
|
+
# see https://api.slack.com/methods/chat.postMessage
|
7
|
+
payload:
|
8
|
+
channel: alerty_test
|
9
|
+
text: "FAILURE: [${hostname}] ${command}\n${output}"
|
10
|
+
|
data/example/webhook.yml
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'alerty'
|
2
|
+
require_relative 'slack/version'
|
3
|
+
require_relative 'slack/slack_client'
|
4
|
+
|
5
|
+
class Alerty
|
6
|
+
class Plugin
|
7
|
+
class Slack
|
8
|
+
def initialize(config)
|
9
|
+
@payload = config.payload || {}
|
10
|
+
if config.webhook_url
|
11
|
+
raise ConfigError.new('slack: webhook_url is given, but empty') if config.webhook_url.empty?
|
12
|
+
@client = SlackClient::IncomingWebhook.new(config.webhook_url)
|
13
|
+
elsif config.slackbot_url
|
14
|
+
raise ConfigError.new('slack: slackbot_url is given, but empty') if config.slackbot_url.empty?
|
15
|
+
@client = SlackClient::Slackbot.new(config.slackbot_url)
|
16
|
+
elsif config.webapi_token
|
17
|
+
raise ConfigError.new('slack: webapi_token is given, but empty') if config.webapi_token.empty?
|
18
|
+
@client = SlackClient::Webapi.new
|
19
|
+
@payload['token'] = config.webapi_token
|
20
|
+
end
|
21
|
+
@client.log = Alerty.logger
|
22
|
+
@client.https_proxy = config.https_proxy if config.https_proxy
|
23
|
+
|
24
|
+
@num_retries = config.num_retries || 3
|
25
|
+
end
|
26
|
+
|
27
|
+
def alert(record)
|
28
|
+
payload = build_payload(record).to_h
|
29
|
+
with_retry(@num_retries) { @client.post_message(payload) }
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def with_retry(num_retries, &block)
|
35
|
+
retries = 0
|
36
|
+
begin
|
37
|
+
yield
|
38
|
+
rescue => e
|
39
|
+
retries += 1
|
40
|
+
sleep 1
|
41
|
+
if retries <= num_retries
|
42
|
+
retry
|
43
|
+
else
|
44
|
+
raise e
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# https://api.slack.com/methods/chat.postMessage
|
50
|
+
# https://api.slack.com/docs/messages
|
51
|
+
# https://api.slack.com/docs/messages/builder
|
52
|
+
# https://api.slack.com/docs/message-attachments
|
53
|
+
def build_payload(record)
|
54
|
+
deep_expand_placeholder(@payload, record)
|
55
|
+
end
|
56
|
+
|
57
|
+
def deep_expand_placeholder(obj, record)
|
58
|
+
case obj
|
59
|
+
when Hash
|
60
|
+
obj.each do |k, v|
|
61
|
+
obj[k] = deep_expand_placeholder(v, record)
|
62
|
+
end
|
63
|
+
when Array
|
64
|
+
obj.each_with_index do |v, i|
|
65
|
+
obj[i] = deep_expand_placeholder(v, record)
|
66
|
+
end
|
67
|
+
when String
|
68
|
+
expand_placeholder(obj, record)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def expand_placeholder(str, record)
|
73
|
+
str.dup.tap {|s|
|
74
|
+
s.gsub!('${hostname}', record[:hostname])
|
75
|
+
s.gsub!('${command}', record[:command])
|
76
|
+
s.gsub!('${exitstatus}', record[:exitstatus].to_s)
|
77
|
+
s.gsub!('${output}', record[:output])
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,283 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
class Alerty
|
7
|
+
class Plugin
|
8
|
+
class Slack
|
9
|
+
module SlackClient
|
10
|
+
class Error < StandardError
|
11
|
+
attr_reader :res, :req_params
|
12
|
+
|
13
|
+
def initialize(res, req_params = {})
|
14
|
+
@res = res
|
15
|
+
@req_params = req_params.dup
|
16
|
+
end
|
17
|
+
|
18
|
+
def message
|
19
|
+
@req_params['token'] = '[FILTERED]' if @req_params['token']
|
20
|
+
"res.code:#{@res.code}, res.body:#{@res.body}, req_params:#{@req_params}"
|
21
|
+
end
|
22
|
+
|
23
|
+
alias :to_s :message
|
24
|
+
end
|
25
|
+
class ChannelNotFoundError < Error; end
|
26
|
+
class NameTakenError < Error; end
|
27
|
+
|
28
|
+
# The base framework of slack client
|
29
|
+
class Base
|
30
|
+
attr_accessor :log, :debug_dev
|
31
|
+
attr_reader :endpoint, :https_proxy
|
32
|
+
|
33
|
+
# @param [String] endpoint
|
34
|
+
#
|
35
|
+
# (Incoming Webhook) required
|
36
|
+
# https://hooks.slack.com/services/XXX/XXX/XXX
|
37
|
+
#
|
38
|
+
# (Slackbot) required
|
39
|
+
# https://xxxx.slack.com/services/hooks/slackbot?token=XXXXX
|
40
|
+
#
|
41
|
+
# (Web API) optional and default to be
|
42
|
+
# https://slack.com/api/
|
43
|
+
#
|
44
|
+
# @param [String] https_proxy (optional)
|
45
|
+
#
|
46
|
+
# https://proxy.foo.bar:port
|
47
|
+
#
|
48
|
+
def initialize(endpoint = nil, https_proxy = nil)
|
49
|
+
self.endpoint = endpoint if endpoint
|
50
|
+
self.https_proxy = https_proxy if https_proxy
|
51
|
+
@log = Logger.new(nil)
|
52
|
+
end
|
53
|
+
|
54
|
+
def endpoint=(endpoint)
|
55
|
+
@endpoint = URI.parse(endpoint)
|
56
|
+
end
|
57
|
+
|
58
|
+
def https_proxy=(https_proxy)
|
59
|
+
@https_proxy = URI.parse(https_proxy)
|
60
|
+
@proxy_class = Net::HTTP.Proxy(@https_proxy.host, @https_proxy.port)
|
61
|
+
end
|
62
|
+
|
63
|
+
def proxy_class
|
64
|
+
@proxy_class ||= Net::HTTP
|
65
|
+
end
|
66
|
+
|
67
|
+
def post(endpoint, params)
|
68
|
+
http = proxy_class.new(endpoint.host, endpoint.port)
|
69
|
+
http.use_ssl = (endpoint.scheme == 'https')
|
70
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
71
|
+
http.set_debug_output(debug_dev) if debug_dev
|
72
|
+
# http.open_timeout = # default: 60s
|
73
|
+
# http.read_timeout = # default: 60s
|
74
|
+
|
75
|
+
req = Net::HTTP::Post.new(endpoint.request_uri)
|
76
|
+
req['Host'] = endpoint.host
|
77
|
+
req['Accept'] = 'application/json; charset=utf-8'
|
78
|
+
req['User-Agent'] = 'fluent-plugin-slack'
|
79
|
+
req.body = encode_body(params)
|
80
|
+
|
81
|
+
res = http.request(req)
|
82
|
+
response_check(res, params)
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def encode_body(params)
|
88
|
+
raise NotImplementedError
|
89
|
+
end
|
90
|
+
|
91
|
+
def response_check(res, params)
|
92
|
+
if res.code != "200"
|
93
|
+
raise Error.new(res, params)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def filter_params(params)
|
98
|
+
params.dup.tap {|p| p['token'] = '[FILTERED]' if p['token'] }
|
99
|
+
end
|
100
|
+
|
101
|
+
# Required to implement to use #with_channels_create
|
102
|
+
# channels.create API is available from only Slack Web API
|
103
|
+
def api
|
104
|
+
raise NotImplementedError
|
105
|
+
end
|
106
|
+
|
107
|
+
def with_channels_create(params = {}, opts = {})
|
108
|
+
retries = 1
|
109
|
+
begin
|
110
|
+
yield
|
111
|
+
rescue ChannelNotFoundError => e
|
112
|
+
if params['token'] and opts['auto_channels_create']
|
113
|
+
log.warn "out_slack: channel \"#{params['channel']}\" is not found. try to create the channel, and then retry to post the message."
|
114
|
+
api.channels_create({name: params['channel'], token: params['token']})
|
115
|
+
retry if (retries -= 1) >= 0 # one time retry
|
116
|
+
else
|
117
|
+
raise e
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Slack client for Incoming Webhook
|
124
|
+
# https://api.slack.com/incoming-webhooks
|
125
|
+
class IncomingWebhook < Base
|
126
|
+
def initialize(endpoint, https_proxy = nil)
|
127
|
+
super
|
128
|
+
end
|
129
|
+
|
130
|
+
def post_message(params = {}, opts = {})
|
131
|
+
log.info { "out_slack: post_message #{params}" }
|
132
|
+
post(endpoint, params)
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def encode_body(params = {})
|
138
|
+
# https://api.slack.com/docs/formatting
|
139
|
+
params.to_json.gsub(/&/, '&').gsub(/</, '<').gsub(/>/, '>')
|
140
|
+
end
|
141
|
+
|
142
|
+
def response_check(res, params)
|
143
|
+
super
|
144
|
+
unless res.body == 'ok'
|
145
|
+
raise Error.new(res, params)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# Slack client for Slackbot Remote Control
|
151
|
+
# https://api.slack.com/slackbot
|
152
|
+
class Slackbot < Base
|
153
|
+
def initialize(endpoint, https_proxy = nil)
|
154
|
+
super
|
155
|
+
end
|
156
|
+
|
157
|
+
def api
|
158
|
+
@api ||= WebApi.new(nil, https_proxy)
|
159
|
+
end
|
160
|
+
|
161
|
+
def post_message(params = {}, opts = {})
|
162
|
+
raise ArgumentError, "channel parameter is required" unless params['channel']
|
163
|
+
with_channels_create(params, opts) do
|
164
|
+
log.info { "out_slack: post_message #{filter_params(params)}" }
|
165
|
+
post(slackbot_endpoint(params), params)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
|
171
|
+
def slackbot_endpoint(params)
|
172
|
+
endpoint.dup.tap {|e| e.query += "&channel=#{URI.encode(params['channel'])}" }
|
173
|
+
end
|
174
|
+
|
175
|
+
def encode_body(params = {})
|
176
|
+
return params['text']if params['text']
|
177
|
+
unless params['attachments']
|
178
|
+
raise ArgumentError, "params['text'] or params['attachments'] is required"
|
179
|
+
end
|
180
|
+
# handle params['attachments']
|
181
|
+
attachment = Array(params['attachments']).first # see only the first for now
|
182
|
+
# {
|
183
|
+
# attachments: [{
|
184
|
+
# text: "HERE",
|
185
|
+
# }]
|
186
|
+
# }
|
187
|
+
text = attachment['text']
|
188
|
+
# {
|
189
|
+
# attachments: [{
|
190
|
+
# fields: [{
|
191
|
+
# title: "title",
|
192
|
+
# value: "HERE",
|
193
|
+
# }]
|
194
|
+
# }]
|
195
|
+
# }
|
196
|
+
if text.nil? and attachment['fields']
|
197
|
+
text = Array(attachment['fields']).first['value'] # see only the first for now
|
198
|
+
end
|
199
|
+
text
|
200
|
+
end
|
201
|
+
|
202
|
+
def response_check(res, params)
|
203
|
+
if res.body == 'channel_not_found'
|
204
|
+
raise ChannelNotFoundError.new(res, params)
|
205
|
+
elsif res.body != 'ok'
|
206
|
+
raise Error.new(res, params)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Slack client for Web API
|
212
|
+
class WebApi < Base
|
213
|
+
DEFAULT_ENDPOINT = "https://slack.com/api/".freeze
|
214
|
+
|
215
|
+
def api
|
216
|
+
self
|
217
|
+
end
|
218
|
+
|
219
|
+
def endpoint
|
220
|
+
@endpoint ||= URI.parse(DEFAULT_ENDPOINT)
|
221
|
+
end
|
222
|
+
|
223
|
+
def post_message_endpoint
|
224
|
+
@post_message_endpoint ||= URI.join(endpoint, "chat.postMessage")
|
225
|
+
end
|
226
|
+
|
227
|
+
def channels_create_endpoint
|
228
|
+
@channels_create_endpoint ||= URI.join(endpoint, "channels.create")
|
229
|
+
end
|
230
|
+
|
231
|
+
# Sends a message to a channel.
|
232
|
+
#
|
233
|
+
# @see https://api.slack.com/methods/chat.postMessage
|
234
|
+
# @see https://github.com/slackhq/slack-api-docs/blob/master/methods/chat.postMessage.md
|
235
|
+
# @see https://github.com/slackhq/slack-api-docs/blob/master/methods/chat.postMessage.json
|
236
|
+
def post_message(params = {}, opts = {})
|
237
|
+
with_channels_create(params, opts) do
|
238
|
+
log.info { "out_slack: post_message #{filter_params(params)}" }
|
239
|
+
post(post_message_endpoint, params)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
# Creates a channel.
|
244
|
+
#
|
245
|
+
# NOTE: Bot user can not create a channel. Token must be issued by Normal User Account
|
246
|
+
# @see https://api.slack.com/bot-users
|
247
|
+
#
|
248
|
+
# @see https://api.slack.com/methods/channels.create
|
249
|
+
# @see https://github.com/slackhq/slack-api-docs/blob/master/methods/channels.create.md
|
250
|
+
# @see https://github.com/slackhq/slack-api-docs/blob/master/methods/channels.create.json
|
251
|
+
def channels_create(params = {}, opts = {})
|
252
|
+
log.info { "out_slack: channels_create #{filter_params(params)}" }
|
253
|
+
post(channels_create_endpoint, params)
|
254
|
+
end
|
255
|
+
|
256
|
+
private
|
257
|
+
|
258
|
+
def encode_body(params = {})
|
259
|
+
body = params.dup
|
260
|
+
if params['attachments']
|
261
|
+
body['attachments'] = params['attachments'].to_json
|
262
|
+
end
|
263
|
+
URI.encode_www_form(body)
|
264
|
+
end
|
265
|
+
|
266
|
+
def response_check(res, params)
|
267
|
+
super
|
268
|
+
res_params = JSON.parse(res.body)
|
269
|
+
return if res_params['ok']
|
270
|
+
case res_params['error']
|
271
|
+
when 'channel_not_found'
|
272
|
+
raise ChannelNotFoundError.new(res, params)
|
273
|
+
when 'name_taken'
|
274
|
+
raise NameTakenError.new(res, params)
|
275
|
+
else
|
276
|
+
raise Error.new(res, params)
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alerty-plugin-slack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Naotoshi Seo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: alerty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Slack plugin for alerty.
|
70
|
+
email:
|
71
|
+
- sonots@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- alerty-plugin-slack.gemspec
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
88
|
+
- example/slackbot.yml
|
89
|
+
- example/webhook.yml
|
90
|
+
- lib/alerty/plugin/slack.rb
|
91
|
+
- lib/alerty/plugin/slack/slack_client.rb
|
92
|
+
- lib/alerty/plugin/slack/version.rb
|
93
|
+
homepage: https://github.com/sonots/alerty-plugin-slack
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.5.1
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Slack plugin for alerty.
|
117
|
+
test_files: []
|