slack-ruby-bot-bhe 0.5.5.3

Sign up to get free protection for your applications and to get access to all the features.
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,17 @@
1
+ ---
2
+ !binary "U0hBMjU2":
3
+ metadata.gz: !binary |-
4
+ ZmQyODViYmUwYjUyYmNiNzVlNzNlZGZhMmVjNGY1ZmEzZmJmNTZhNmU0MGY5
5
+ NjdhYjBlODIxOGNkZmEwN2U1MA==
6
+ data.tar.gz: !binary |-
7
+ OGZmZWRjYmRlNzVjZTVkMzBhMTU2MGZmMWUzYzA1MzY5YmU4ZThjMDJiMjg2
8
+ MjBmZTcyYjJkZDRhZmRiNGFkYw==
9
+ SHA512:
10
+ metadata.gz: !binary |-
11
+ ZjVhYzc0MDBjMWU5ZjQxMTM5Yjk3MDM4NDdiNjIzY2YyOTU2ODFmOTY5ZDBj
12
+ ZDI3ZmFiOWI2MTdiOTMyZTVhZGQ1MmU2MmU5ZTFmZGExN2M4MjE3MTJmNGM5
13
+ ZjBiNzU4MzVjYjljMzE0NDYzMGZjNTFkNWRmMDA3MmE3M2ViMDI=
14
+ data.tar.gz: !binary |-
15
+ OTEyOWQ3NzQ2MjAyODdmZjBlZWU1ODE1MjA0MmU0M2UyODllN2VjNGRhYjhj
16
+ YWRiYTJiYTJhOTQ2ZTY3MmQ4MmNiZGFjMzY2NGRmY2YyZjI1YTI5MDI4NjJk
17
+ MmY5YjE1NTg3ODYzYzg3NDNmZWE5NzA3N2JkYjNkNTljZThiODY=
@@ -0,0 +1,3 @@
1
+ .env
2
+ pkg
3
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+ - bin/**/*
5
+
6
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,58 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2016-01-03 14:51:05 -0700 using RuboCop version 0.32.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 1
9
+ Lint/HandleExceptions:
10
+ Enabled: false
11
+
12
+ # Offense count: 1
13
+ Lint/UselessAccessModifier:
14
+ Enabled: false
15
+
16
+ # Offense count: 8
17
+ Metrics/AbcSize:
18
+ Max: 28
19
+
20
+ # Offense count: 1
21
+ # Configuration parameters: CountComments.
22
+ Metrics/ClassLength:
23
+ Max: 101
24
+
25
+ # Offense count: 3
26
+ Metrics/CyclomaticComplexity:
27
+ Max: 8
28
+
29
+ # Offense count: 104
30
+ # Configuration parameters: AllowURI, URISchemes.
31
+ Metrics/LineLength:
32
+ Max: 145
33
+
34
+ # Offense count: 6
35
+ # Configuration parameters: CountComments.
36
+ Metrics/MethodLength:
37
+ Max: 22
38
+
39
+ # Offense count: 2
40
+ Metrics/PerceivedComplexity:
41
+ Max: 8
42
+
43
+ # Offense count: 17
44
+ Style/Documentation:
45
+ Enabled: false
46
+
47
+ # Offense count: 2
48
+ Style/DoubleNegation:
49
+ Enabled: false
50
+
51
+ # Offense count: 1
52
+ # Configuration parameters: Exclude.
53
+ Style/FileName:
54
+ Enabled: false
55
+
56
+ # Offense count: 1
57
+ Style/ModuleFunction:
58
+ Enabled: false
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 2.1.6
3
+
@@ -0,0 +1,81 @@
1
+ ### 0.5.5 (1/4/2016)
2
+
3
+ * Added `SlackRubyBot::Bot` DSL sugar - [@dblock](https://github.com/dblock).
4
+
5
+ ### 0.5.4 (1/3/2016)
6
+
7
+ * Enable setting `send_gifs` per instance of `SlackRubyBot::Server` - [@dblock](https://github.com/dblock).
8
+
9
+ ### 0.5.3 (12/28/2015)
10
+
11
+ * [#36](https://github.com/dblock/slack-ruby-bot/issues/36): Fix: non-English bot aliases now work - [@dblock](https://github.com/dblock).
12
+
13
+ ### 0.5.2 (12/26/2015)
14
+
15
+ * Enable setting bot aliases per instance of `SlackRubyBot::Server` - [@dblock](https://github.com/dblock).
16
+
17
+ ### 0.5.1 (12/23/2015)
18
+
19
+ * Fix: restart sync vs. async - [@dblock](https://github.com/dblock).
20
+ * [#33](https://github.com/dblock/slack-ruby-bot/pull/33): `SlackRubyBot::App.instance` now creates an instance of the class on which it is called - [@dmvt](https://github.com/dmvt).
21
+
22
+ ### 0.5.0 (12/7/2015)
23
+
24
+ * Disable animated GIFs via `SlackRubyBot::Config.send_gifs` or ENV['SLACK_RUBY_BOT_SEND_GIFS'] - [@dblock](https://github.com/dblock).
25
+ * `SlackRubyBot::Server` supports `restart!` with retry - [@dblock](https://github.com/dblock).
26
+ * `SlackRubyBot::Server` publicly supports `auth!`, `start!` and `start_async` that make up a `run` loop - [@dblock](https://github.com/dblock).
27
+ * Extracted `SlackRubyBot::Server` from `SlackRubyBot::App` - [@dblock](https://github.com/dblock).
28
+ * Fix: explicitly require 'giphy' - [@dblock](https://github.com/dblock).
29
+ * Fix: undefined method `stop` for `Slack::RealTime::Client` - [@dblock](https://github.com/dblock).
30
+ * [#29](https://github.com/dblock/slack-ruby-bot/pull/29): Fixed bot failing to correctly respond to unknown commands when queried with format `@botname` - [@crayment](https://github.com/crayment).
31
+ * [#30](https://github.com/dblock/slack-ruby-bot/pull/30): Fix RegexpError when parsing command - [@kuboshizuma](https://github.com/kuboshizuma).
32
+
33
+ ### 0.4.5 (10/29/2015)
34
+
35
+ * [#23](https://github.com/dblock/slack-ruby-bot/pull/23): Fixed `match` that forced bot name into the expression being evaluated - [@dblock](https://github.com/dblock).
36
+ * [#22](https://github.com/dblock/slack-ruby-bot/issues/22), [slack-ruby-client#17](https://github.com/dblock/slack-ruby-client/issues/17): Do not respond to messages from self, override with `allow_message_loops` - [@dblock](https://github.com/dblock).
37
+
38
+ ### 0.4.4 (10/5/2015)
39
+
40
+ * [#17](https://github.com/dblock/slack-ruby-bot/issues/17): Address bot by `name:` - [@dblock](https://github.com/dblock).
41
+ * [#19](https://github.com/dblock/slack-ruby-bot/issues/19): Retry on `Faraday::Error::TimeoutError`, `TimeoutError` and `SSLError` - [@dblock](https://github.com/dblock).
42
+ * [#3](https://github.com/dblock/slack-ruby-bot/issues/3): Retry on `migration_in_progress` errors during `rtm.start` - [@dblock](https://github.com/dblock).
43
+ * Respond to direct messages without being addressed by name - [@dblock](https://github.com/dblock).
44
+ * Added `send_gif`, to allow GIFs to be sent without text - [@maclover7](https://github.com/maclover7).
45
+
46
+ ### 0.4.3 (8/21/2015)
47
+
48
+ * [#13](https://github.com/dblock/slack-ruby-bot/issues/13): You can now address the bot by its Slack @id - [@dblock](https://github.com/dblock).
49
+
50
+ ### 0.4.2 (8/20/2015)
51
+
52
+ * [#12](https://github.com/dblock/slack-ruby-bot/issues/12): Added support for bot aliases - [@dblock](https://github.com/dblock).
53
+
54
+ ### 0.4.1 (7/25/2015)
55
+
56
+ * Use a real client in `respond_with_slack_message` expectaions - [@dblock](https://github.com/dblock).
57
+
58
+ ### 0.4.0 (7/25/2015)
59
+
60
+ * Using [slack-ruby-client](https://github.com/dblock/slack-ruby-client) - [@dblock](https://github.com/dblock).
61
+ * Use RealTime API to post messages - [@dblock](https://github.com/dblock).
62
+
63
+ ### 0.3.1 (7/21/2015)
64
+
65
+ * [#8](https://github.com/dblock/slack-ruby-bot/issues/8): Fix: `undefined method 'strip!' for nil:NilClass` on nil message - [@dblock](https://github.com/dblock).
66
+
67
+ ### 0.3.0 (7/19/2015)
68
+
69
+ * [#5](https://github.com/dblock/slack-ruby-bot/issues/5): Added support for free-formed routes via `match` - [@dblock](https://github.com/dblock).
70
+ * [#6](https://github.com/dblock/slack-ruby-bot/issues/6): Commands and operators take blocks - [@dblock](https://github.com/dblock).
71
+ * [#4](https://github.com/dblock/slack-ruby-bot/issues/4): Messages are posted with `as_user: true` by default - [@dblock](https://github.com/dblock).
72
+
73
+ ### 0.2.0 (7/10/2015)
74
+
75
+ * Sending `send_message` with nil or empty text will yield `Nothing to see here.` with a GIF instead of `no_text` - [@dblock](https://github.com/dblock).
76
+ * Added support for operators with `operator [name]` - [@dblock](https://github.com/dblock).
77
+ * Added support for custom commands with `command [name]` - [@dblock](https://github.com/dblock).
78
+
79
+ ### 0.1.0 (6/2/2015)
80
+
81
+ * Initial public release - [@dblock](https://github.com/dblock).
@@ -0,0 +1,139 @@
1
+ # Contributing to SlackRubyBot
2
+
3
+ This project is work of [many contributors](https://github.com/dblock/slack-ruby-bot/graphs/contributors).
4
+
5
+ You're encouraged to submit [pull requests](https://github.com/dblock/slack-ruby-bot/pulls), [propose features and discuss issues](https://github.com/dblock/slack-ruby-bot/issues).
6
+
7
+ In the examples below, substitute your Github username for `contributor` in URLs.
8
+
9
+ ## Fork the Project
10
+
11
+ Fork the [project on Github](https://github.com/dblock/slack-ruby-bot) and check out your copy.
12
+
13
+ ```
14
+ git clone https://github.com/contributor/slack-ruby-bot.git
15
+ cd slack-ruby-bot
16
+ git remote add upstream https://github.com/dblock/slack-ruby-bot.git
17
+ ```
18
+
19
+ ## Bundle Install and Test
20
+
21
+ Ensure that you can build the project and run tests.
22
+
23
+ ```
24
+ bundle install
25
+ bundle exec rake
26
+ ```
27
+
28
+ ## Run SlackRubyBot in Development
29
+
30
+ Create a private slack group for yourself.
31
+
32
+ Create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot).
33
+
34
+ ![](screenshots/register-bot.png)
35
+
36
+ On the next screen, note the API token.
37
+
38
+ Run `SLACK_API_TOKEN=<your API token> foreman start`.
39
+
40
+ You can also create a `.env` file with `SLACK_API_TOKEN=<your API token>` and just run `foreman start`.
41
+
42
+ ## Create a Topic Branch
43
+
44
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
45
+
46
+ ```
47
+ git checkout master
48
+ git pull upstream master
49
+ git checkout -b my-feature-branch
50
+ ```
51
+
52
+ ## Write Tests
53
+
54
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build.
55
+ Add to [spec](spec).
56
+
57
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
58
+
59
+ ## Write Code
60
+
61
+ Implement your feature or bug fix.
62
+
63
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop).
64
+ Run `bundle exec rubocop` and fix any style issues highlighted.
65
+
66
+ Make sure that `bundle exec rake` completes without errors.
67
+
68
+ ## Write Documentation
69
+
70
+ Document any external behavior in the [README](README.md).
71
+
72
+ ## Update Changelog
73
+
74
+ Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*.
75
+ Make it look like every other line, including your name and link to your Github account.
76
+
77
+ ## Commit Changes
78
+
79
+ Make sure git knows your name and email address:
80
+
81
+ ```
82
+ git config --global user.name "Your Name"
83
+ git config --global user.email "contributor@example.com"
84
+ ```
85
+
86
+ Writing good commit logs is important. A commit log should describe what changed and why.
87
+
88
+ ```
89
+ git add ...
90
+ git commit
91
+ ```
92
+
93
+ ## Push
94
+
95
+ ```
96
+ git push origin my-feature-branch
97
+ ```
98
+
99
+ ## Make a Pull Request
100
+
101
+ Go to https://github.com/contributor/slack-ruby-bot and select your feature branch.
102
+ Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
103
+
104
+ ## Rebase
105
+
106
+ If you've been working on a change for a while, rebase with upstream/master.
107
+
108
+ ```
109
+ git fetch upstream
110
+ git rebase upstream/master
111
+ git push origin my-feature-branch -f
112
+ ```
113
+
114
+ ## Update CHANGELOG Again
115
+
116
+ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
117
+
118
+ ```
119
+ * [#123](https://github.com/dblock/slack-ruby-bot/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
120
+ ```
121
+
122
+ Amend your previous commit and force push the changes.
123
+
124
+ ```
125
+ git commit --amend
126
+ git push origin my-feature-branch -f
127
+ ```
128
+
129
+ ## Check on Your Pull Request
130
+
131
+ Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
132
+
133
+ ## Be Patient
134
+
135
+ It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
136
+
137
+ ## Thank You
138
+
139
+ Please do know that we really appreciate and value your time and work. We love you, really.
@@ -0,0 +1,33 @@
1
+ ## Installation
2
+
3
+ Create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot).
4
+
5
+ ![](screenshots/register-bot.png)
6
+
7
+ On the next screen, note the API token.
8
+
9
+ ### Environment
10
+
11
+ #### SLACK_API_TOKEN
12
+
13
+ Set SLACK_API_TOKEN from the Bot integration settings on Slack.
14
+
15
+ ```
16
+ heroku config:add SLACK_API_TOKEN=...
17
+ ```
18
+
19
+ #### GIPHY_API_KEY
20
+
21
+ The bot replies with animated GIFs. While it's currently not necessary, you may need to set GIPHY_API_KEY in the future, see [github.com/Giphy/GiphyAPI](https://github.com/Giphy/GiphyAPI) for details.
22
+
23
+ #### SLACK_RUBY_BOT_ALIASES
24
+
25
+ Optional names for this bot.
26
+
27
+ ```
28
+ heroku config:add SLACK_RUBY_BOT_ALIASES=":pong: table-tennis ping-pong"
29
+ ```
30
+
31
+ ### Heroku Idling
32
+
33
+ Heroku free tier applications will idle. Either pay 7$ a month for the hobby dyno or use [UptimeRobot](http://uptimerobot.com) or similar to prevent your instance from sleeping or pay for a production dyno.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2015-2016 Daniel Doubrovkine, Artsy and Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,263 @@
1
+ Slack-Ruby-Bot
2
+ ==============
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/slack-ruby-bot.svg)](http://badge.fury.io/rb/slack-ruby-bot)
5
+ [![Build Status](https://travis-ci.org/dblock/slack-ruby-bot.svg)](https://travis-ci.org/dblock/slack-ruby-bot)
6
+ [![Code Climate](https://codeclimate.com/github/dblock/slack-ruby-bot/badges/gpa.svg)](https://codeclimate.com/github/dblock/slack-ruby-bot)
7
+
8
+ A generic Slack bot framework written in Ruby on top of [slack-ruby-client](https://github.com/dblock/slack-ruby-client). This library does all the heavy lifting, such as message parsing, so you can focus on implementing slack bot commands. It also attempts to introduce the bare minimum number of requirements or any sorts of limitations. It's a Slack bot boilerplate.
9
+
10
+ ![](slack.png)
11
+
12
+ ## Useful to Me?
13
+
14
+ * If you are just trying to send messages to Slack, use [slack-ruby-client](https://github.com/dblock/slack-ruby-client), which this library is built on top of.
15
+ * If you're trying to roll out a full service with Slack button integration, check out [slack-bot-server](https://github.com/dblock/slack-bot-server), which uses this library.
16
+ * Otherwise, this piece of the puzzle will help you create a single bot instance for one team.
17
+
18
+ ## Stable Release
19
+
20
+ You're reading the documentation for the **stable** release of slack-ruby-bot, 0.5.5.
21
+
22
+ ## Usage
23
+
24
+ ### A Minimal Bot
25
+
26
+ #### Gemfile
27
+
28
+ ```ruby
29
+ source 'http://rubygems.org'
30
+
31
+ gem 'slack-ruby-bot'
32
+ ```
33
+
34
+ #### pongbot.rb
35
+
36
+ ```ruby
37
+ require 'slack-ruby-bot'
38
+
39
+ class PongBot < SlackRubyBot::Bot
40
+ command 'ping' do |client, data, match|
41
+ client.message text: 'pong', channel: data.channel
42
+ end
43
+ end
44
+
45
+ PongBot.run
46
+ ```
47
+
48
+ After [registering the bot](DEPLOYMENT.md), run with `SLACK_API_TOKEN=... bundle exec ruby pongbot.rb`. Have the bot join a channel and send it a ping.
49
+
50
+ ![](screenshots/demo.gif)
51
+
52
+ ### A Production Bot
53
+
54
+ 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. See our [Writing a Production Bot](TUTORIAL.md) tutorial for more information.
55
+
56
+ ### More Involved Examples
57
+
58
+ The following examples of bots based on slack-ruby-bot are listed in growing order of complexity.
59
+
60
+ * [slack-bot-on-rails](https://github.com/dblock/slack-bot-on-rails): A bot running on Rails and using React to display Slack messages on a website.
61
+ * [slack-mathbot](https://github.com/dblock/slack-mathbot): Slack integration with math.
62
+ * [slack-google-bot](https://github.com/dblock/slack-google-bot): A Slack bot that searches Google, including CSE.
63
+ * [slack-aws](https://github.com/dblock/slack-aws): Slack integration with Amazon Web Services.
64
+ * [slack-gamebot](https://github.com/dblock/slack-gamebot): A game bot service for ping pong, chess, etc, hosted at [playplay.io](http://playplay.io).
65
+
66
+ ### Commands and Operators
67
+
68
+ Bots are addressed by name, they respond to commands and operators.
69
+
70
+ You can combine multiple commands and use a block to implement them.
71
+
72
+ ```ruby
73
+ command 'call', '呼び出し' do |client, data, match|
74
+ send_message client, data.channel, 'called'
75
+ end
76
+ ```
77
+
78
+ Command match data includes `match['bot']`, `match['command']` and `match['expression']`. The `bot` match always checks against the `SlackRubyBot::Config.user` and `SlackRubyBot::Config.user_id` values obtained when the bot starts.
79
+
80
+ Operators are 1-letter long and are similar to commands. They don't require addressing a bot nor separating an operator from its arguments. The following class responds to `=2+2`.
81
+
82
+ ```ruby
83
+ operator '=' do |data, match|
84
+ # implementation detail
85
+ end
86
+ ```
87
+
88
+ Operator match data includes `match['operator']` and `match['expression']`. The `bot` match always checks against the `SlackRubyBot::Config.user` setting.
89
+
90
+ ### Bot Aliases
91
+
92
+ A bot will always respond to its name (eg. `rubybot`) and Slack ID (eg. `@rubybot`), but you can specify multiple aliases via the `SLACK_RUBY_BOT_ALIASES` environment variable or via an explicit configuration.
93
+
94
+ ```
95
+ SLACK_RUBY_BOT_ALIASES=:pp: table-tennis
96
+ ```
97
+
98
+ ```ruby
99
+ SlackRubyBot.configure do |config|
100
+ config.aliases = [':pong:', 'pongbot']
101
+ end
102
+ ```
103
+
104
+ This is particularly fun with emoji.
105
+
106
+ ![](screenshots/aliases.gif)
107
+
108
+ Bots also will respond to a direct message, with or without the bot name in the message itself.
109
+
110
+ ![](screenshots/dms.gif)
111
+
112
+ ### Generic Routing
113
+
114
+ Commands and operators are generic versions of bot routes. You can respond to just about anything by defining a custom route.
115
+
116
+ ```ruby
117
+ class Weather < SlackRubyBot::Bot
118
+ match /^How is the weather in (?<location>\w*)\?$/ do |client, data, match|
119
+ send_message client, data.channel, "The weather in #{match[:location]} is nice."
120
+ end
121
+ end
122
+ ```
123
+
124
+ ![](screenshots/weather.gif)
125
+
126
+ ### SlackRubyBot::Commands::Base
127
+
128
+ The `SlackRubyBot::Bot` class is just sugare deriving from `SlackRubyBot::Commands::Base`. You can divide the bot implementation into subclasses of `SlackRubyBot::Commands::Base` manually. By default a command class responds, case-insensitively, to its name. A class called `Phone` that inherits from `SlackRubyBot::Commands::Base` responds to `phone` and `Phone` and calls the `call` method when implemented.
129
+
130
+ ```ruby
131
+ class Phone < SlackRubyBot::Commands::Base
132
+ command 'call'
133
+
134
+ def self.call(client, data, match)
135
+ send_message client, data.channel, 'called'
136
+ end
137
+ end
138
+ ```
139
+
140
+ To respond to custom commands and to disable automatic class name matching, use the `command` keyword. The following command responds to `call` and `呼び出し` (call in Japanese).
141
+
142
+ ```ruby
143
+ class Phone < SlackRubyBot::Commands::Base
144
+ command 'call'
145
+ command '呼び出し'
146
+
147
+ def self.call(client, data, match)
148
+ send_message client, data.channel, 'called'
149
+ end
150
+ end
151
+ ```
152
+
153
+ Other available functions include the following.
154
+
155
+ #### send_message(client, channel, text)
156
+
157
+ Send text using a RealTime client to a channel.
158
+
159
+ #### send_message_with_gif(client, channel, text, keyword)
160
+
161
+ Send text along with a random animated GIF based on a keyword.
162
+
163
+ ## send_gif(client, channel, keyword)
164
+
165
+ Send a random animated GIF based on a keyword.
166
+
167
+ ### Built-In Commands
168
+
169
+ Slack-ruby-bot comes with several built-in commands. You can re-define built-in commands, normally, as described above.
170
+
171
+ #### [bot name]
172
+
173
+ This is also known as the `default` command. Shows bot version and links.
174
+
175
+ #### [bot name] hi
176
+
177
+ Politely says 'hi' back.
178
+
179
+ #### [bot name] help
180
+
181
+ Get help.
182
+
183
+ ### Hooks
184
+
185
+ Hooks are event handlers and respond to Slack RTM API [events](https://api.slack.com/events), such as [hello](lib/slack-ruby-bot/hooks/hello.rb) or [message](lib/slack-ruby-bot/hooks/message.rb). You can implement your own by extending [SlackRubyBot::Hooks::Base](lib/slack-ruby-bot/hooks/base.rb).
186
+
187
+ For example, the following hook handles [user_change](https://api.slack.com/events/user_change), an event sent when a team member updates their profile or data. This can be useful to update the local user cache when a user is renamed.
188
+
189
+ ```ruby
190
+ module MyBot
191
+ module Hooks
192
+ module UserChange
193
+ extend SlackRubyBot::Hooks::Base
194
+
195
+ def user_change(client, data)
196
+ # data['user']['id'] contains the user ID
197
+ # data['user']['name'] contains the new user name
198
+ ...
199
+ end
200
+ end
201
+ end
202
+ end
203
+ ```
204
+
205
+ ### Disable Animated GIFs
206
+
207
+ By default bots send animated GIFs in default commands and errors. To disable animated GIFs set `send_gifs` or `ENV['SLACK_RUBY_BOT_SEND_GIFS']` to `false`.
208
+
209
+ ```ruby
210
+ SlackRubyBot.configure do |config|
211
+ config.send_gifs = false
212
+ end
213
+ ```
214
+
215
+ ### Message Loop Protection
216
+
217
+ By default bots do not respond to their own messages. If you wish to change that behavior, set `allow_message_loops` to `true`.
218
+
219
+ ```ruby
220
+ SlackRubyBot.configure do |config|
221
+ config.allow_message_loops = true
222
+ end
223
+ ```
224
+
225
+ ### Advanced Integration
226
+
227
+ You may want to integrate a bot or multiple bots into other systems, in which case a globally configured bot may not work for you. You may create instances of [SlackRubyBot::Server](lib/slack-ruby-bot/server.rb) which accepts `token`, `aliases` and `send_gifs`.
228
+
229
+ ```ruby
230
+ EM.run do
231
+ bot1 = SlackRubyBot::Server.new(token: token1, aliases: ['bot1'])
232
+ bot1.auth!
233
+ bot1.start_async
234
+
235
+ bot2 = SlackRubyBot::Server.new(token: token2, send_gifs: false, aliases: ['bot2'])
236
+ bot2.auth!
237
+ bot2.start_async
238
+ end
239
+ ```
240
+
241
+ For an example of advanced integration that supports multiple teams, see [slack-gamebot](https://github.com/dblock/slack-gamebot) and [playplay.io](http://playplay.io) that is built on top of it.
242
+
243
+ ### RSpec Shared Behaviors
244
+
245
+ Slack-ruby-bot ships with a number of shared RSpec behaviors that can be used in your RSpec tests. Require 'slack-ruby-bot/rspec' in your `spec_helper.rb`.
246
+
247
+ * [behaves like a slack bot](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/it_behaves_like_a_slack_bot.rb): A bot quacks like a Slack Ruby bot.
248
+ * [respond with slack message](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb): The bot responds with a message.
249
+ * [respond with error](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb): An exception is raised inside a bot command.
250
+
251
+ ## Contributing
252
+
253
+ See [CONTRIBUTING](CONTRIBUTING.md).
254
+
255
+ ## Upgrading
256
+
257
+ See [CHANGELOG](CHANGELOG.md) for a history of changes and [UPGRADING](UPGRADING.md) for how to upgrade to more recent versions.
258
+
259
+ ## Copyright and License
260
+
261
+ Copyright (c) 2015-2016, [Daniel Doubrovkine](https://twitter.com/dblockdotorg), [Artsy](https://www.artsy.net) and [Contributors](CHANGELOG.md).
262
+
263
+ This project is licensed under the [MIT License](LICENSE.md).