slack-ruby-bot-bhe 0.5.5.3

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.
Files changed (90) hide show
  1. checksums.yaml +17 -0
  2. data/.gitignore +3 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +6 -0
  5. data/.rubocop_todo.yml +58 -0
  6. data/.travis.yml +3 -0
  7. data/CHANGELOG.md +81 -0
  8. data/CONTRIBUTING.md +139 -0
  9. data/DEPLOYMENT.md +33 -0
  10. data/Gemfile +3 -0
  11. data/LICENSE.md +22 -0
  12. data/README.md +263 -0
  13. data/RELEASING.md +67 -0
  14. data/Rakefile +19 -0
  15. data/TUTORIAL.md +205 -0
  16. data/UPGRADING.md +64 -0
  17. data/examples/minimal/Gemfile +3 -0
  18. data/examples/minimal/Procfile +1 -0
  19. data/examples/minimal/pongbot.rb +9 -0
  20. data/examples/weather/Gemfile +3 -0
  21. data/examples/weather/Procfile +1 -0
  22. data/examples/weather/weatherbot.rb +9 -0
  23. data/lib/config/application.rb +14 -0
  24. data/lib/config/boot.rb +8 -0
  25. data/lib/config/environment.rb +3 -0
  26. data/lib/initializers/giphy.rb +5 -0
  27. data/lib/slack-ruby-bot.rb +25 -0
  28. data/lib/slack-ruby-bot/about.rb +7 -0
  29. data/lib/slack-ruby-bot/app.rb +46 -0
  30. data/lib/slack-ruby-bot/bot.rb +7 -0
  31. data/lib/slack-ruby-bot/client.rb +44 -0
  32. data/lib/slack-ruby-bot/commands.rb +5 -0
  33. data/lib/slack-ruby-bot/commands/about.rb +12 -0
  34. data/lib/slack-ruby-bot/commands/base.rb +121 -0
  35. data/lib/slack-ruby-bot/commands/help.rb +9 -0
  36. data/lib/slack-ruby-bot/commands/hi.rb +9 -0
  37. data/lib/slack-ruby-bot/commands/unknown.rb +11 -0
  38. data/lib/slack-ruby-bot/config.rb +40 -0
  39. data/lib/slack-ruby-bot/hooks.rb +3 -0
  40. data/lib/slack-ruby-bot/hooks/base.rb +10 -0
  41. data/lib/slack-ruby-bot/hooks/hello.rb +11 -0
  42. data/lib/slack-ruby-bot/hooks/message.rb +50 -0
  43. data/lib/slack-ruby-bot/rspec.rb +12 -0
  44. data/lib/slack-ruby-bot/rspec/support/fixtures/slack/auth_test.yml +16 -0
  45. data/lib/slack-ruby-bot/rspec/support/fixtures/slack/migration_in_progress.yml +30 -0
  46. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/it_behaves_like_a_slack_bot.rb +27 -0
  47. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb +30 -0
  48. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +19 -0
  49. data/lib/slack-ruby-bot/rspec/support/slack_api_key.rb +5 -0
  50. data/lib/slack-ruby-bot/rspec/support/slack_ruby_bot_configure.rb +9 -0
  51. data/lib/slack-ruby-bot/rspec/support/vcr.rb +8 -0
  52. data/lib/slack-ruby-bot/server.rb +111 -0
  53. data/lib/slack-ruby-bot/version.rb +3 -0
  54. data/lib/slack_ruby_bot.rb +1 -0
  55. data/screenshots/aliases.gif +0 -0
  56. data/screenshots/demo.gif +0 -0
  57. data/screenshots/dms.gif +0 -0
  58. data/screenshots/register-bot.png +0 -0
  59. data/screenshots/weather.gif +0 -0
  60. data/slack-ruby-bot.gemspec +28 -0
  61. data/slack.png +0 -0
  62. data/spec/slack-ruby-bot/app_spec.rb +15 -0
  63. data/spec/slack-ruby-bot/commands/about_spec.rb +19 -0
  64. data/spec/slack-ruby-bot/commands/aliases_spec.rb +22 -0
  65. data/spec/slack-ruby-bot/commands/bot_spec.rb +19 -0
  66. data/spec/slack-ruby-bot/commands/commands_precedence_spec.rb +22 -0
  67. data/spec/slack-ruby-bot/commands/commands_regexp_escape_spec.rb +17 -0
  68. data/spec/slack-ruby-bot/commands/commands_spaces_spec.rb +20 -0
  69. data/spec/slack-ruby-bot/commands/commands_spec.rb +21 -0
  70. data/spec/slack-ruby-bot/commands/commands_with_block_spec.rb +23 -0
  71. data/spec/slack-ruby-bot/commands/direct_messages_spec.rb +38 -0
  72. data/spec/slack-ruby-bot/commands/empty_text_spec.rb +22 -0
  73. data/spec/slack-ruby-bot/commands/help_spec.rb +10 -0
  74. data/spec/slack-ruby-bot/commands/hi_spec.rb +19 -0
  75. data/spec/slack-ruby-bot/commands/match_spec.rb +17 -0
  76. data/spec/slack-ruby-bot/commands/message_loop_spec.rb +34 -0
  77. data/spec/slack-ruby-bot/commands/nil_message_spec.rb +22 -0
  78. data/spec/slack-ruby-bot/commands/not_implemented_spec.rb +15 -0
  79. data/spec/slack-ruby-bot/commands/operators_spec.rb +20 -0
  80. data/spec/slack-ruby-bot/commands/operators_with_block_spec.rb +23 -0
  81. data/spec/slack-ruby-bot/commands/send_gif_spec.rb +32 -0
  82. data/spec/slack-ruby-bot/commands/send_message_spec.rb +19 -0
  83. data/spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb +73 -0
  84. data/spec/slack-ruby-bot/commands/unknown_spec.rb +18 -0
  85. data/spec/slack-ruby-bot/config_spec.rb +55 -0
  86. data/spec/slack-ruby-bot/rspec/respond_with_error_spec.rb +19 -0
  87. data/spec/slack-ruby-bot/server_spec.rb +73 -0
  88. data/spec/slack-ruby-bot/version_spec.rb +7 -0
  89. data/spec/spec_helper.rb +1 -0
  90. metadata +313 -0
@@ -0,0 +1,67 @@
1
+ # Releasing Slack-Ruby-Bot
2
+
3
+ There're no particular rules about when to release slack-ruby-bot. Release bug fixes frequenty, features not so frequently and breaking API changes rarely.
4
+
5
+ ### Release
6
+
7
+ Run tests, check that all tests succeed locally.
8
+
9
+ ```
10
+ bundle install
11
+ rake
12
+ ```
13
+
14
+ Check that the last build succeeded in [Travis CI](https://travis-ci.org/dblock/slack-ruby-bot) for all supported platforms.
15
+
16
+ Increment the version, modify [lib/slack-ruby-bot/version.rb](lib/slack-ruby-bot/version.rb).
17
+
18
+ * Increment the third number if the release has bug fixes and/or very minor features, only (eg. change `0.2.1` to `0.2.2`).
19
+ * Increment the second number if the release contains major features or breaking API changes (eg. change `0.2.1` to `0.3.0`).
20
+
21
+ Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.
22
+
23
+ ```
24
+ ### 0.2.2 (7/10/2015)
25
+ ```
26
+
27
+ Remove the line with "Your contribution here.", since there will be no more contributions to this release.
28
+
29
+ Commit your changes.
30
+
31
+ ```
32
+ git add CHANGELOG.md lib/slack-ruby-bot/version.rb
33
+ git commit -m "Preparing for release, 0.2.2."
34
+ git push origin master
35
+ ```
36
+
37
+ Release.
38
+
39
+ ```
40
+ $ rake release
41
+
42
+ slack-ruby-bot 0.2.2 built to pkg/slack-ruby-bot-0.2.2.gem.
43
+ Tagged v0.2.2.
44
+ Pushed git commits and tags.
45
+ Pushed slack-ruby-bot 0.2.2 to rubygems.org.
46
+ ```
47
+
48
+ ### Prepare for the Next Version
49
+
50
+ Add the next release to [CHANGELOG.md](CHANGELOG.md).
51
+
52
+ ```
53
+ Next Release
54
+ ============
55
+
56
+ * Your contribution here.
57
+ ```
58
+
59
+ Increment the third version number in [lib/slack-ruby-bot/version.rb](lib/slack-ruby-bot/version.rb).
60
+
61
+ Comit your changes.
62
+
63
+ ```
64
+ git add CHANGELOG.md lib/slack-ruby-bot/version.rb
65
+ git commit -m "Preparing for next development iteration, 0.2.3."
66
+ git push origin master
67
+ ```
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'bundler/gem_tasks'
4
+
5
+ Bundler.setup :default, :development
6
+
7
+ unless ENV['RACK_ENV'] == 'production'
8
+ require 'rspec/core'
9
+ require 'rspec/core/rake_task'
10
+
11
+ RSpec::Core::RakeTask.new(:spec) do |spec|
12
+ spec.pattern = FileList['spec/**/*_spec.rb']
13
+ end
14
+
15
+ require 'rubocop/rake_task'
16
+ RuboCop::RakeTask.new
17
+
18
+ task default: [:rubocop, :spec]
19
+ end
@@ -0,0 +1,205 @@
1
+ ## Production Bot Tutorial
2
+
3
+ In this tutorial we'll implement [slack-mathbot](https://github.com/dblock/slack-mathbot).
4
+
5
+ ### Introduction
6
+
7
+ A typical production Slack bot is a combination of a vanilla web server and a websocket application that talks to the [Slack Real Time Messaging API](https://api.slack.com/rtm). The web server is optional, but most people will run their Slack bots on [Heroku](https://dashboard.heroku.com) in which case a web server is required to prevent Heroku from shutting the bot down. It also makes it convenient to develop a bot and test using `foreman`.
8
+
9
+ ### Getting Started
10
+
11
+ #### Gemfile
12
+
13
+ Create a `Gemfile` that uses [slack-ruby-bot](https://github.com/dblock/slack-ruby-bot), [sinatra](https://github.com/sinatra/sinatra) (a web framework) and [puma](https://github.com/puma/puma) (a web server). For development we'll also use [foreman](https://github.com/theforeman/foreman) and write tests with [rspec](https://github.com/rspec/rspec).
14
+
15
+ ```ruby
16
+ source 'http://rubygems.org'
17
+
18
+ gem 'slack-ruby-bot'
19
+ gem 'puma'
20
+ gem 'sinatra'
21
+
22
+ group :development, :test do
23
+ gem 'rake'
24
+ gem 'foreman'
25
+ end
26
+
27
+ group :test do
28
+ gem 'rspec'
29
+ gem 'rack-test'
30
+ end
31
+ ```
32
+
33
+ Run `bundle install` to get all the gems.
34
+
35
+ #### Application
36
+
37
+ Create a folder called `slack-mathbot` and inside of it create `app.rb`.
38
+
39
+ ```ruby
40
+ module SlackMathbot
41
+ class App < SlackRubyBot::App
42
+ end
43
+ end
44
+ ```
45
+
46
+ #### Commands
47
+
48
+ Create a folder called `slack-mathbot/commands` and inside of it create `calculate.rb`. For now this calculator will always return 4.
49
+
50
+ ```ruby
51
+ module SlackMathbot
52
+ module Commands
53
+ class Calculate < SlackRubyBot::Commands::Base
54
+ command 'calculate' do |client, data, _match|
55
+ send_message client, data.channel, '4'
56
+ end
57
+ end
58
+ end
59
+ end
60
+ ```
61
+
62
+ #### Require Everything
63
+
64
+ Create a `slack-mathbot.rb` at the root and require the above files.
65
+
66
+ ```ruby
67
+ require 'slack-ruby-bot'
68
+ require 'slack-mathbot/commands/calculate'
69
+ require 'slack-mathbot/app'
70
+ ```
71
+
72
+ #### Web Server
73
+
74
+ We will need to keep the bot alive on Heroku, so create `web.rb`.
75
+
76
+ ```ruby
77
+ require 'sinatra/base'
78
+
79
+ module SlackMathbot
80
+ class Web < Sinatra::Base
81
+ get '/' do
82
+ 'Math is good for you.'
83
+ end
84
+ end
85
+ end
86
+ ```
87
+
88
+ #### Config.ru
89
+
90
+ Tie all the pieces together in `config.ru` which creates a thread for the bot and runs the web server on the main thread.
91
+
92
+ ```ruby
93
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
94
+
95
+ require 'slack-mathbot'
96
+ require 'web'
97
+
98
+ Thread.abort_on_exception = true
99
+
100
+ Thread.new do
101
+ begin
102
+ SlackMathbot::App.instance.run
103
+ rescue Exception => e
104
+ STDERR.puts "ERROR: #{e}"
105
+ STDERR.puts e.backtrace
106
+ raise e
107
+ end
108
+ end
109
+
110
+ run SlackMathbot::Web
111
+ ```
112
+
113
+ ### Create a Bot User
114
+
115
+ In Slack administration create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot).
116
+
117
+ ![](screenshots/register-bot.png)
118
+
119
+ On the next screen, note the API token.
120
+
121
+ #### .env
122
+
123
+ Create a `.env` file with the API token from above and make sure to add it to `.gitignore`.
124
+
125
+ ```
126
+ SLACK_API_TOKEN=...
127
+ ```
128
+
129
+ ### Procfile
130
+
131
+ Create a `Procfile` which `foreman` will use when you run the `foreman start` command below.
132
+
133
+ ```
134
+ web: bundle exec puma -p $PORT
135
+ ```
136
+
137
+ ### Run the Bot
138
+
139
+ Run `foreman start`. Your bot should be running.
140
+
141
+ ```
142
+ 14:32:32 web.1 | Puma starting in single mode...
143
+ 14:32:32 web.1 | * Version 2.11.3 (ruby 2.1.6-p336), codename: Intrepid Squirrel
144
+ 14:32:32 web.1 | * Min threads: 0, max threads: 16
145
+ 14:32:32 web.1 | * Environment: development
146
+ 14:32:35 web.1 | * Listening on tcp://0.0.0.0:5000
147
+ 14:32:35 web.1 | Use Ctrl-C to stop
148
+ 14:32:36 web.1 | I, [2015-07-10T14:32:36.216663 #98948] INFO -- : Welcome 'mathbot' to the 'xyz' team at https://xyz.slack.com/.
149
+ 14:32:36 web.1 | I, [2015-07-10T14:32:36.766955 #98948] INFO -- : Successfully connected to https://xyz.slack.com/.
150
+ ```
151
+
152
+ ### Try
153
+
154
+ Invite the bot to a channel via `/invite [bot name]` and send it a `calculate` command with `[bot name] calculate 2+2`. It will respond with `4` from the code above.
155
+
156
+ ### Write Tests
157
+
158
+ #### Spec Helper
159
+
160
+ Create `spec/spec_helper.rb` that includes the bot files and shared RSpec support from slack-ruby-bot.
161
+
162
+ ```ruby
163
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
164
+
165
+ require 'slack-ruby-bot/rspec'
166
+ require 'slack-mathbot'
167
+ ```
168
+
169
+ #### Test the Bot Application
170
+
171
+ Create a test for the bot application itself in `spec/slack-mathbot/app_spec.rb`.
172
+
173
+ ```ruby
174
+ require 'spec_helper'
175
+
176
+ describe SlackMathbot::App do
177
+ def app
178
+ SlackMathbot::App.new
179
+ end
180
+ it_behaves_like 'a slack ruby bot'
181
+ end
182
+ ```
183
+
184
+ #### Test a Command
185
+
186
+ Create a test for the `calculate` command in `spec/slack-mathbot/commands/calculate_spec.rb`. The bot is addressed by its user name.
187
+
188
+ ```ruby
189
+ require 'spec_helper'
190
+
191
+ describe SlackMathbot::Commands::Calculate do
192
+ def app
193
+ SlackMathbot::App.new
194
+ end
195
+ it 'returns 4' do
196
+ expect(message: "#{SlackRubyBot.config.user} calculate 2+2", channel: 'channel').to respond_with_slack_message('4')
197
+ end
198
+ end
199
+ ```
200
+
201
+ See [lib/slack-ruby-bot/rspec/support/slack-ruby-bot](lib/slack-ruby-bot/rspec/support/slack-ruby-bot) for other shared RSpec behaviors.
202
+
203
+ ### Deploy
204
+
205
+ See [DEPLOYMENT](DEPLOYMENT.md) for how to deploy your bot to production.
@@ -0,0 +1,64 @@
1
+ Upgrading SlackRubyBot
2
+ ======================
3
+
4
+ ### Upgrading to >= 0.4.0
5
+
6
+ This version uses [slack-ruby-client](https://github.com/dblock/slack-ruby-client) instead of [slack-ruby-gem](https://github.com/aki017/slack-ruby-gem).
7
+
8
+ The command interface now takes a `client` parameter, which is the RealTime Messaging API instance. Add the new parameter to all `call` calls in classes that inherit from `SlackRubyBot::Commands::Base`.
9
+
10
+ Before:
11
+
12
+ ```ruby
13
+ def self.call(data, match)
14
+ ...
15
+ end
16
+ ```
17
+
18
+ After:
19
+
20
+ ```ruby
21
+ def self.call(client, data, match)
22
+ ...
23
+ end
24
+ ```
25
+
26
+ This also applies to `command`, `operator` and `match` blocks.
27
+
28
+ Before:
29
+
30
+ ```ruby
31
+ command 'ping' do |data, match|
32
+ ...
33
+ end
34
+ ```
35
+
36
+ After:
37
+
38
+ ```ruby
39
+ command 'ping' do |client, data, match|
40
+ ...
41
+ end
42
+ ```
43
+
44
+ You can now send messages directly via the RealTime Messaging API.
45
+
46
+ ```ruby
47
+ client.message text: 'text', channel: 'channel'
48
+ ```
49
+
50
+ Otherwise you must now pass the `client` parameter to `send_message` and `send_message_with_gif`.
51
+
52
+ ```ruby
53
+ def self.call(client, data, match)
54
+ send_message client, data.channel, 'hello'
55
+ end
56
+ ```
57
+
58
+ ```ruby
59
+ def self.call(client, data, match)
60
+ send_message_with_gif client, data.channel, 'hello', 'hi'
61
+ end
62
+ ```
63
+
64
+
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'slack-ruby-bot', path: '../..'
@@ -0,0 +1 @@
1
+ console: bundle exec ruby pongbot.rb
@@ -0,0 +1,9 @@
1
+ require 'slack-ruby-bot'
2
+
3
+ class Bot < SlackRubyBot::Bot
4
+ command 'ping' do |client, data, _match|
5
+ client.message text: 'pong', channel: data.channel
6
+ end
7
+ end
8
+
9
+ Bot.run
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'slack-ruby-bot', path: '../..'
@@ -0,0 +1 @@
1
+ console: bundle exec ruby weatherbot.rb
@@ -0,0 +1,9 @@
1
+ require 'slack-ruby-bot'
2
+
3
+ class WeatherBot < SlackRubyBot::Bot
4
+ match(/^How is the weather in (?<location>\w*)\?$/i) do |client, data, match|
5
+ send_message client, data.channel, "The weather in #{match[:location]} is nice."
6
+ end
7
+ end
8
+
9
+ WeatherBot.run
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'boot'
5
+
6
+ Bundler.require :default, ENV['RACK_ENV']
7
+
8
+ Dir[File.expand_path('../../initializers', __FILE__) + '/**/*.rb'].each do |file|
9
+ require file
10
+ end
11
+
12
+ require File.expand_path('../application', __FILE__)
13
+
14
+ require 'slack_ruby_bot'
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'logger'
4
+ require 'active_support'
5
+ require 'active_support/core_ext'
6
+ require 'hashie'
7
+ require 'slack'
8
+ require 'giphy'
@@ -0,0 +1,3 @@
1
+ ENV['RACK_ENV'] ||= 'development'
2
+
3
+ require File.expand_path('../application', __FILE__)
@@ -0,0 +1,5 @@
1
+ require 'giphy'
2
+
3
+ Giphy::Configuration.configure do |config|
4
+ config.api_key = ENV['GIPHY_API_KEY'] || 'dc6zaTOxFJmzC' # from https://github.com/Giphy/GiphyAPI
5
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../config/environment', __FILE__)
2
+
3
+ require 'slack-ruby-bot/version'
4
+ require 'slack-ruby-bot/about'
5
+ require 'slack-ruby-bot/config'
6
+ require 'slack-ruby-bot/hooks'
7
+
8
+ module SlackRubyBot
9
+ class << self
10
+ def configure
11
+ block_given? ? yield(Config) : Config
12
+ end
13
+
14
+ def config
15
+ Config
16
+ end
17
+ end
18
+ end
19
+
20
+ require 'slack-ruby-client'
21
+ require 'slack-ruby-bot/commands'
22
+ require 'slack-ruby-bot/client'
23
+ require 'slack-ruby-bot/server'
24
+ require 'slack-ruby-bot/app'
25
+ require 'slack-ruby-bot/bot'