boty 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26a97fbe857b6bd235700d9ccfc27873546108de
4
- data.tar.gz: d229decc0996d14056e398cc8e0afb2c8a1363ca
3
+ metadata.gz: 748d4a310415b259aa1f4bc59de28841bf526146
4
+ data.tar.gz: df5b63db2607e615c5ed70ed55f032de33abfefb
5
5
  SHA512:
6
- metadata.gz: 9822d6113cab1579edddfc4c162583c717de0ce960911ff718b9e8cfc1931adba10165e7762548727d76299a128c4d411406225712b1bb68605fc137129c69aa
7
- data.tar.gz: 9d6d41549b9638608265664eadd325fc9f2fa11cf2cb377f036c2415872e8a58decd0eb6777849ca6c290f62c851e294367c0de6b4733dd5758188501ceaec98
6
+ metadata.gz: 64b8378c295747a414308b0f360bad3606c74bad7648693947394fa91de4d5fc6155ab1b6075d69c40e0d366a03150eb8ddfa777f038de546540fe77747a530a
7
+ data.tar.gz: 849a9ca497b019181b738a8a6138d8c57e08be2c3e498d6d3e01d68f55f12b5770c3c7549266e9701e5ab8eeed4ca225c2d291e19492b881556bb8e921c9ca72
data/.gitignore CHANGED
@@ -13,3 +13,5 @@
13
13
  /tmp
14
14
 
15
15
  .env.local
16
+ pkg
17
+ .rspec
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boty (0.0.1)
4
+ boty (0.0.2)
5
5
  eventmachine
6
6
  faye-websocket
7
+ thor
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
@@ -12,6 +13,7 @@ GEM
12
13
  diff-lcs (1.2.5)
13
14
  dotenv (2.0.2)
14
15
  eventmachine (1.0.8)
16
+ fakefs (0.6.7)
15
17
  faye-websocket (0.10.1)
16
18
  eventmachine (>= 0.12.0)
17
19
  websocket-driver (>= 0.5.1)
@@ -44,6 +46,7 @@ DEPENDENCIES
44
46
  bundler (~> 1.10)
45
47
  byebug
46
48
  dotenv
49
+ fakefs
47
50
  foreman
48
51
  rake (~> 10.0)
49
52
  rspec
data/boty CHANGED
@@ -9,15 +9,4 @@ session.start do |bot|
9
9
  next if message.from? self
10
10
  say "Ohay <@#{message.user}>! Ainda não sei o que fazer aqui. Sorry."
11
11
  end
12
-
13
- bot.respond(/pug me/i) do
14
- response = JSON.parse Net::HTTP.get(URI "http://pugme.herokuapp.com/random")
15
- say "<#{response["pug"]}>"
16
- end
17
-
18
- bot.respond(/pug bomb( (\d+))?/i) do |message|
19
- count = message.match[2] || 5
20
- response = JSON.parse Net::HTTP.get(URI "http://pugme.herokuapp.com/bomb?count=#{count}")
21
- response["pugs"].each do |pug| say "<#{pug}>" end
22
- end
23
12
  end
data/boty.gemspec CHANGED
@@ -15,14 +15,15 @@ Gem::Specification.new do |spec|
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "bin"
19
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.required_ruby_version = "~>2.0"
23
23
 
24
24
  spec.add_runtime_dependency "eventmachine"
25
25
  spec.add_runtime_dependency "faye-websocket"
26
+ spec.add_runtime_dependency "thor"
26
27
 
27
28
  spec.add_development_dependency "bundler", "~> 1.10"
28
29
  spec.add_development_dependency "rake", "~> 10.0"
@@ -30,4 +31,5 @@ Gem::Specification.new do |spec|
30
31
  spec.add_development_dependency "byebug"
31
32
  spec.add_development_dependency "rspec"
32
33
  spec.add_development_dependency "dotenv"
34
+ spec.add_development_dependency "fakefs"
33
35
  end
data/exe/boty ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require "boty"
3
+ require "boty/cli"
4
+
5
+ Boty::CLI.start ARGV
data/lib/boty/bot.rb CHANGED
@@ -4,20 +4,14 @@ module Boty
4
4
  attr_reader :id, :name
5
5
 
6
6
  def initialize(bot_info, session)
7
- @raw_info = bot_info
8
- @id = bot_info["id"]
9
- @name = bot_info["name"]
7
+ @raw_info, @id, @name = bot_info, bot_info["id"], bot_info["name"]
10
8
  @verbose = false
11
9
  @events = {}
10
+ load_default_scripts
12
11
  end
13
12
 
14
13
  def event(data)
15
- type = data["type"].to_sym
16
- debug "bot specifc event[#{type}] arrived", data
17
- unless @events[type]
18
- debug "no action binded to #{type}"
19
- return
20
- end
14
+ return unless type = event_type(data)
21
15
 
22
16
  @events[type].each do |action|
23
17
  action.call data
@@ -41,7 +35,7 @@ module Boty
41
35
  on :message do |data|
42
36
  # ignores if the mentions was sent by the bot itself, avoid infinite
43
37
  # loops.
44
- next if data["user"] == self.id
38
+ next if message_from_bot_itself? data
45
39
  debug "message wasn't from bot itself, so continuing..."
46
40
  yield_message_if_matches(data, block) do
47
41
  has_mention?(data) && regex.match(data["text"])
@@ -50,7 +44,7 @@ module Boty
50
44
  end
51
45
 
52
46
  def say(message)
53
- channel = (@_message.channel if @_message) || "general"
47
+ channel = (@_message && @_message.channel) || "general"
54
48
  uri = URI("https://slack.com/api/chat.postMessage?" +
55
49
  "token=#{ENV['SLACK_BOT_API_TOKEN']}&as_user=true&channel=#{channel}&" +
56
50
  "text=#{URI.encode message}")
@@ -71,10 +65,36 @@ module Boty
71
65
  stuff.each do |printable| talker.call printable end
72
66
  end
73
67
 
68
+ def load_default_scripts
69
+ # TODO: guarantee that we not load the same file twice
70
+ Dir["script/**/*.rb"].each do |file|
71
+ instance_eval File.read(file), file, 1
72
+ end
73
+ default_scripts_path = File.expand_path("../../../script", __FILE__)
74
+ # also loads the default scripts:
75
+ Dir["#{default_scripts_path}/**/*.rb"].each do |file|
76
+ instance_eval File.read(file), file, 1
77
+ end
78
+ end
79
+
80
+ def event_type(data)
81
+ type = data["type"].to_sym
82
+ debug "bot specifc event[#{type}] arrived", data
83
+ unless @events[type]
84
+ debug "no action binded to #{type}"
85
+ return
86
+ end
87
+ type
88
+ end
89
+
74
90
  def has_mention?(data)
75
91
  /<@#{name}>|<@#{id}>/ =~ data["text"]
76
92
  end
77
93
 
94
+ def message_from_bot_itself?(data)
95
+ data["user"] == self.id
96
+ end
97
+
78
98
  def yield_message_if_matches(data, message_block)
79
99
  match = yield
80
100
  if match
data/lib/boty/cli.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "thor"
2
+
3
+ module Boty
4
+ class CLI < Thor
5
+ include Thor::Actions
6
+ class_option :verbose, type: :boolean
7
+
8
+ attr_reader :bot_name, :company, :api_key
9
+
10
+ source_root File.expand_path("../../../template", __FILE__)
11
+
12
+ desc "new [BOT_NAME]",
13
+ "Creates a BOT_NAME dir with your new bot structure."
14
+ def new(bot_name)
15
+ @bot_name = bot_name
16
+ acquire_bot_info bot_name
17
+ directory "project", bot_name, verbose: verbose
18
+ inside bot_name do
19
+ run "chmod +x bot"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def verbose
26
+ options[:verbose]
27
+ end
28
+
29
+ def acquire_bot_info(bot_name)
30
+ @company = ask("What is your Slack company name?") || "acme"
31
+ @api_key = ask("What is the #{@bot_name} api key?") || "omg-lol-bbq"
32
+ end
33
+ end
34
+ end
data/lib/boty/session.rb CHANGED
@@ -20,20 +20,12 @@ module Boty
20
20
  def start(&block)
21
21
  EM.run do
22
22
  login
23
- bot = Bot.new @slack_info["self"], self
24
- block.call bot if block_given?
25
- debug "bot is configured and ready to go!"
26
-
27
- debug "starting to listen on #{@session_url}"
28
- @ws = Faye::WebSocket::Client.new @session_url
29
-
30
- @ws.on :message do |event|
31
- debug "message arrived", event.data
32
- bot.event JSON.parse(event.data)
33
- end
23
+ bot = initialize_bot(&block)
34
24
 
35
- @ws.on :close do
36
- debug "bye bye"
25
+ stablish_connection do |ws|
26
+ ws.on :message do |event|
27
+ on_message event, bot
28
+ end
37
29
  end
38
30
  end
39
31
  end
@@ -54,7 +46,29 @@ module Boty
54
46
  @slack_info = JSON.parse Net::HTTP.get(URI @rtm_start_url)
55
47
  debug "yep! logged in!"
56
48
  @session_url = @slack_info["url"]
57
- @logged = true
49
+ end
50
+
51
+ def initialize_bot(&block)
52
+ Bot.new(@slack_info["self"], self).tap { |bot|
53
+ block.call bot if block_given?
54
+ debug "bot is configured and ready to go!"
55
+ }
56
+ end
57
+
58
+ def on_message(event, bot)
59
+ debug "message arrived", event.data
60
+ bot.event JSON.parse(event.data)
61
+ end
62
+
63
+ def on_close
64
+ debug "bye byeb."
65
+ end
66
+
67
+ def stablish_connection
68
+ debug "starting to listen on #{@session_url}"
69
+ ws = Faye::WebSocket::Client.new @session_url
70
+ ws.on :close do on_close end
71
+ yield ws if block_given?
58
72
  end
59
73
  end
60
74
  end
data/lib/boty/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Boty
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/script/pug.rb ADDED
@@ -0,0 +1,10 @@
1
+ respond(/pug me/i) do
2
+ response = JSON.parse Net::HTTP.get(URI "http://pugme.herokuapp.com/random")
3
+ say "<#{response["pug"]}>"
4
+ end
5
+
6
+ respond(/pug bomb( (\d+))?/i) do |message|
7
+ count = message.match[2] || 5
8
+ response = JSON.parse Net::HTTP.get(URI "http://pugme.herokuapp.com/bomb?count=#{count}")
9
+ response["pugs"].each do |pug| say "<#{pug}>" end
10
+ end
@@ -0,0 +1,6 @@
1
+ require "bundler"; Bundler.setup
2
+ if defined? Dotenv
3
+ Dotenv.load
4
+ Dotenv.overload ".env.local"
5
+ end
6
+ require "boty"
@@ -0,0 +1,2 @@
1
+ SLACK_COMPANY=<%= company %>
2
+ SLACK_BOT_API_TOKEN=<%= api_key %>
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "boty"
4
+ gem "dotenv"
@@ -0,0 +1 @@
1
+ <%=bot_name%>: bundle exec ./bot
File without changes
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require "./<%= bot_name %>"
3
+
4
+ session = Boty::Session.new
5
+ session.verbose = true
6
+ session.start do |bot|
7
+ bot.verbose = true
8
+ bot.message(/<%= bot_name %>/i) do |message|
9
+ next if message.from? self
10
+ say "Ohay <@#{message.user}>! I'm here, that's for sure."
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ respond(/ping/i) do
2
+ say "pong"
3
+ end
4
+
5
+ respond(/echo (.*)$/i) do |message|
6
+ say message.match[1]
7
+ end
8
+
9
+ respond(/time/i) do
10
+ say "reeepitaaa: #{Time.now}"
11
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Valeriano
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -122,19 +136,31 @@ dependencies:
122
136
  - - ">="
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: fakefs
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
125
153
  description: Boty is intendted to be a framework for construction of automated Slack
126
154
  Bots for your needs.
127
155
  email:
128
156
  - ricardo.valeriano@gmail.com
129
157
  executables:
130
- - console
131
- - setup
158
+ - boty
132
159
  extensions: []
133
160
  extra_rdoc_files: []
134
161
  files:
135
162
  - ".env"
136
163
  - ".gitignore"
137
- - ".rspec"
138
164
  - CODE_OF_CONDUCT.md
139
165
  - Gemfile
140
166
  - Gemfile.lock
@@ -146,18 +172,26 @@ files:
146
172
  - bin/setup
147
173
  - boty
148
174
  - boty.gemspec
149
- - lib/assets/.keep
175
+ - exe/boty
150
176
  - lib/boty.rb
151
177
  - lib/boty/bot.rb
178
+ - lib/boty/cli.rb
152
179
  - lib/boty/dsl.rb
153
180
  - lib/boty/message.rb
154
181
  - lib/boty/session.rb
155
182
  - lib/boty/version.rb
156
183
  - lib/tasks/.keep
157
184
  - log/.keep
158
- - script/boty/pug.rb
159
- - vendor/assets/javascripts/.keep
160
- - vendor/assets/stylesheets/.keep
185
+ - script/pug.rb
186
+ - template/project/%bot_name%.rb
187
+ - template/project/.env.local.tt
188
+ - template/project/Gemfile
189
+ - template/project/Procfile.tt
190
+ - template/project/README.md
191
+ - template/project/app/.empty
192
+ - template/project/bot.tt
193
+ - template/project/script/ping.rb
194
+ - template/project/spec/.empty
161
195
  homepage: http://github.com/ricardovaleriano/boty
162
196
  licenses:
163
197
  - MIT
@@ -183,4 +217,3 @@ signing_key:
183
217
  specification_version: 4
184
218
  summary: Boty is a pretty bot specially tailored for Slack.
185
219
  test_files: []
186
- has_rdoc:
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --color
2
- --format=documentation
3
- --require=spec_helper
data/script/boty/pug.rb DELETED
@@ -1,10 +0,0 @@
1
- respond(/pug me/i) do |jabber, message|
2
- response = JSON.parse Net::HTTP.get URI("http://pugme.herokuapp.com/random")
3
- jabber.say "<#{response["pug"]}>", message.channel
4
- end
5
-
6
- respond(/pug bomb( (\d+))?/i) do |jabber, message|
7
- count = message.match[2] || 5
8
- response = JSON.parse Net::HTTP.get URI("http://pugme.herokuapp.com/bomb?count=#{count}")
9
- response["pugs"].each do |pug| jabber.say "<#{pug}>", message.channel end
10
- end