realtime-slackbot 0.0.2 → 0.1.0
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/slack/matchers/matcher.rb +70 -0
- data/lib/slack/matchers/matcher_group.rb +31 -0
- data/lib/slack/message.rb +9 -4
- data/lib/slack/slack.rb +17 -4
- metadata +4 -3
- data/lib/slack/matcher.rb +0 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c13855c42c7a8a3ff7a09ff6069b60016891ebf
|
4
|
+
data.tar.gz: ec4ba39af1236d7568f6718ee12b975ebd859e04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c57c00355b00b0b4d208152e7c6c2e8f846adf682158bc5ccda42d201c6f0883b21afd67a18bbfe0bbaab52524de4d0a2722d5c6ac44a981bf8d3c500e2c0e2
|
7
|
+
data.tar.gz: 887ab75eb513edddc1982698b2b366edabb73ee1e0e2830dece5ff39aac5c556042e34af3bde9ca53e1872cb2fdbcba780fed99dd4827fef280e3b4485358213
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class SlackBot::Matcher
|
2
|
+
def initialize
|
3
|
+
@tests = []
|
4
|
+
@finally = nil
|
5
|
+
end
|
6
|
+
|
7
|
+
def run_on(msg)
|
8
|
+
@tests.each do |test|
|
9
|
+
unless test.call(msg)
|
10
|
+
return false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
@finally.call(msg) if @finally
|
14
|
+
return true
|
15
|
+
end
|
16
|
+
|
17
|
+
def from?(user)
|
18
|
+
@tests << lambda do |msg|
|
19
|
+
user = user.to_s.downcase
|
20
|
+
msg_user = msg.user
|
21
|
+
return false unless msg_user
|
22
|
+
if msg_user.id == user || msg_user.name.downcase == user
|
23
|
+
return true
|
24
|
+
else
|
25
|
+
real = msg_user.real_name
|
26
|
+
return real == user || real.split(' ').map { |n| user == n }.any?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def in?(options={})
|
33
|
+
@tests << lambda do |msg|
|
34
|
+
if options[:id]
|
35
|
+
return msg.channel == options[:id]
|
36
|
+
elsif options[:name]
|
37
|
+
chan = msg.bot.channel(options[:name])
|
38
|
+
return chan['id'] == msg.channel
|
39
|
+
end
|
40
|
+
end
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def include?(text)
|
45
|
+
@tests << Proc.new do |msg|
|
46
|
+
msg.text.downcase.include? text
|
47
|
+
end
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def match?(reg)
|
52
|
+
@tests << Proc.new { |msg| msg.text.downcase.match reg }
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def try?(&block)
|
57
|
+
@tests << block
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def then(&block)
|
62
|
+
@finally = block
|
63
|
+
end
|
64
|
+
|
65
|
+
def then_reply(*args)
|
66
|
+
@finally = Proc.new do |msg|
|
67
|
+
msg.reply(*args)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'matcher'
|
2
|
+
|
3
|
+
module SlackBot
|
4
|
+
class MatcherGroup
|
5
|
+
def initialize(action)
|
6
|
+
@action = action
|
7
|
+
@matchers = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def respond_for(msg)
|
11
|
+
@before_match.call(msg) if @before_match
|
12
|
+
@matchers.each do |m|
|
13
|
+
break if m.run_on(msg)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def when
|
18
|
+
m = Matcher.new
|
19
|
+
@matchers.push m
|
20
|
+
return m
|
21
|
+
end
|
22
|
+
|
23
|
+
def before_match(&block)
|
24
|
+
@before_match = block
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
"#{@action} #{@matchers.count}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/slack/message.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'json'
|
2
|
-
require_relative 'matcher'
|
2
|
+
require_relative 'matchers/matcher'
|
3
3
|
|
4
4
|
module SlackBot
|
5
5
|
class Message
|
@@ -18,6 +18,10 @@ module SlackBot
|
|
18
18
|
def to_s
|
19
19
|
"#{@user.name}: #{@data['text']}"
|
20
20
|
end
|
21
|
+
|
22
|
+
def user
|
23
|
+
@bot.user(@data['user'])
|
24
|
+
end
|
21
25
|
|
22
26
|
def [](key)
|
23
27
|
@data[key]
|
@@ -28,10 +32,11 @@ module SlackBot
|
|
28
32
|
end
|
29
33
|
|
30
34
|
def method_missing(name, *args)
|
31
|
-
if
|
32
|
-
|
33
|
-
else
|
35
|
+
# Access data if no args and is valid key, else throw exception
|
36
|
+
if args.count == 0 && @data.has_key?(name.to_s)
|
34
37
|
@data[name.to_s]
|
38
|
+
else
|
39
|
+
super(name, args)
|
35
40
|
end
|
36
41
|
end
|
37
42
|
end
|
data/lib/slack/slack.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require_relative 'user'
|
2
2
|
require_relative 'message'
|
3
|
+
require_relative 'matchers/matcher_group'
|
3
4
|
require 'net/http'
|
4
5
|
require 'faye/websocket'
|
5
6
|
require 'eventmachine'
|
@@ -14,6 +15,7 @@ module SlackBot
|
|
14
15
|
def initialize(auth_key, options={})
|
15
16
|
@debug = options[:log]
|
16
17
|
@key = auth_key.strip
|
18
|
+
@matchers = Hash.new
|
17
19
|
end
|
18
20
|
|
19
21
|
def get_url
|
@@ -25,7 +27,7 @@ module SlackBot
|
|
25
27
|
|
26
28
|
def run
|
27
29
|
url = get_url()
|
28
|
-
EM.run
|
30
|
+
EM.run do
|
29
31
|
@socket = ws = Faye::WebSocket::Client.new(url)
|
30
32
|
|
31
33
|
ws.on :open do |event|
|
@@ -54,11 +56,18 @@ module SlackBot
|
|
54
56
|
@socket = ws = nil
|
55
57
|
hook(:closed)
|
56
58
|
end
|
57
|
-
|
59
|
+
end
|
58
60
|
end
|
59
61
|
|
60
62
|
def hook(action, *args)
|
61
|
-
if self.respond_to? action
|
63
|
+
if self.respond_to? "#{action}_matcher"
|
64
|
+
unless @matchers.has_key? action
|
65
|
+
matcher_group = MatcherGroup.new(action)
|
66
|
+
self.send("#{action}_matcher", matcher_group)
|
67
|
+
@matchers[action] = matcher_group
|
68
|
+
end
|
69
|
+
@matchers[action].respond_for(args.first)
|
70
|
+
elsif self.respond_to? action
|
62
71
|
begin
|
63
72
|
send(action, *args)
|
64
73
|
rescue Exception => e
|
@@ -71,7 +80,7 @@ module SlackBot
|
|
71
80
|
def post(channel, message)
|
72
81
|
data = {
|
73
82
|
id: 1,
|
74
|
-
type:
|
83
|
+
type: 'message',
|
75
84
|
channel: channel,
|
76
85
|
text: message
|
77
86
|
}
|
@@ -104,6 +113,10 @@ module SlackBot
|
|
104
113
|
@users[id]
|
105
114
|
end
|
106
115
|
|
116
|
+
def channel(name)
|
117
|
+
@team_info['channels'].select { |c| c['name'] == name }.first
|
118
|
+
end
|
119
|
+
|
107
120
|
def log(type, message)
|
108
121
|
if debug?
|
109
122
|
puts "#{type}: #{message}"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: realtime-slackbot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Will Richardson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|
@@ -46,7 +46,8 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- lib/realtime-slackbot.rb
|
48
48
|
- lib/slack/bot.rb
|
49
|
-
- lib/slack/matcher.rb
|
49
|
+
- lib/slack/matchers/matcher.rb
|
50
|
+
- lib/slack/matchers/matcher_group.rb
|
50
51
|
- lib/slack/message.rb
|
51
52
|
- lib/slack/slack.rb
|
52
53
|
- lib/slack/user.rb
|
data/lib/slack/matcher.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
class SlackBot::Matcher
|
2
|
-
def initialize(msg)
|
3
|
-
@message = msg
|
4
|
-
@passing = true
|
5
|
-
end
|
6
|
-
|
7
|
-
def from?(user)
|
8
|
-
return self unless @passing
|
9
|
-
user = user.to_s.downcase
|
10
|
-
if @user
|
11
|
-
if @user.id == user || @user.name.downcase == user
|
12
|
-
res = true
|
13
|
-
else
|
14
|
-
real = @user.real_name
|
15
|
-
res = real == user || real.split(' ').map { |n| user == n }.any?
|
16
|
-
end
|
17
|
-
else
|
18
|
-
res = false
|
19
|
-
end
|
20
|
-
@passing &&= res
|
21
|
-
self
|
22
|
-
end
|
23
|
-
|
24
|
-
def in?(options={})
|
25
|
-
return self unless @passing
|
26
|
-
res = false
|
27
|
-
if options[:id]
|
28
|
-
res = @message['channel'] == options[:id]
|
29
|
-
elsif options[:name]
|
30
|
-
chans = @bot.team_info['channels'].select { |name| ch[id] == channel }
|
31
|
-
res = chans.first == options[:name]
|
32
|
-
end
|
33
|
-
@passing &&= res
|
34
|
-
self
|
35
|
-
end
|
36
|
-
|
37
|
-
def include?(text)
|
38
|
-
return self unless @passing
|
39
|
-
@passing &&= @message['text'].downcase.include? text
|
40
|
-
self
|
41
|
-
end
|
42
|
-
|
43
|
-
def match?(reg)
|
44
|
-
return self unless @passing
|
45
|
-
@passing &&= @message['text'].downcase.match reg
|
46
|
-
self
|
47
|
-
end
|
48
|
-
|
49
|
-
def then(&block)
|
50
|
-
block.call if @passing
|
51
|
-
end
|
52
|
-
|
53
|
-
def then_reply(*args)
|
54
|
-
if @passing
|
55
|
-
@message.reply(*args)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|