simple_slack 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/simple_slack/botter.rb +114 -113
- data/lib/simple_slack/client.rb +19 -17
- data/lib/simple_slack/getter.rb +90 -90
- data/lib/simple_slack/poster.rb +51 -49
- data/lib/simple_slack/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13b40759d9819966ef20fb9327dbaf0cf1b89702
|
4
|
+
data.tar.gz: 91baadd84d1e5e3be391c356165d65ddb1557ab9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55e423c0b8a876d36fe36ecfafc210081a0e2e5f973ac19511c620eaff181fd45f8cc42ceabca3245a1ca6822d6441cfe99da83121325f840626fabd6d7ffb4c
|
7
|
+
data.tar.gz: bfb07c56c760a97ad2c22e06e7f8317a1382c86fc1d442baacf2de388cff8687d63ad50eef7790f28dbef228dc9ecd97405f6666ef116c9c840865b2d6432d9d
|
data/lib/simple_slack/botter.rb
CHANGED
@@ -1,137 +1,138 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module SimpleSlack
|
2
|
+
class Botter
|
3
|
+
def initialize(token, client)
|
4
|
+
Slack.configure {|c| c.token = token }
|
5
|
+
@r_client = Slack.realtime
|
6
|
+
@client = client
|
7
|
+
@channel, @text, @user = ["all"] * 3
|
8
|
+
@responce_channel, @responce_text, @responce_user = nil, nil, "slacker"
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
def set_condition(channel: nil, text: nil, user: nil)
|
12
|
+
@channel = set_condition_channel(channel) if channel
|
13
|
+
@user = set_condition_user(user) if user
|
14
|
+
@text = set_condition_text(text) if text
|
15
|
+
{ channel: @channel, user: @user, text: @text }
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
def set_responce(channel: nil , text: nil, user: nil)
|
19
|
+
@responce_channel = channel if channel
|
20
|
+
@responce_text = text if text
|
21
|
+
@responce_user = user if user
|
22
|
+
if block_given?
|
23
|
+
@responce_block = Proc.new(&yield)
|
24
|
+
end
|
25
|
+
{ channel: @responce_channel, user: @responce_user, text: @responce_text }
|
23
26
|
end
|
24
|
-
{ channel: @responce_channel, user: @responce_user, text: @responce_text }
|
25
|
-
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
def status
|
29
|
+
variables = instance_variables.map {|v| v.to_s }
|
30
|
+
variables.map {|v| { v.to_s => instance_variable_get(v) } }
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
def start
|
34
|
+
return p "not set params. need set_responce(channel: , text: , user: )" unless valid_params
|
35
|
+
@r_client.on :message do |data|
|
36
|
+
if fit_condition?(data)
|
37
|
+
send_responce(data)
|
38
|
+
end
|
37
39
|
end
|
40
|
+
puts "client start!"
|
41
|
+
@r_client.start
|
38
42
|
end
|
39
|
-
puts "client start!"
|
40
|
-
@r_client.start
|
41
|
-
end
|
42
43
|
|
43
|
-
|
44
|
+
private
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
46
|
+
def send_responce(data)
|
47
|
+
if @responce_block.nil?
|
48
|
+
@client.post.channel(to: @responce_channel, text: @responce_text, name: @responce_user)
|
49
|
+
else
|
50
|
+
responce = {}.tap do |res|
|
51
|
+
res[:user] = @client.get.user(data["user"])[:name] rescue "unknown"
|
52
|
+
res[:channel] = @client.get.channel(data["channel"])[:name] rescue "unknown"
|
53
|
+
res[:text] = data["text"]
|
54
|
+
end
|
55
|
+
@responce_block.call(data, responce)
|
53
56
|
end
|
54
|
-
@responce_block.call(data, responce)
|
55
57
|
end
|
56
|
-
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
def fit_condition?(data)
|
60
|
+
fit_params?(@channel, data["channel"]) && fit_params?(@user, data["user"]) && fit_params?(@text, data["text"])
|
61
|
+
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
63
|
+
def fit_params?(set_params, res_param)
|
64
|
+
return true if set_params == "all"
|
65
|
+
case set_params
|
66
|
+
when String
|
67
|
+
set_params == res_param
|
68
|
+
when Array
|
69
|
+
set_params.any? {|param| fit_params?(param, res_param) }
|
70
|
+
when Regexp
|
71
|
+
set_params =~ res_param
|
72
|
+
when Hash
|
73
|
+
set_params[:id] == res_param
|
74
|
+
else
|
75
|
+
false
|
76
|
+
end
|
75
77
|
end
|
76
|
-
end
|
77
78
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
79
|
+
def set_condition_channel(channel)
|
80
|
+
return nil if channel.nil?
|
81
|
+
return "all" if channel.to_s == "all" rescue false
|
82
|
+
case channel
|
83
|
+
when String
|
84
|
+
@client.get.channel(channel)
|
85
|
+
when Array
|
86
|
+
channel.map {|ch| @client.get.channel(ch.to_s) }
|
87
|
+
when Hash
|
88
|
+
channel
|
89
|
+
when Symbol
|
90
|
+
@client.get.channel(channel.to_s)
|
91
|
+
else
|
92
|
+
"error: invalid Object"
|
93
|
+
end
|
92
94
|
end
|
93
|
-
end
|
94
95
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
96
|
+
def set_condition_user(user)
|
97
|
+
return nil if user.nil?
|
98
|
+
return "all" if user.to_s == "all" rescue false
|
99
|
+
case user
|
100
|
+
when String
|
101
|
+
@client.get.user(user)
|
102
|
+
when Array
|
103
|
+
user.map {|u| set_condition_user(u) }
|
104
|
+
when Hash
|
105
|
+
user
|
106
|
+
when Symbol
|
107
|
+
@client.get.user(user.to_s)
|
108
|
+
else
|
109
|
+
"error: invalid Object"
|
110
|
+
end
|
109
111
|
end
|
110
|
-
end
|
111
112
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
113
|
+
def set_condition_text(text)
|
114
|
+
return nil if text.nil?
|
115
|
+
return "all" if text.to_s == "all" rescue false
|
116
|
+
case text
|
117
|
+
when String
|
118
|
+
text.gsub(/\p{blank}/,"\s").strip.split("\s")
|
119
|
+
when Array
|
120
|
+
text.map {|t| set_condition_text(t) }
|
121
|
+
when Regexp
|
122
|
+
text
|
123
|
+
else
|
124
|
+
text.to_s rescue nil
|
125
|
+
end
|
124
126
|
end
|
125
|
-
end
|
126
127
|
|
127
|
-
|
128
|
-
|
129
|
-
|
128
|
+
def set_responce_channel(channel)
|
129
|
+
set_condition_channel(channel)
|
130
|
+
end
|
130
131
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
132
|
+
def valid_params
|
133
|
+
return true if @responce_block
|
134
|
+
variables = ["@responce_channel", "@responce_text"]
|
135
|
+
variables.none? {|v| instance_variable_get(v).nil? }
|
136
|
+
end
|
135
137
|
end
|
136
|
-
|
137
138
|
end
|
data/lib/simple_slack/client.rb
CHANGED
@@ -4,26 +4,28 @@ require 'simple_slack/poster'
|
|
4
4
|
require 'simple_slack/botter'
|
5
5
|
require 'simple_slack/toggl'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
module SimpleSlack
|
8
|
+
class Client
|
9
|
+
def initialize(token = ENV['SLACK_API_TOKEN'])
|
10
|
+
@token = token
|
11
|
+
Slack.configure { |config| config.token = @token }
|
12
|
+
@slack = Slack.client
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
def get
|
16
|
+
@getter ||= SimpleSlack::Getter.new(@slack)
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
def post
|
20
|
+
@poster ||= SimpleSlack::Poster.new(@slack, self)
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def bot
|
24
|
+
@botter ||= SimpleSlack::Botter.new(@token, self)
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
def toggl(toggl_api_token = ENV['TOGGL_API_TOKEN'])
|
28
|
+
@toggl ||= SimpleSlack::Toggl.new(toggl_api_token, self)
|
29
|
+
end
|
28
30
|
end
|
29
31
|
end
|
data/lib/simple_slack/getter.rb
CHANGED
@@ -1,111 +1,111 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# use options for
|
7
|
-
# :is_channel, :creator, :members, :topic, :purpose, :num_members, etc
|
8
|
-
def channels(options = [])
|
9
|
-
channels = @slack.channels_list
|
10
|
-
channels["channels"].map do |channel|
|
11
|
-
add_params = options.empty? ? {} : options_to_hash(options, channel)
|
12
|
-
{ id: channel["id"], name: channel["name"] }.merge(add_params)
|
13
|
-
end.sort_by {|ch| ch[:name] }
|
14
|
-
end
|
15
|
-
|
16
|
-
def channel(key, options = [])
|
17
|
-
key.to_s =~ /\AC.{8}\Z/ ? key_id = key : key_name = key
|
18
|
-
channel_list = channels(options)
|
19
|
-
if key_id
|
20
|
-
channel_list.find{|ch| ch[:id] == key_id }
|
21
|
-
elsif key_name
|
22
|
-
channel_list.find{|ch| ch[:name] == key_name }
|
1
|
+
module SimpleSlack
|
2
|
+
class Getter
|
3
|
+
def initialize(slack)
|
4
|
+
@slack = slack
|
23
5
|
end
|
24
|
-
end
|
25
6
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
7
|
+
# use options for
|
8
|
+
# :is_channel, :creator, :members, :topic, :purpose, :num_members, etc
|
9
|
+
def channels(options = [])
|
10
|
+
channels = @slack.channels_list
|
11
|
+
channels["channels"].map do |channel|
|
12
|
+
add_params = options.empty? ? {} : options_to_hash(options, channel)
|
13
|
+
{ id: channel["id"], name: channel["name"] }.merge(add_params)
|
14
|
+
end.sort_by {|ch| ch[:name] }
|
15
|
+
end
|
35
16
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
17
|
+
def channel(key, options = [])
|
18
|
+
key.to_s =~ /\AC.{8}\Z/ ? key_id = key : key_name = key
|
19
|
+
channel_list = channels(options)
|
20
|
+
if key_id
|
21
|
+
channel_list.find{|ch| ch[:id] == key_id }
|
22
|
+
elsif key_name
|
23
|
+
channel_list.find{|ch| ch[:name] == key_name }
|
24
|
+
end
|
43
25
|
end
|
44
|
-
end
|
45
26
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
27
|
+
# use options for
|
28
|
+
# :real_name, :is_admin, :is_bot, etc...
|
29
|
+
def users(options = [])
|
30
|
+
users = @slack.users_list
|
31
|
+
users["members"].map do |user|
|
32
|
+
add_params = options.empty? ? {} : options_to_hash(options, user)
|
33
|
+
{ id: user["id"], name: user["name"] }.merge(add_params)
|
34
|
+
end.sort_by {|ch| ch[:name] }
|
53
35
|
end
|
54
|
-
end
|
55
36
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
37
|
+
def user(key, options = [])
|
38
|
+
key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
|
39
|
+
user_list = users(options)
|
40
|
+
if key_id
|
41
|
+
user_list.find{|user| user[:id] == key_id }
|
42
|
+
elsif key_name
|
43
|
+
user_list.find{|user| user[:name] == key_name }
|
44
|
+
end
|
63
45
|
end
|
64
|
-
end
|
65
46
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
user(im["user"])
|
75
|
-
end
|
76
|
-
add_params = options.empty? ? {} : options_to_hash(options, im)
|
77
|
-
{ id: im["id"], user: im_user }.merge(add_params)
|
47
|
+
# use options for
|
48
|
+
# :image_24, :image_32, :image_48, image_72, etc...
|
49
|
+
def images(options = [])
|
50
|
+
users = @slack.users_list
|
51
|
+
users["members"].map do |user|
|
52
|
+
add_params = options.empty? ? {} : options_to_hash(options, user["profile"])
|
53
|
+
{ id: user["id"], name: user["name"], image: user["profile"]["image_24"] }.merge(add_params)
|
54
|
+
end
|
78
55
|
end
|
79
|
-
end
|
80
56
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
57
|
+
def image(key, options = [])
|
58
|
+
key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
|
59
|
+
image_list = images(options)
|
60
|
+
if key_id
|
61
|
+
image_list.find{|user| user[:id] == key_id }
|
62
|
+
elsif key_name
|
63
|
+
image_list.find{|user| user[:name] == key_name }
|
64
|
+
end
|
88
65
|
end
|
89
|
-
end
|
90
66
|
|
91
|
-
|
92
|
-
|
93
|
-
|
67
|
+
# use options for
|
68
|
+
# :created, :is_im, :is_org_shared, :is_user_deleted
|
69
|
+
def ims(options = [])
|
70
|
+
im_list = @slack.im_list
|
71
|
+
im_list["ims"].map do |im|
|
72
|
+
im_user = if im["user"] == "USLACKBOT"
|
73
|
+
{ id: im["user"], name: "slackbot" }
|
74
|
+
else
|
75
|
+
user(im["user"])
|
76
|
+
end
|
77
|
+
add_params = options.empty? ? {} : options_to_hash(options, im)
|
78
|
+
{ id: im["id"], user: im_user }.merge(add_params)
|
79
|
+
end
|
80
|
+
end
|
94
81
|
|
95
|
-
|
96
|
-
|
97
|
-
|
82
|
+
def im(key, options = [])
|
83
|
+
key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
|
84
|
+
im_list = ims(options)
|
85
|
+
if key_id
|
86
|
+
im_list.find{|im| im[:user][:id] == key_id }
|
87
|
+
elsif key_name
|
88
|
+
im_list.find{|im| im[:user][:name] == key_name }
|
89
|
+
end
|
90
|
+
end
|
98
91
|
|
99
|
-
|
92
|
+
def chats
|
93
|
+
"yet"
|
94
|
+
end
|
100
95
|
|
101
|
-
|
102
|
-
|
103
|
-
options.each do |op|
|
104
|
-
marged_option.merge!({ op.to_sym => type[op.to_s] })
|
96
|
+
def chat
|
97
|
+
"yet"
|
105
98
|
end
|
106
99
|
|
107
|
-
|
108
|
-
end
|
100
|
+
private
|
109
101
|
|
102
|
+
def options_to_hash(options, type = {})
|
103
|
+
marged_option = {}
|
104
|
+
options.each do |op|
|
105
|
+
marged_option.merge!({ op.to_sym => type[op.to_s] })
|
106
|
+
end
|
110
107
|
|
108
|
+
marged_option
|
109
|
+
end
|
110
|
+
end
|
111
111
|
end
|
data/lib/simple_slack/poster.rb
CHANGED
@@ -1,67 +1,69 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module SimpleSlack
|
2
|
+
class Poster
|
3
|
+
def initialize(slack, simple_slack)
|
4
|
+
@slack = slack
|
5
|
+
@simple_slack = simple_slack
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def channels(to: , text: , name: "slacker", **options)
|
9
|
+
to.all? do |t|
|
10
|
+
id = convert_channel_to_id(t.to_s)
|
11
|
+
send_chat({ username: name, channel: id, text: text }.merge(options))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def channel(to: , text: , name: "slacker", **options)
|
16
|
+
id = convert_channel_to_id(to.to_s)
|
10
17
|
send_chat({ username: name, channel: id, text: text }.merge(options))
|
11
18
|
end
|
12
|
-
end
|
13
19
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
20
|
+
def user(to: , text: , name: "slacker")
|
21
|
+
"yet"
|
22
|
+
end
|
18
23
|
|
19
|
-
|
20
|
-
|
21
|
-
|
24
|
+
def chat(channel: nil, user: nil, text: , name: "slacker", **options)
|
25
|
+
if channel
|
26
|
+
self.channel({ to: channel, text: text, name: name }.merge(options))
|
27
|
+
elsif user
|
28
|
+
"yet"
|
29
|
+
end
|
30
|
+
end
|
22
31
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
32
|
+
def ims(to: , text: , name: "slacker", **options)
|
33
|
+
to.all? do |t|
|
34
|
+
id = convert_im_to_id(t.to_s)
|
35
|
+
send_chat({ username: name, channel: id, text: text }.merge(options))
|
36
|
+
end
|
28
37
|
end
|
29
|
-
end
|
30
38
|
|
31
|
-
|
32
|
-
|
33
|
-
id = convert_im_to_id(t.to_s)
|
39
|
+
def im(to: , text: , name: "slacker", **options)
|
40
|
+
id = convert_im_to_id(to.to_s)
|
34
41
|
send_chat({ username: name, channel: id, text: text }.merge(options))
|
35
42
|
end
|
36
|
-
end
|
37
43
|
|
38
|
-
|
39
|
-
id = convert_im_to_id(to.to_s)
|
40
|
-
send_chat({ username: name, channel: id, text: text }.merge(options))
|
41
|
-
end
|
44
|
+
private
|
42
45
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
result["ok"]
|
48
|
-
end
|
46
|
+
def send_chat(username: , channel: , text: , icon_emoji: ":ghost:", **options)
|
47
|
+
result = @slack.chat_postMessage({ username: username, channel: channel, text: text, icon_emoji: icon_emoji }.merge(options))
|
48
|
+
result["ok"]
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
def convert_channel_to_id(key)
|
52
|
+
channel = @simple_slack.get.channel(key)
|
53
|
+
if channel
|
54
|
+
channel[:id]
|
55
|
+
else
|
56
|
+
raise "チャンネルが見つかりませんでした。"
|
57
|
+
end
|
56
58
|
end
|
57
|
-
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
def convert_im_to_id(key)
|
61
|
+
im = @simple_slack.get.im(key)
|
62
|
+
if im
|
63
|
+
im[:id]
|
64
|
+
else
|
65
|
+
raise "IMが見つかりませんでした。"
|
66
|
+
end
|
65
67
|
end
|
66
68
|
end
|
67
69
|
end
|
data/lib/simple_slack/version.rb
CHANGED