boty 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +9 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +5 -4
- data/README.md +1 -0
- data/bin/bot +2 -1
- data/boty.gemspec +1 -2
- data/gemfiles/Gemfile.rbx +6 -0
- data/lib/boty/action.rb +6 -1
- data/lib/boty/bot.rb +10 -8
- data/lib/boty/locale.rb +11 -0
- data/lib/boty/script_dsl.rb +57 -5
- data/lib/boty/session.rb +4 -4
- data/lib/boty/version.rb +1 -1
- data/lib/boty.rb +2 -3
- data/script/knows.rb +2 -2
- data/script/pug.rb +5 -5
- data/script/remember.rb +8 -9
- data/template/project/bot.tt +11 -3
- data/template/project/locale/.empty +0 -0
- metadata +20 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7c783caaed0c590d532335aeea11bea43728cd9
|
4
|
+
data.tar.gz: 9771f725b49e5aca5f20e60c637afdfa5117ba97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3363a3588cc5ef5978bbb106752ffd158de63f04242e2b38093f3de8dc25b92873a3cdb22ca994bf5c800676d6ec94d62d6d4ecd61d2cbdb8cea895cb4bc650a
|
7
|
+
data.tar.gz: 48ab7f8d3be6eae365461c780f9aa0e9bbd333c48ccc33363a54ece2a55d66bdb498d3ac1e2a09e0b9612f440afffc70846806ff1ff4cecf5ef65f1ee4dda912
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
boty (0.0.
|
4
|
+
boty (0.0.15)
|
5
5
|
eventmachine
|
6
|
+
faraday
|
6
7
|
faye-websocket
|
7
8
|
i18n
|
8
9
|
thor
|
@@ -15,12 +16,13 @@ GEM
|
|
15
16
|
dotenv (2.0.2)
|
16
17
|
eventmachine (1.0.8)
|
17
18
|
fakefs (0.6.7)
|
19
|
+
faraday (0.9.2)
|
20
|
+
multipart-post (>= 1.2, < 3)
|
18
21
|
faye-websocket (0.10.1)
|
19
22
|
eventmachine (>= 0.12.0)
|
20
23
|
websocket-driver (>= 0.5.1)
|
21
|
-
foreman (0.78.0)
|
22
|
-
thor (~> 0.19.1)
|
23
24
|
i18n (0.7.0)
|
25
|
+
multipart-post (2.0.0)
|
24
26
|
rake (10.4.2)
|
25
27
|
rspec (3.4.0)
|
26
28
|
rspec-core (~> 3.4.0)
|
@@ -49,7 +51,6 @@ DEPENDENCIES
|
|
49
51
|
byebug
|
50
52
|
dotenv
|
51
53
|
fakefs
|
52
|
-
foreman
|
53
54
|
rake (~> 10.0)
|
54
55
|
rspec
|
55
56
|
|
data/README.md
CHANGED
data/bin/bot
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require "./lib/boty"
|
3
3
|
|
4
|
-
Boty.locale = ARGV.pop || :en
|
5
4
|
|
6
5
|
session = Boty::Session.new
|
7
6
|
session.start do |bot|
|
7
|
+
Boty.locale = ARGV.pop || :en
|
8
|
+
|
8
9
|
bot.desc I18n.t "template.presence", bot_name: bot.name
|
9
10
|
bot.message(/#{bot.name}/i) do |message|
|
10
11
|
next if message.from? self
|
data/boty.gemspec
CHANGED
@@ -25,11 +25,10 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_runtime_dependency "faye-websocket"
|
26
26
|
spec.add_runtime_dependency "thor"
|
27
27
|
spec.add_runtime_dependency "i18n"
|
28
|
+
spec.add_runtime_dependency "faraday"
|
28
29
|
|
29
30
|
spec.add_development_dependency "bundler", "~> 1.10"
|
30
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
-
spec.add_development_dependency "foreman"
|
32
|
-
spec.add_development_dependency "byebug"
|
33
32
|
spec.add_development_dependency "rspec"
|
34
33
|
spec.add_development_dependency "dotenv"
|
35
34
|
spec.add_development_dependency "fakefs"
|
data/lib/boty/action.rb
CHANGED
@@ -39,7 +39,12 @@ module Boty
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def execute(bot, message)
|
42
|
-
ScriptDSL.new(bot)
|
42
|
+
dsl = ScriptDSL.new(bot)
|
43
|
+
if match = message.match!(regex)
|
44
|
+
matches = Array(match)
|
45
|
+
matches.shift
|
46
|
+
dsl.instance_exec(*matches, &action)
|
47
|
+
end
|
43
48
|
rescue => e
|
44
49
|
logger.error e.message
|
45
50
|
raise e
|
data/lib/boty/bot.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
module Boty
|
2
2
|
class Bot
|
3
3
|
include Boty::Logger
|
4
|
+
attr_accessor :trigger_message
|
4
5
|
attr_reader :id, :name
|
5
6
|
|
6
7
|
def initialize(bot_info, session)
|
8
|
+
Locale.reload
|
7
9
|
@raw_info, @id, @name = bot_info, bot_info["id"], bot_info["name"]
|
8
10
|
@handlers ||= []
|
9
11
|
@commands ||= []
|
@@ -33,11 +35,11 @@ module Boty
|
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
def
|
38
|
+
def match(regex, &block)
|
37
39
|
@handlers << create_action(regex, &block)
|
38
40
|
end
|
39
41
|
|
40
|
-
def
|
42
|
+
def no_match(regex, &block)
|
41
43
|
@handlers.delete_if do |action|
|
42
44
|
action.is_this? regex, block
|
43
45
|
end
|
@@ -64,7 +66,7 @@ module Boty
|
|
64
66
|
end
|
65
67
|
|
66
68
|
def say(message, api_parameters = {})
|
67
|
-
channel = (@
|
69
|
+
channel = (@trigger_message && @trigger_message.channel) || "general"
|
68
70
|
options = { channel: channel }.merge api_parameters
|
69
71
|
post_response = Slack.chat.post_message message, options
|
70
72
|
logger.debug { "post response: #{post_response}" }
|
@@ -82,8 +84,8 @@ module Boty
|
|
82
84
|
end
|
83
85
|
|
84
86
|
def im(message)
|
85
|
-
logger.debug { "sending im messge to #{@
|
86
|
-
Slack.chat.post_im @
|
87
|
+
logger.debug { "sending im messge to #{@trigger_message.user.name}" }
|
88
|
+
Slack.chat.post_im @trigger_message.user.id, message
|
87
89
|
end
|
88
90
|
|
89
91
|
def brain
|
@@ -119,13 +121,13 @@ module Boty
|
|
119
121
|
|
120
122
|
def message_handler(data)
|
121
123
|
actions = has_valid_mention?(data) ? @commands : @handlers
|
122
|
-
@
|
124
|
+
@trigger_message = Message.new data
|
123
125
|
begin
|
124
126
|
Array(actions).each do |action|
|
125
|
-
action.execute self, @
|
127
|
+
action.execute self, @trigger_message
|
126
128
|
end
|
127
129
|
ensure
|
128
|
-
@
|
130
|
+
@trigger_message = nil
|
129
131
|
end
|
130
132
|
end
|
131
133
|
end
|
data/lib/boty/locale.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Boty
|
2
|
+
class Locale
|
3
|
+
def self.reload
|
4
|
+
default_locales_path = File.expand_path("../../../locale/**/*.yml", __FILE__)
|
5
|
+
locales = (Dir["locale/**/*.yml"] + Dir[default_locales_path]).
|
6
|
+
map { |file| File.expand_path file }
|
7
|
+
I18n.load_path = locales.uniq
|
8
|
+
I18n.available_locales = I18n::Backend::Simple.new.available_locales
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/boty/script_dsl.rb
CHANGED
@@ -2,23 +2,75 @@ require "forwardable"
|
|
2
2
|
|
3
3
|
module Boty
|
4
4
|
class ScriptDSL
|
5
|
-
attr_accessor :bot
|
6
|
-
|
7
5
|
INSTANCES = {}
|
8
6
|
private_constant :INSTANCES
|
9
7
|
class << self
|
10
8
|
alias original_constructor new
|
11
9
|
end
|
12
10
|
|
11
|
+
attr_accessor :bot
|
12
|
+
|
13
13
|
extend Forwardable
|
14
|
-
def_delegators :bot, :desc, :respond, :name, :
|
14
|
+
def_delegators :bot, :desc, :respond, :name, :brain, :know_how, :im, :say,
|
15
|
+
:match, :no_match, :no_respond, :no
|
16
|
+
def_delegators :message, :user, :channel
|
17
|
+
|
18
|
+
def self.new(bot)
|
19
|
+
INSTANCES[bot] ||= original_constructor bot
|
20
|
+
end
|
15
21
|
|
16
22
|
def initialize(bot)
|
17
23
|
@bot = bot
|
18
24
|
end
|
19
25
|
|
20
|
-
def
|
21
|
-
|
26
|
+
def message
|
27
|
+
@bot.trigger_message
|
28
|
+
end
|
29
|
+
|
30
|
+
def hear(*args, &block)
|
31
|
+
match(*args, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def command(*args, &block)
|
35
|
+
respond(*args, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def match_im(*args, &block)
|
39
|
+
command(*args, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def http
|
43
|
+
@http ||= HTTP.new
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class HTTP
|
48
|
+
[:get, :post, :put, :delete, :head, :patch, :options].each do |verb|
|
49
|
+
define_method verb do |url, params = {}|
|
50
|
+
response = connection verb, url, params
|
51
|
+
handle_response response
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def connection(verb, url, params)
|
58
|
+
uri = URI url
|
59
|
+
Faraday.new(url: "#{uri.scheme}://#{uri.host}") { |builder|
|
60
|
+
builder.adapter(*Faraday.default_adapter)
|
61
|
+
}.public_send(verb) { |req|
|
62
|
+
req.url uri.path, params
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def handle_response(response)
|
67
|
+
# TODO: use a response parser accordingly to the
|
68
|
+
body = response.body
|
69
|
+
if /application\/json/.match response.headers["Content-Type"]
|
70
|
+
JSON.parse body
|
71
|
+
else
|
72
|
+
body
|
73
|
+
end
|
22
74
|
end
|
23
75
|
end
|
24
76
|
end
|
data/lib/boty/session.rb
CHANGED
@@ -9,18 +9,18 @@ module Boty
|
|
9
9
|
def start(&block)
|
10
10
|
EM.run do
|
11
11
|
login
|
12
|
-
bot = initialize_bot(&block)
|
13
|
-
|
12
|
+
@bot = initialize_bot(&block)
|
14
13
|
stablish_connection do |ws|
|
15
14
|
ws.on :message do |event|
|
16
15
|
begin
|
17
|
-
on_message event, bot
|
16
|
+
on_message event, @bot
|
18
17
|
rescue StandardError => e
|
19
18
|
logger.error "Message #{event} could not be processed. #{e.message}"
|
20
19
|
end
|
21
20
|
end
|
22
21
|
end
|
23
22
|
end
|
23
|
+
self
|
24
24
|
end
|
25
25
|
|
26
26
|
private
|
@@ -34,7 +34,7 @@ module Boty
|
|
34
34
|
|
35
35
|
def initialize_bot(&block)
|
36
36
|
Bot.new(@slack_info["self"], self).tap { |bot|
|
37
|
-
block.call bot if block_given?
|
37
|
+
block.call ScriptDSL.new(bot) if block_given?
|
38
38
|
logger.debug { "bot is configured and ready to go!" }
|
39
39
|
}
|
40
40
|
end
|
data/lib/boty/version.rb
CHANGED
data/lib/boty.rb
CHANGED
@@ -10,15 +10,13 @@ rescue LoadError
|
|
10
10
|
end
|
11
11
|
|
12
12
|
require "faye/websocket"
|
13
|
-
|
14
13
|
require "i18n"
|
15
|
-
locales = Dir[File.expand_path("../../locale/**/*", __FILE__)]
|
16
|
-
I18n.load_path += locales
|
17
14
|
|
18
15
|
$:.unshift File.expand_path("../../lib", __FILE__)
|
19
16
|
|
20
17
|
module Boty
|
21
18
|
def self.locale=(lang)
|
19
|
+
Locale.reload
|
22
20
|
I18n.locale = lang
|
23
21
|
end
|
24
22
|
|
@@ -35,4 +33,5 @@ require "boty/message"
|
|
35
33
|
require "boty/action"
|
36
34
|
require "boty/script_loader"
|
37
35
|
require "boty/script_dsl"
|
36
|
+
require "boty/locale"
|
38
37
|
require "boty/bot"
|
data/script/knows.rb
CHANGED
data/script/pug.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
desc "pug me", I18n.t("scripts.pug_me")
|
2
|
-
|
3
|
-
response =
|
2
|
+
command(/pug me/i) do
|
3
|
+
response = http.get "http://pugme.herokuapp.com/random"
|
4
4
|
say "<#{response["pug"]}>"
|
5
5
|
end
|
6
6
|
|
7
7
|
desc "pug bomb X", I18n.t("scripts.pug_bomb")
|
8
|
-
|
9
|
-
count =
|
10
|
-
response =
|
8
|
+
command(/pug bomb( \d+)?/i) do |count|
|
9
|
+
count = (count || "5").strip
|
10
|
+
response = http.get "http://pugme.herokuapp.com/bomb", count: count
|
11
11
|
response["pugs"].each do |pug| say "<#{pug}>" end
|
12
12
|
end
|
data/script/remember.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
desc I18n.t("scripts.remember.command"),
|
2
2
|
I18n.t("scripts.remember.desc", name: name)
|
3
|
-
|
3
|
+
command(/remember(:?) (.+)/i) do |_, stuff|
|
4
4
|
brain[:user] ||= {}
|
5
|
-
brain[:user][
|
6
|
-
brain[:user][
|
5
|
+
brain[:user][user.name] ||= []
|
6
|
+
brain[:user][user.name] << stuff
|
7
7
|
im I18n.t("scripts.remember.response")
|
8
8
|
end
|
9
9
|
|
10
10
|
desc I18n.t("scripts.about.command"), I18n.t("scripts.about.desc")
|
11
|
-
|
12
|
-
user = message.match[3]
|
11
|
+
command(/about (\w+\b)(:?) (.+)/i) do |brain_area, _, user_about|
|
13
12
|
im I18n.t "scripts.about.response",
|
14
|
-
brain_area:
|
15
|
-
user_asking:
|
16
|
-
user_about:
|
17
|
-
brain[message.match[1].to_sym][
|
13
|
+
brain_area: brain_area,
|
14
|
+
user_asking: user.name,
|
15
|
+
user_about: user_about
|
16
|
+
brain[message.match[1].to_sym][user_about].each do |info| im info end
|
18
17
|
end
|
data/template/project/bot.tt
CHANGED
@@ -1,13 +1,21 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require "./<%= bot_name %>"
|
3
3
|
|
4
|
-
|
4
|
+
def set_locale(locale)
|
5
|
+
begin
|
6
|
+
Boty.locale = locale || :en
|
7
|
+
rescue I18n::InvalidLocale
|
8
|
+
Boty.locale = :en
|
9
|
+
end
|
10
|
+
end
|
5
11
|
|
6
12
|
session = Boty::Session.new
|
7
13
|
session.start do |bot|
|
14
|
+
set_locale ARGV.pop
|
15
|
+
|
8
16
|
bot.desc I18n.t "template.presence", bot_name: bot.name
|
9
|
-
bot.
|
17
|
+
bot.match(/#{bot.name}/i) do
|
10
18
|
next if message.from? self
|
11
|
-
say I18n.t "template.hello", user_name:
|
19
|
+
say I18n.t "template.hello", user_name: user.name
|
12
20
|
end
|
13
21
|
end
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: boty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Valeriano
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|
@@ -67,61 +67,47 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: faraday
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
76
|
-
type: :
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: bundler
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '10
|
89
|
+
version: '1.10'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '10
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: foreman
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
96
|
+
version: '1.10'
|
111
97
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
98
|
+
name: rake
|
113
99
|
requirement: !ruby/object:Gem::Requirement
|
114
100
|
requirements:
|
115
|
-
- - "
|
101
|
+
- - "~>"
|
116
102
|
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
103
|
+
version: '10.0'
|
118
104
|
type: :development
|
119
105
|
prerelease: false
|
120
106
|
version_requirements: !ruby/object:Gem::Requirement
|
121
107
|
requirements:
|
122
|
-
- - "
|
108
|
+
- - "~>"
|
123
109
|
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
110
|
+
version: '10.0'
|
125
111
|
- !ruby/object:Gem::Dependency
|
126
112
|
name: rspec
|
127
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -175,6 +161,7 @@ extra_rdoc_files: []
|
|
175
161
|
files:
|
176
162
|
- ".env"
|
177
163
|
- ".gitignore"
|
164
|
+
- ".travis.yml"
|
178
165
|
- CODE_OF_CONDUCT.md
|
179
166
|
- Gemfile
|
180
167
|
- Gemfile.lock
|
@@ -187,10 +174,12 @@ files:
|
|
187
174
|
- bin/setup
|
188
175
|
- boty.gemspec
|
189
176
|
- exe/boty
|
177
|
+
- gemfiles/Gemfile.rbx
|
190
178
|
- lib/boty.rb
|
191
179
|
- lib/boty/action.rb
|
192
180
|
- lib/boty/bot.rb
|
193
181
|
- lib/boty/cli.rb
|
182
|
+
- lib/boty/locale.rb
|
194
183
|
- lib/boty/logger.rb
|
195
184
|
- lib/boty/message.rb
|
196
185
|
- lib/boty/rspec.rb
|
@@ -221,6 +210,7 @@ files:
|
|
221
210
|
- template/project/Rakefile
|
222
211
|
- template/project/app/.empty
|
223
212
|
- template/project/bot.tt
|
213
|
+
- template/project/locale/.empty
|
224
214
|
- template/project/script/ping.rb
|
225
215
|
- template/project/spec/.empty
|
226
216
|
- template/project/spec/script/ping_spec.rb
|