@ -0,0 +1,3 @@ | |||
.env | |||
pkg | |||
Gemfile.lock |
@ -0,0 +1,2 @@ | |||
--format documentation | |||
--color |
@ -0,0 +1,6 @@ | |||
AllCops: | |||
Exclude: | |||
- vendor/**/* | |||
- bin/**/* | |||
inherit_from: .rubocop_todo.yml |
@ -0,0 +1,45 @@ | |||
# This configuration was generated by `rubocop --auto-gen-config` | |||
# on 2015-09-23 08:45:19 -0400 using RuboCop version 0.32.1. | |||
# The point is for the user to remove these configuration records | |||
# one by one as the offenses are removed from the code base. | |||
# Note that changes in the inspected code, or installation of new | |||
# versions of RuboCop, may require this file to be generated again. | |||
# Offense count: 1 | |||
Lint/HandleExceptions: | |||
Enabled: false | |||
# Offense count: 1 | |||
Lint/UselessAccessModifier: | |||
Enabled: false | |||
# Offense count: 4 | |||
Metrics/AbcSize: | |||
Max: 23 | |||
# Offense count: 1 | |||
Metrics/CyclomaticComplexity: | |||
Max: 7 | |||
# Offense count: 85 | |||
# Configuration parameters: AllowURI, URISchemes. | |||
Metrics/LineLength: | |||
Max: 142 | |||
# Offense count: 4 | |||
# Configuration parameters: CountComments. | |||
Metrics/MethodLength: | |||
Max: 21 | |||
# Offense count: 15 | |||
Style/Documentation: | |||
Enabled: false | |||
# Offense count: 1 | |||
# Configuration parameters: Exclude. | |||
Style/FileName: | |||
Enabled: false | |||
# Offense count: 1 | |||
Style/ModuleFunction: | |||
Enabled: false |
@ -0,0 +1,3 @@ | |||
rvm: | |||
- 2.1.6 | |||
@ -0,0 +1,45 @@ | |||
### 0.4.4 (Next) | |||
* [#17](https://github.com/dblock/slack-ruby-bot/issues/17): Address bot by `name:` - [@dblock](https://githubcom/dblock). | |||
* [#19](https://github.com/dblock/slack-ruby-bot/issues/19): Retry on `Faraday::Error::TimeoutError`, `TimeoutError` and `SSLError` - [@dblock](https://githubcom/dblock). | |||
* [#3](https://github.com/dblock/slack-ruby-bot/issues/3): Retry on `migration_in_progress` errors during `rtm.start` - [@dblock](https://githubcom/dblock). | |||
* Respond to direct messages without being addressed by name - [@dblock](https://githubcom/dblock). | |||
* Added `send_gif`, to allow GIFs to be sent without text - [@maclover7](https://github.com/maclover7). | |||
### 0.4.3 (8/21/2015) | |||
* [#13](https://github.com/dblock/slack-ruby-bot/issues/13): You can now address the bot by its Slack @id - [@dblock](https://githubcom/dblock). | |||
### 0.4.2 (8/20/2015) | |||
* [#12](https://github.com/dblock/slack-ruby-bot/issues/12): Added support for bot aliases - [@dblock](https://githubcom/dblock). | |||
### 0.4.1 (7/25/2015) | |||
* Use a real client in `respond_with_slack_message` expectaions - [@dblock](https://githubcom/dblock). | |||
### 0.4.0 (7/25/2015) | |||
* Using [slack-ruby-client](https://github.com/dblock/slack-ruby-client) - [@dblock](https://githubcom/dblock). | |||
* Use RealTime API to post messages - [@dblock](https://githubcom/dblock). | |||
### 0.3.1 (7/21/2015) | |||
* [#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). | |||
### 0.3.0 (7/19/2015) | |||
* [#5](https://github.com/dblock/slack-ruby-bot/issues/5): Added support for free-formed routes via `match` - [@dblock](https://github.com/dblock). | |||
* [#6](https://github.com/dblock/slack-ruby-bot/issues/6): Commands and operators take blocks - [@dblock](https://github.com/dblock). | |||
* [#4](https://github.com/dblock/slack-ruby-bot/issues/4): Messages are posted with `as_user: true` by default - [@dblock](https://github.com/dblock). | |||
### 0.2.0 (7/10/2015) | |||
* 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). | |||
* Added support for operators with `operator [name]` - [@dblock](https://github.com/dblock). | |||
* Added support for custom commands with `command [name]` - [@dblock](https://github.com/dblock). | |||
### 0.1.0 (6/2/2015) | |||
* Initial public release - [@dblock](https://github.com/dblock). | |||
@ -0,0 +1,139 @@ | |||
# Contributing to SlackRubyBot | |||
This project is work of [many contributors](https://github.com/dblock/slack-ruby-bot/graphs/contributors). | |||
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). | |||
In the examples below, substitute your Github username for `contributor` in URLs. | |||
## Fork the Project | |||
Fork the [project on Github](https://github.com/dblock/slack-ruby-bot) and check out your copy. | |||
``` | |||
git clone https://github.com/contributor/slack-ruby-bot.git | |||
cd slack-ruby-bot | |||
git remote add upstream https://github.com/dblock/slack-ruby-bot.git | |||
``` | |||
## Bundle Install and Test | |||
Ensure that you can build the project and run tests. | |||
``` | |||
bundle install | |||
bundle exec rake | |||
``` | |||
## Run SlackRubyBot in Development | |||
Create a private slack group for yourself. | |||
Create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot). | |||
![](screenshots/register-bot.png) | |||
On the next screen, note the API token. | |||
Run `SLACK_API_TOKEN=<your API token> foreman start`. | |||
You can also create a `.env` file with `SLACK_API_TOKEN=<your API token>` and just run `foreman start`. | |||
## Create a Topic Branch | |||
Make sure your fork is up-to-date and create a topic branch for your feature or bug fix. | |||
``` | |||
git checkout master | |||
git pull upstream master | |||
git checkout -b my-feature-branch | |||
``` | |||
## Write Tests | |||
Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. | |||
Add to [spec](spec). | |||
We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix. | |||
## Write Code | |||
Implement your feature or bug fix. | |||
Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop). | |||
Run `bundle exec rubocop` and fix any style issues highlighted. | |||
Make sure that `bundle exec rake` completes without errors. | |||
## Write Documentation | |||
Document any external behavior in the [README](README.md). | |||
## Update Changelog | |||
Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. | |||
Make it look like every other line, including your name and link to your Github account. | |||
## Commit Changes | |||
Make sure git knows your name and email address: | |||
``` | |||
git config --global user.name "Your Name" | |||
git config --global user.email "contributor@example.com" | |||
``` | |||
Writing good commit logs is important. A commit log should describe what changed and why. | |||
``` | |||
git add ... | |||
git commit | |||
``` | |||
## Push | |||
``` | |||
git push origin my-feature-branch | |||
``` | |||
## Make a Pull Request | |||
Go to https://github.com/contributor/slack-ruby-bot and select your feature branch. | |||
Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days. | |||
## Rebase | |||
If you've been working on a change for a while, rebase with upstream/master. | |||
``` | |||
git fetch upstream | |||
git rebase upstream/master | |||
git push origin my-feature-branch -f | |||
``` | |||
## Update CHANGELOG Again | |||
Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows. | |||
``` | |||
* [#123](https://github.com/dblock/slack-ruby-bot/pull/123): Reticulated splines - [@contributor](https://github.com/contributor). | |||
``` | |||
Amend your previous commit and force push the changes. | |||
``` | |||
git commit --amend | |||
git push origin my-feature-branch -f | |||
``` | |||
## Check on Your Pull Request | |||
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. | |||
## Be Patient | |||
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! | |||
## Thank You | |||
Please do know that we really appreciate and value your time and work. We love you, really. |
@ -0,0 +1,33 @@ | |||
## Installation | |||
Create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot). | |||
![](screenshots/register-bot.png) | |||
On the next screen, note the API token. | |||
### Environment | |||
#### SLACK_API_TOKEN | |||
Set SLACK_API_TOKEN from the Bot integration settings on Slack. | |||
``` | |||
heroku config:add SLACK_API_TOKEN=... | |||
``` | |||
#### GIPHY_API_KEY | |||
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. | |||
#### SLACK_RUBY_BOT_ALIASES | |||
Optional names for this bot. | |||
``` | |||
heroku config:add SLACK_RUBY_BOT_ALIASES=":pong: table-tennis ping-pong" | |||
``` | |||
### Heroku Idling | |||
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. |
@ -0,0 +1,3 @@ | |||
source 'http://rubygems.org' | |||
gemspec |
@ -0,0 +1,22 @@ | |||
MIT License | |||
Copyright (c) 2015 Daniel Doubrovkine, Artsy and Contributors | |||
Permission is hereby granted, free of charge, to any person obtaining | |||
a copy of this software and associated documentation files (the | |||
"Software"), to deal in the Software without restriction, including | |||
without limitation the rights to use, copy, modify, merge, publish, | |||
distribute, sublicense, and/or sell copies of the Software, and to | |||
permit persons to whom the Software is furnished to do so, subject to | |||
the following conditions: | |||
The above copyright notice and this permission notice shall be | |||
included in all copies or substantial portions of the Software. | |||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@ -0,0 +1,216 @@ | |||
Slack-Ruby-Bot | |||
============== | |||
[![Gem Version](https://badge.fury.io/rb/slack-ruby-bot.svg)](http://badge.fury.io/rb/slack-ruby-bot) | |||
[![Build Status](https://travis-ci.org/dblock/slack-ruby-bot.png)](https://travis-ci.org/dblock/slack-ruby-bot) | |||
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. | |||
## Usage | |||
### A Minimal Bot | |||
#### Gemfile | |||
```ruby | |||
source 'http://rubygems.org' | |||
gem 'slack-ruby-bot' | |||
``` | |||
#### pongbot.rb | |||
```ruby | |||
require 'slack-ruby-bot' | |||
module PongBot | |||
class App < SlackRubyBot::App | |||
end | |||
class Ping < SlackRubyBot::Commands::Base | |||
command 'ping' do |client, data, _match| | |||
client.message text: 'pong', channel: data.channel | |||
end | |||
end | |||
end | |||
PongBot::App.instance.run | |||
``` | |||
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. | |||
![](screenshots/demo.gif) | |||
### A Production Bot | |||
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. | |||
### More Involved Examples | |||
The following examples of production-grade bots based on slack-ruby-bot are listed in growing order of complexity. | |||
* [slack-mathbot](https://github.com/dblock/slack-mathbot): Slack integration with math. | |||
* [slack-google-bot](https://github.com/dblock/slack-google-bot): A Slack bot that searches Google, including CSE. | |||
* [slack-aws](https://github.com/dblock/slack-aws): Slack integration with Amazon Web Services. | |||
* [slack-gamebot](https://github.com/dblock/slack-gamebot): A generic game bot for ping-pong, chess, etc. | |||
### Commands and Operators | |||
Bots are addressed by name, they respond to commands and operators. 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. | |||
```ruby | |||
class Phone < SlackRubyBot::Commands::Base | |||
command 'call' | |||
def self.call(client, data, _match) | |||
send_message client, data.channel, 'called' | |||
end | |||
end | |||
``` | |||
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). | |||
```ruby | |||
class Phone < SlackRubyBot::Commands::Base | |||
command 'call' | |||
command '呼び出し' | |||
def self.call(client, data, _match) | |||
send_message client, data.channel, 'called' | |||
end | |||
end | |||
``` | |||
You can combine multiple commands and use a block to implement them. | |||
```ruby | |||
class Phone < SlackRubyBot::Commands::Base | |||
command 'call', '呼び出し' do |client, data, _match| | |||
send_message client, data.channel, 'called' | |||
end | |||
end | |||
``` | |||
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. | |||
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`. | |||
```ruby | |||
class Calculator < SlackRubyBot::Commands::Base | |||
operator '=' do |_data, _match| | |||
# implementation detail | |||
end | |||
end | |||
``` | |||
Operator match data includes `match['operator']` and `match['expression']`. The `bot` match always checks against the `SlackRubyBot::Config.user` setting. | |||
### Bot Aliases | |||
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. | |||
``` | |||
SLACK_RUBY_BOT_ALIASES=:pp: table-tennis | |||
``` | |||
```ruby | |||
SlackRubyBot.configure do |config| | |||
config.aliases = [':pong:', 'pongbot'] | |||
end | |||
``` | |||
This is particularly fun with emoji. | |||
![](screenshots/aliases.gif) | |||
Bots also will respond to a direct message, with or without the bot name in the message itself. | |||
![](screenshots/dms.gif) | |||
### Generic Routing | |||
Commands and operators are generic versions of bot routes. You can respond to just about anything by defining a custom route. | |||
```ruby | |||
class Weather < SlackRubyBot::Commands::Base | |||
match /^How is the weather in (?<location>\w*)\?$/ do |client, data, match| | |||
send_message client, data.channel, "The weather in #{match[:location]} is nice." | |||
end | |||
end | |||
``` | |||
![](screenshots/weather.gif) | |||
### SlackRubyBot::Commands::Base Functions | |||
#### send_message(client, channel, text) | |||
Send text using a RealTime client to a channel. | |||
#### send_message_with_gif(client, channel, text, keyword) | |||
Send text along with a random animated GIF based on a keyword. | |||
## send_gif(client, channel, keyword) | |||
Send a random animated GIF based on a keyword. | |||
### Built-In Commands | |||
Slack-ruby-bot comes with several built-in commands. You can re-define built-in commands, normally, as described above. | |||
#### [bot name] | |||
This is also known as the `default` command. Shows bot version and links. | |||
#### [bot name] hi | |||
Politely says 'hi' back. | |||
#### [bot name] help | |||
Get help. | |||
### Hooks | |||
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). | |||
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. | |||
```ruby | |||
module MyBot | |||
module Hooks | |||
module UserChange | |||
extend SlackRubyBot::Hooks::Base | |||
def user_change(client, data) | |||
# data['user']['id'] contains the user ID | |||
# data['user']['name'] contains the new user name | |||
... | |||
end | |||
end | |||
end | |||
end | |||
``` | |||
### RSpec Shared Behaviors | |||
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`. | |||
* [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. | |||
* [respond with slack message](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb): The bot responds with a message. | |||
* [respond with error](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb): An exception is raised inside a bot command. | |||
## Contributing | |||
See [CONTRIBUTING](CONTRIBUTING.md). | |||
## Upgrading | |||
See [CHANGELOG](CHANGELOG.md) for a history of changes and [UPGRADING](UPGRADING.md) for how to upgrade to more recent versions. | |||
## Copyright and License | |||
Copyright (c) 2015, [Daniel Doubrovkine](https://twitter.com/dblockdotorg), [Artsy](https://www.artsy.net) and [Contributors](CHANGELOG.md). | |||
This project is licensed under the [MIT License](LICENSE.md). |
@ -0,0 +1,67 @@ | |||
# Releasing Slack-Ruby-Bot | |||
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. | |||
### Release | |||
Run tests, check that all tests succeed locally. | |||
``` | |||
bundle install | |||
rake | |||
``` | |||
Check that the last build succeeded in [Travis CI](https://travis-ci.org/dblock/slack-ruby-bot) for all supported platforms. | |||
Increment the version, modify [lib/slack-ruby-bot/version.rb](lib/slack-ruby-bot/version.rb). | |||
* 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`). | |||
* Increment the second number if the release contains major features or breaking API changes (eg. change `0.2.1` to `0.3.0`). | |||
Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version. | |||
``` | |||
### 0.2.2 (7/10/2015) | |||
``` | |||
Remove the line with "Your contribution here.", since there will be no more contributions to this release. | |||
Commit your changes. | |||
``` | |||
git add CHANGELOG.md lib/slack-ruby-bot/version.rb | |||
git commit -m "Preparing for release, 0.2.2." | |||
git push origin master | |||
``` | |||
Release. | |||
``` | |||
$ rake release | |||
slack-ruby-bot 0.2.2 built to pkg/slack-ruby-bot-0.2.2.gem. | |||
Tagged v0.2.2. | |||
Pushed git commits and tags. | |||
Pushed slack-ruby-bot 0.2.2 to rubygems.org. | |||
``` | |||
### Prepare for the Next Version | |||
Add the next release to [CHANGELOG.md](CHANGELOG.md). | |||
``` | |||
Next Release | |||
============ | |||
* Your contribution here. | |||
``` | |||
Increment the third version number in [lib/slack-ruby-bot/version.rb](lib/slack-ruby-bot/version.rb). | |||
Comit your changes. | |||
``` | |||
git add CHANGELOG.md lib/slack-ruby-bot/version.rb | |||
git commit -m "Preparing for next development iteration, 0.2.3." | |||
git push origin master | |||
``` |
@ -0,0 +1,19 @@ | |||
require 'rubygems' | |||
require 'bundler' | |||
require 'bundler/gem_tasks' | |||
Bundler.setup :default, :development | |||
unless ENV['RACK_ENV'] == 'production' | |||
require 'rspec/core' | |||
require 'rspec/core/rake_task' | |||
RSpec::Core::RakeTask.new(:spec) do |spec| | |||
spec.pattern = FileList['spec/**/*_spec.rb'] | |||
end | |||
require 'rubocop/rake_task' | |||
RuboCop::RakeTask.new | |||
task default: [:rubocop, :spec] | |||
end |
@ -0,0 +1,203 @@ | |||
## Production Bot Tutorial | |||
In this tutorial we'll implement [slack-mathbot](https://github.com/dblock/slack-mathbot). | |||
### Introduction | |||
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`. | |||
### Getting Started | |||
#### Gemfile | |||
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). | |||
```ruby | |||
source 'http://rubygems.org' | |||
gem 'slack-ruby-bot' | |||
gem 'puma' | |||
gem 'sinatra' | |||
group :development, :test do | |||
gem 'rake' | |||
gem 'foreman' | |||
end | |||
group :test do | |||
gem 'rspec' | |||
gem 'rack-test' | |||
end | |||
``` | |||
Run `bundle install` to get all the gems. | |||
#### Application | |||
Create a folder called `slack-mathbot` and inside of it create `app.rb`. | |||
```ruby | |||
module SlackMathbot | |||
class App < SlackRubyBot::App | |||
end | |||
end | |||
``` | |||
#### Commands | |||
Create a folder called `slack-mathbot/commands` and inside of it create `calculate.rb`. For now this calculator will always return 4. | |||
```ruby | |||
module SlackMathbot | |||
module Commands | |||
class Calculate < SlackRubyBot::Commands::Base | |||
command 'calculate' do |client, data, _match| | |||
send_message client, data.channel, '4' | |||
end | |||
end | |||
end | |||
end | |||
``` | |||
#### Require Everything | |||
Create a `slack-mathbot.rb` at the root and require the above files. | |||
```ruby | |||
require 'slack-ruby-bot' | |||
require 'slack-mathbot/commands/calculate' | |||
require 'slack-mathbot/app' | |||
``` | |||
#### Web Server | |||
We will need to keep the bot alive on Heroku, so create `web.rb`. | |||
```ruby | |||
require 'sinatra/base' | |||
module SlackMathbot | |||
class Web < Sinatra::Base | |||
get '/' do | |||
'Math is good for you.' | |||
end | |||
end | |||
end | |||
``` | |||
#### Config.ru | |||
Tie all the pieces togehter in `config.ru` which creates a thread for the bot and runs the web server on the main thread. | |||
```ruby | |||
$LOAD_PATH.unshift(File.dirname(__FILE__)) | |||
require 'slack-mathbot' | |||
require 'web' | |||
Thread.new do | |||
begin | |||
SlackMathbot::App.instance.run | |||
rescue Exception => e | |||
STDERR.puts "ERROR: #{e}" | |||
STDERR.puts e.backtrace | |||
raise e | |||
end | |||
end | |||
run SlackMathbot::Web | |||
``` | |||
### Create a Bot User | |||
In Slack administration create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot). | |||
![](screenshots/register-bot.png) | |||
On the next screen, note the API token. | |||
#### .env | |||
Create a `.env` file with the API token from above and make sure to add it to `.gitignore`. | |||
``` | |||
SLACK_API_TOKEN=... | |||
``` | |||
### Procfile | |||
Create a `Procfile` which `foreman` will use when you run the `foreman start` command below. | |||
``` | |||
web: bundle exec puma -p $PORT | |||
``` | |||
### Run the Bot | |||
Run `foreman start`. Your bot should be running. | |||
``` | |||
14:32:32 web.1 | Puma starting in single mode... | |||
14:32:32 web.1 | * Version 2.11.3 (ruby 2.1.6-p336), codename: Intrepid Squirrel | |||
14:32:32 web.1 | * Min threads: 0, max threads: 16 | |||
14:32:32 web.1 | * Environment: development | |||
14:32:35 web.1 | * Listening on tcp://0.0.0.0:5000 | |||
14:32:35 web.1 | Use Ctrl-C to stop | |||
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/. | |||
14:32:36 web.1 | I, [2015-07-10T14:32:36.766955 #98948] INFO -- : Successfully connected to https://xyz.slack.com/. | |||
``` | |||
### Try | |||
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. | |||
### Write Tests | |||
#### Spec Helper | |||
Create `spec/spec_helper.rb` that includes the bot files and shared RSpec support from slack-ruby-bot. | |||
```ruby | |||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..')) | |||
require 'slack-ruby-bot/rspec' | |||
require 'slack-mathbot' | |||
``` | |||
#### Test the Bot Application | |||
Create a test for the bot application itself in `spec/slack-mathbot/app_spec.rb`. | |||
```ruby | |||
require 'spec_helper' | |||
describe SlackMathbot::App do | |||
def app | |||
SlackMathbot::App.new | |||
end | |||
it_behaves_like 'a slack ruby bot' | |||
end | |||
``` | |||
#### Test a Command | |||
Create a test for the `calculate` command in `spec/slack-mathbot/commands/calculate_spec.rb`. The bot is addressed by its user name. | |||
```ruby | |||
require 'spec_helper' | |||
describe SlackMathbot::Commands::Calculate do | |||
def app | |||
SlackMathbot::App.new | |||
end | |||
it 'returns 4' do | |||
expect(message: "#{SlackRubyBot.config.user} calculate 2+2", channel: 'channel').to respond_with_slack_message('4') | |||
end | |||
end | |||
``` | |||
See [lib/slack-ruby-bot/rspec/support/slack-ruby-bot](lib/slack-ruby-bot/rspec/support/slack-ruby-bot) for other shared RSpec behaviors. | |||
### Deploy | |||
See [DEPLOYMENT](DEPLOYMENT.md) for how to deploy your bot to production. |
@ -0,0 +1,64 @@ | |||
Upgrading SlackRubyBot | |||
====================== | |||
### Upgrading to >= 0.4.0 | |||
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). | |||
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`. | |||
Before: | |||
```ruby | |||
def self.call(data, match) | |||
... | |||
end | |||
``` | |||
After: | |||
```ruby | |||
def self.call(client, data, match) | |||
... | |||
end | |||
``` | |||
This also applies to `command`, `operator` and `match` blocks. | |||
Before: | |||
```ruby | |||
command 'ping' do |data, match| | |||
... | |||
end | |||
``` | |||
After: | |||
```ruby | |||
command 'ping' do |client, data, match| | |||
... | |||
end | |||
``` | |||
You can now send messages directly via the RealTime Messaging API. | |||
```ruby | |||
client.message text: 'text', channel: 'channel' | |||
``` | |||
Otherwise you must now pass the `client` parameter to `send_message` and `send_message_with_gif`. | |||
```ruby | |||
def self.call(client, data, match) | |||
send_message client, data.channel, 'hello' | |||
end | |||
``` | |||
```ruby | |||
def self.call(client, data, match) | |||
send_message_with_gif client, data.channel, 'hello', 'hi' | |||
end | |||
``` | |||
@ -0,0 +1,3 @@ | |||
source 'http://rubygems.org' | |||
gem 'slack-ruby-bot', path: '../..' |
@ -0,0 +1 @@ | |||
console: bundle exec ruby pongbot.rb |
@ -0,0 +1,14 @@ | |||
require 'slack-ruby-bot' | |||
module PongBot | |||
class App < SlackRubyBot::App | |||
end | |||
class Ping < SlackRubyBot::Commands::Base | |||
def self.call(client, data, _match) | |||
client.message text: 'pong', channel: data.channel | |||
end | |||
end | |||
end | |||
PongBot::App.instance.run |
@ -0,0 +1,3 @@ | |||
source 'http://rubygems.org' | |||
gem 'slack-ruby-bot', path: '../..' |
@ -0,0 +1,14 @@ | |||
require 'slack-ruby-bot' | |||
module WeatherBot | |||
class App < SlackRubyBot::App | |||
end | |||
class Weather < SlackRubyBot::Commands::Base | |||
match(/^How is the weather in (?<location>\w*)\?$/i) do |client, data, match| | |||
send_message client, data.channel, "The weather in #{match[:location]} is nice." | |||
end | |||
end | |||
end | |||
WeatherBot::App.instance.run |
@ -0,0 +1,14 @@ | |||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..')) | |||
$LOAD_PATH.unshift(File.dirname(__FILE__)) | |||
require 'boot' | |||
Bundler.require :default, ENV['RACK_ENV'] | |||
Dir[File.expand_path('../../initializers', __FILE__) + '/**/*.rb'].each do |file| | |||
require file | |||
end | |||
require File.expand_path('../application', __FILE__) | |||
require 'slack_ruby_bot' |
@ -0,0 +1,8 @@ | |||
require 'rubygems' | |||
require 'bundler/setup' | |||
require 'logger' | |||
require 'active_support' | |||
require 'active_support/core_ext' | |||
require 'hashie' | |||
require 'slack' | |||
require 'giphy' |
@ -0,0 +1,3 @@ | |||
ENV['RACK_ENV'] ||= 'development' | |||
require File.expand_path('../application', __FILE__) |
@ -0,0 +1,3 @@ | |||
Giphy::Configuration.configure do |config| | |||
config.api_key = ENV['GIPHY_API_KEY'] || 'dc6zaTOxFJmzC' # from https://github.com/Giphy/GiphyAPI | |||
end |
@ -0,0 +1,21 @@ | |||
require File.expand_path('../config/environment', __FILE__) | |||
require 'slack-ruby-bot/version' | |||
require 'slack-ruby-bot/about' | |||
require 'slack-ruby-bot/config' | |||
require 'slack-ruby-bot/hooks' | |||
module SlackRubyBot | |||
class << self | |||
def configure | |||
block_given? ? yield(Config) : Config | |||
end | |||
def config | |||
Config | |||
end | |||
end | |||
end | |||
require 'slack-ruby-bot/commands' | |||
require 'slack-ruby-bot/app' |
@ -0,0 +1,7 @@ | |||
module SlackRubyBot | |||
ABOUT = <<-ABOUT | |||
#{SlackRubyBot::VERSION} | |||
https://github.com/dblock/slack-ruby-bot | |||
https://twitter.com/dblockdotorg | |||
ABOUT | |||
end |
@ -0,0 +1,111 @@ | |||
module SlackRubyBot | |||
class App | |||
cattr_accessor :hooks | |||
include SlackRubyBot::Hooks::Hello | |||
include SlackRubyBot::Hooks::Message | |||
def initialize | |||
SlackRubyBot.configure do |config| | |||
config.token = ENV['SLACK_API_TOKEN'] || fail("Missing ENV['SLACK_API_TOKEN'].") | |||
config.aliases = ENV['SLACK_RUBY_BOT_ALIASES'].split(' ') if ENV['SLACK_RUBY_BOT_ALIASES'] | |||
end | |||
Slack.configure do |config| | |||
config.token = SlackRubyBot.config.token | |||
end | |||
end | |||
def config | |||
SlackRubyBot.config | |||
end | |||
def self.instance | |||
@instance ||= SlackRubyBot::App.new | |||
end | |||
def run | |||
auth! | |||
start! | |||
end | |||
def stop! | |||
client.stop | |||
end | |||
private | |||
def logger | |||
@logger ||= begin | |||
$stdout.sync = true | |||
Logger.new(STDOUT) | |||
end | |||
end | |||
def start! | |||
loop do | |||
begin | |||
client.start! | |||
rescue Slack::Web::Api::Error => e | |||
logger.error e | |||
case e.message | |||
when 'migration_in_progress' | |||
sleep 1 # ignore, try again | |||
else | |||
raise e | |||
end | |||
rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed, Faraday::Error::SSLError => e | |||
logger.error e | |||
sleep 1 # ignore, try again | |||
rescue StandardError => e | |||
logger.error e | |||
raise e | |||
ensure | |||
@client = nil | |||
end | |||
end | |||
end | |||
def client | |||
@client ||= begin | |||
client = Slack::RealTime::Client.new | |||
hooks.each do |hook| | |||
client.on hook do |data| | |||
begin | |||
send hook, client, data | |||
rescue StandardError => e | |||
logger.error e | |||
begin | |||
client.message(channel: data['channel'], text: e.message) if data.key?('channel') | |||
rescue | |||
# ignore | |||
end | |||
end | |||
end | |||
end | |||
client | |||
end | |||
end | |||
def auth! | |||
auth = client.web_client.auth_test | |||
SlackRubyBot.configure do |config| | |||
config.url = auth['url'] | |||
config.team = auth['team'] | |||
config.user = auth['user'] | |||
config.team_id = auth['team_id'] | |||
config.user_id = auth['user_id'] | |||
end | |||
logger.info "Welcome '#{SlackRubyBot.config.user}' to the '#{SlackRubyBot.config.team}' team at #{SlackRubyBot.config.url}." | |||
end | |||
def reset! | |||
SlackRubyBot.configure do |config| | |||
config.url = nil | |||
config.team = nil | |||
config.user = nil | |||
config.team_id = nil | |||
config.user_id = nil | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,5 @@ | |||
require 'slack-ruby-bot/commands/base' | |||
require 'slack-ruby-bot/commands/about' | |||
require 'slack-ruby-bot/commands/help' | |||
require 'slack-ruby-bot/commands/hi' | |||
require 'slack-ruby-bot/commands/unknown' |
@ -0,0 +1,12 @@ | |||
module SlackRubyBot | |||
module Commands | |||
class Default < Base | |||
command 'about' | |||
match(/^(?<bot>[\w[:punct:]@<>]*)$/) | |||
def self.call(client, data, _match) | |||
send_message_with_gif client, data.channel, SlackRubyBot::ABOUT, 'selfie' | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,119 @@ | |||
module SlackRubyBot | |||
module Commands | |||
class Base | |||
class_attribute :routes | |||
def self.send_message(client, channel, text, options = {}) | |||
if text && text.length > 0 | |||
send_client_message(client, { channel: channel, text: text }.merge(options)) | |||
else | |||
send_message_with_gif client, channel, 'Nothing to see here.', 'nothing', options | |||
end | |||
end | |||
def self.send_message_with_gif(client, channel, text, keywords, options = {}) | |||
get_gif_and_send({ | |||
client: client, | |||
channel: channel, | |||
text: text, | |||
keywords: keywords | |||
}.merge(options)) | |||
end | |||
def self.send_gif(client, channel, keywords, options = {}) | |||
get_gif_and_send({ | |||
client: client, | |||
channel: channel, | |||
keywords: keywords | |||
}.merge(options)) | |||
end | |||
def self.logger | |||
@logger ||= begin | |||
$stdout.sync = true | |||
Logger.new(STDOUT) | |||
end | |||
end | |||
def self.default_command_name | |||
name && name.split(':').last.downcase | |||
end | |||
def self.operator(*values, &block) | |||
values.each do |value| | |||
match Regexp.new("^(?<operator>\\#{value})(?<expression>.*)$", Regexp::IGNORECASE), &block | |||
end | |||
end | |||
def self.command(*values, &block) | |||
values.each do |value| | |||
match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})$", Regexp::IGNORECASE), &block | |||
match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block | |||
end | |||
end | |||
def self.invoke(client, data) | |||
self.finalize_routes! | |||
expression = parse(data) | |||
called = false | |||
routes.each_pair do |route, method| | |||
match = route.match(expression) | |||
next unless match | |||
next if match.names.include?('bot') && !SlackRubyBot.config.name?(match['bot']) | |||
called = true | |||
if method | |||
method.call(client, data, match) | |||
elsif self.respond_to?(:call) | |||
send(:call, client, data, match) | |||
else | |||
fail NotImplementedError, data.text | |||
end | |||
break | |||
end | |||
called | |||
end | |||
def self.match(match, &block) | |||
self.routes ||= {} | |||
self.routes[match] = block | |||
end | |||
private | |||
def self.parse(data) | |||
text = data.text | |||
return text unless data.channel && data.channel[0] == 'D' && data.user && data.user != SlackRubyBot.config.user_id | |||
SlackRubyBot.config.names.each do |name| | |||
text.downcase.tap do |td| | |||
return text if td == name || td.starts_with?("#{name} ") | |||
end | |||
end | |||
"#{SlackRubyBot.config.user} #{text}" | |||
end | |||
def self.finalize_routes! | |||
return if self.routes && self.routes.any? | |||
command default_command_name | |||
end | |||
def self.get_gif_and_send(options = {}) | |||
options = options.dup | |||
gif = begin | |||
keywords = options.delete(:keywords) | |||
Giphy.random(keywords) | |||
rescue StandardError => e | |||
logger.warn "Giphy.random: #{e.message}" | |||
nil | |||
end | |||
client = options.delete(:client) | |||
text = options.delete(:text) | |||
text = [text, gif && gif.image_url.to_s].compact.join("\n") | |||
send_client_message(client, { text: text }.merge(options)) | |||
end | |||
def self.send_client_message(client, data) | |||
client.message(data) | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,9 @@ | |||
module SlackRubyBot | |||
module Commands | |||
class Help < Base | |||
def self.call(client, data, _match) | |||
send_message_with_gif client, data.channel, 'See https://github.com/dblock/slack-ruby-bot, please.', 'help' | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,9 @@ | |||
module SlackRubyBot | |||
module Commands | |||
class Hi < Base | |||
def self.call(client, data, _match) | |||
send_message client, data.channel, "Hi <@#{data.user}>!" | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,11 @@ | |||
module SlackRubyBot | |||
module Commands | |||
class Unknown < Base | |||
match(/^(?<bot>\w*)[\s]*(?<expression>.*)$/) | |||
def self.call(client, data, _match) | |||
send_message_with_gif client, data.channel, "Sorry <@#{data.user}>, I don't understand that command!", 'idiot' | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,21 @@ | |||
module SlackRubyBot | |||
module Config | |||
extend self | |||
attr_accessor :token | |||
attr_accessor :url | |||
attr_accessor :aliases | |||
attr_accessor :user | |||
attr_accessor :user_id | |||
attr_accessor :team | |||
attr_accessor :team_id | |||
def names | |||
[user, aliases, "<@#{user_id.downcase}>", "<@#{user_id.downcase}>:", "#{user}:"].compact.flatten | |||
end | |||
def name?(name) | |||
name && names.include?(name.downcase) | |||
end | |||
end | |||
end |
@ -0,0 +1,3 @@ | |||
require 'slack-ruby-bot/hooks/base' | |||
require 'slack-ruby-bot/hooks/hello' | |||
require 'slack-ruby-bot/hooks/message' |
@ -0,0 +1,10 @@ | |||
module SlackRubyBot | |||
module Hooks | |||
module Base | |||
def included(caller) | |||
caller.hooks ||= [] | |||
caller.hooks << name.demodulize.underscore.to_sym | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,11 @@ | |||
module SlackRubyBot | |||
module Hooks | |||
module Hello | |||
extend Base | |||
def hello(_client, _data) | |||
logger.info "Successfully connected to #{SlackRubyBot.config.url}." | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,49 @@ | |||
module SlackRubyBot | |||
module Hooks | |||
module Message | |||
extend Base | |||
def message(client, data) | |||
data = Hashie::Mash.new(data) | |||
data.text.strip! if data.text | |||
result = child_command_classes.detect { |d| d.invoke(client, data) } | |||
result ||= built_in_command_classes.detect { |d| d.invoke(client, data) } | |||
result ||= SlackRubyBot::Commands::Unknown.tap { |d| d.invoke(client, data) } | |||
result | |||
end | |||
private | |||
# | |||
# All commands. | |||
# | |||
# @return [Array] Descendants of SlackRubyBot::Commands::Base. | |||
# | |||
def command_classes | |||
SlackRubyBot::Commands::Base.descendants | |||
end | |||
# | |||
# All non-built-in, ie. custom commands. | |||
# | |||
# @return [Array] Non-built-in descendants of SlackRubyBot::Commands::Base. | |||
# | |||
def child_command_classes | |||
command_classes.reject do |k| | |||
k.name && k.name.starts_with?('SlackRubyBot::Commands::') | |||
end | |||
end | |||
# | |||
# All built-in commands. | |||
# | |||
# @return [Array] Built-in descendants of SlackRubyBot::Commands::Base. | |||
# | |||
def built_in_command_classes | |||
command_classes.select do |k| | |||
k.name && k.name.starts_with?('SlackRubyBot::Commands::') && k != SlackRubyBot::Commands::Unknown | |||
end | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,12 @@ | |||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..')) | |||
require 'rubygems' | |||
require 'rspec' | |||
require 'rack/test' | |||
require 'config/environment' | |||
require 'slack-ruby-bot' | |||
Dir[File.join(File.dirname(__FILE__), 'rspec/support', '**/*.rb')].each do |file| | |||
require file | |||
end |
@ -0,0 +1,16 @@ | |||
--- | |||
http_interactions: | |||
- request: | |||
method: post | |||
uri: https://slack.com/api/auth.test | |||
body: | |||
string: token=token | |||
response: | |||
status: | |||
code: 200 | |||
message: OK | |||
body: | |||
encoding: UTF-8 | |||
string: '{"ok":true,"url":"https:\/\/rubybot.slack.com\/","team":"team_name","user":"user_name","team_id":"TDEADBEEF","user_id":"UBAADFOOD"}' | |||
http_version: | |||
recorded_at: Tue, 28 Apr 2015 12:55:22 GMT |
@ -0,0 +1,30 @@ | |||
--- | |||
http_interactions: | |||
- request: | |||
method: post | |||
uri: https://slack.com/api/rtm.start | |||
body: | |||
string: token=token | |||
response: | |||
status: | |||
code: 200 | |||
message: OK | |||
body: | |||
encoding: UTF-8 | |||
string: '{"ok":false,"error":"migration_in_progress"}' | |||
http_version: | |||
recorded_at: Tue, 28 Apr 2015 12:55:22 GMT | |||
- request: | |||
method: post | |||
uri: https://slack.com/api/rtm.start | |||
body: | |||
string: token=token | |||
response: | |||
status: | |||
code: 200 | |||
message: OK | |||
body: | |||
encoding: UTF-8 | |||
string: '{"ok":false,"error":"unknown"}' | |||
http_version: | |||
recorded_at: Tue, 28 Apr 2015 12:55:22 GMT |
@ -0,0 +1,30 @@ | |||
shared_examples 'a slack ruby bot' do | |||
context 'not configured' do | |||
before do | |||
@slack_api_token = ENV.delete('SLACK_API_TOKEN') | |||
end | |||
after do | |||
ENV['SLACK_API_TOKEN'] = @slack_api_token | |||
end | |||
it 'requires SLACK_API_TOKEN' do | |||
expect { subject }.to raise_error RuntimeError, "Missing ENV['SLACK_API_TOKEN']." | |||
end | |||
end | |||
context 'configured', vcr: { cassette_name: 'auth_test' } do | |||
context 'run' do | |||
before do | |||
subject.send(:auth!) | |||
end | |||
after do | |||
subject.send(:reset!) | |||
end | |||
it 'succeeds auth' do | |||
expect(subject.config.url).to eq 'https://rubybot.slack.com/' | |||
expect(subject.config.team).to eq 'team_name' | |||
expect(subject.config.user).to eq 'user_name' | |||
expect(subject.config.team_id).to eq 'TDEADBEEF' | |||
expect(subject.config.user_id).to eq 'UBAADFOOD' | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,30 @@ | |||
require 'rspec/expectations' | |||
RSpec::Matchers.define :respond_with_error do |error, error_message| | |||
match do |actual| | |||
channel, user, message = parse(actual) | |||
allow(Giphy).to receive(:random) | |||
begin | |||
expect do | |||
client = app.send(:client) | |||
app.send(:message, client, text: message, channel: channel, user: user) | |||
end.to raise_error error, error_message | |||
rescue RSpec::Expectations::ExpectationNotMetError => e | |||
@error_message = e.message | |||
raise e | |||
end | |||
true | |||
end | |||
failure_message do |actual| | |||
_, _, message = parse(actual) | |||
@error_message || "expected for '#{message}' to fail with '#{expected}'" | |||
end | |||
private | |||
def parse(actual) | |||
actual = { message: actual } unless actual.is_a?(Hash) | |||
[actual[:channel] || 'channel', actual[:user] || 'user', actual[:message]] | |||
end | |||
end |
@ -0,0 +1,19 @@ | |||
require 'rspec/expectations' | |||
RSpec::Matchers.define :respond_with_slack_message do |expected| | |||
match do |actual| | |||
channel, user, message = parse(actual) | |||
allow(Giphy).to receive(:random) | |||
client = app.send(:client) | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: channel, text: expected) | |||
app.send(:message, client, text: message, channel: channel, user: user) | |||
true | |||
end | |||
private | |||
def parse(actual) | |||
actual = { message: actual } unless actual.is_a?(Hash) | |||
[actual[:channel] || 'channel', actual[:user] || 'user', actual[:message]] | |||
end | |||
end |
@ -0,0 +1,5 @@ | |||
RSpec.configure do |config| | |||
config.before :all do | |||
ENV['SLACK_API_TOKEN'] ||= 'test' | |||
end | |||
end |
@ -0,0 +1,9 @@ | |||
RSpec.configure do |config| | |||
config.before :each do | |||
SlackRubyBot.configure do |c| | |||
c.token = 'testtoken' | |||
c.user = 'rubybot' | |||
c.user_id = 'DEADBEEF' | |||
end | |||
end | |||
end |
@ -0,0 +1,8 @@ | |||
require 'vcr' | |||
VCR.configure do |config| | |||
config.cassette_library_dir = File.join(File.dirname(__FILE__), 'fixtures/slack') | |||
config.hook_into :webmock | |||
# config.default_cassette_options = { record: :new_episodes } | |||
config.configure_rspec_metadata! | |||
end |
@ -0,0 +1,3 @@ | |||
module SlackRubyBot | |||
VERSION = '0.4.4' | |||
end |
@ -0,0 +1 @@ | |||
require 'slack-ruby-bot' |
@ -0,0 +1,28 @@ | |||
$LOAD_PATH.push File.expand_path('../lib', __FILE__) | |||
require 'slack-ruby-bot/version' | |||
Gem::Specification.new do |s| | |||
s.name = 'slack-ruby-bot' | |||
s.version = SlackRubyBot::VERSION | |||
s.authors = ['Daniel Doubrovkine'] | |||
s.email = 'dblock@dblock.org' | |||
s.platform = Gem::Platform::RUBY | |||
s.required_rubygems_version = '>= 1.3.6' | |||
s.files = `git ls-files`.split("\n") | |||
s.test_files = `git ls-files -- spec/*`.split("\n") | |||
s.require_paths = ['lib'] | |||
s.homepage = 'http://github.com/dblock/slack-ruby-bot' | |||
s.licenses = ['MIT'] | |||
s.summary = 'The easiest way to write a Slack bot in Ruby.' | |||
s.add_dependency 'hashie' | |||
s.add_dependency 'slack-ruby-client' | |||
s.add_dependency 'activesupport' | |||
s.add_dependency 'giphy', '~> 2.0.2' | |||
s.add_dependency 'websocket-driver', '~> 0.5.4' | |||
s.add_development_dependency 'rake' | |||
s.add_development_dependency 'rspec' | |||
s.add_development_dependency 'rack-test' | |||
s.add_development_dependency 'vcr' | |||
s.add_development_dependency 'webmock' | |||
s.add_development_dependency 'rubocop', '0.32.1' | |||
end |
@ -0,0 +1,31 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::App do | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it_behaves_like 'a slack ruby bot' | |||
context 'retries on rtm.start errors' do | |||
let(:client) { Slack::RealTime::Client.new } | |||
let(:logger) { subject.send :logger } | |||
before do | |||
allow(subject).to receive(:sleep) | |||
expect(Slack::RealTime::Client).to receive(:new).twice.and_return(client) | |||
expect(logger).to receive(:error).twice | |||
end | |||
it 'migration_in_progress', vcr: { cassette_name: 'migration_in_progress' } do | |||
expect do | |||
subject.send :start! | |||
end.to raise_error Slack::Web::Api::Error, 'unknown' | |||
end | |||
[Faraday::Error::ConnectionFailed, Faraday::Error::TimeoutError, Faraday::Error::SSLError].each do |err| | |||
it "#{err}" do | |||
expect(client).to receive(:start!) { fail err, 'Faraday' } | |||
expect(client).to receive(:start!) { fail 'unknown' } | |||
expect do | |||
subject.send :start! | |||
end.to raise_error 'unknown' | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,19 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands::Default do | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'lowercase' do | |||
expect(message: SlackRubyBot.config.user).to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
it 'upcase' do | |||
expect(message: SlackRubyBot.config.user.upcase).to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
it 'name:' do | |||
expect(message: "#{SlackRubyBot.config.user}:").to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
it 'id' do | |||
expect(message: "<@#{SlackRubyBot.config.user_id}>").to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
end |
@ -0,0 +1,19 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot do | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
before do | |||
ENV['SLACK_RUBY_BOT_ALIASES'] = ':emoji: alias' | |||
end | |||
after do | |||
ENV.delete('SLACK_RUBY_BOT_ALIASES') | |||
end | |||
it 'responds to emoji' do | |||
expect(message: ':emoji: hi').to respond_with_slack_message('Hi <@user>!') | |||
end | |||
it 'responds to an alias' do | |||
expect(message: 'alias hi').to respond_with_slack_message('Hi <@user>!') | |||
end | |||
end |
@ -0,0 +1,22 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'tomato' do |client, data, match| | |||
send_message client, data.channel, "#{match[:command]}: #{match[:expression]}" | |||
end | |||
command 'tomatoes' do |client, data, match| | |||
send_message client, data.channel, "#{match[:command]}: #{match[:expression]}" | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'matches commands' do | |||
expect(message: "#{SlackRubyBot.config.user} tomato red").to respond_with_slack_message('tomato: red') | |||
expect(message: "#{SlackRubyBot.config.user} tomatoes green").to respond_with_slack_message('tomatoes: green') | |||
end | |||
end |
@ -0,0 +1,20 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'space' do |client, data, match| | |||
send_message client, data.channel, "#{match[:command]}: #{match[:expression]}" | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'matches leading spaces' do | |||
expect(message: " #{SlackRubyBot.config.user} space red").to respond_with_slack_message('space: red') | |||
end | |||
it 'matches trailing spaces' do | |||
expect(message: "#{SlackRubyBot.config.user} space red ").to respond_with_slack_message('space: red') | |||
end | |||
end |
@ -0,0 +1,21 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'sayhi' | |||
command 'saybye' | |||
def self.call(client, data, match) | |||
send_message client, data.channel, "#{match[:command]}: #{match[:expression]}" | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'supports multiple commands' do | |||
expect(message: "#{SlackRubyBot.config.user} sayhi arg1 arg2").to respond_with_slack_message('sayhi: arg1 arg2') | |||
expect(message: "#{SlackRubyBot.config.user} saybye arg1 arg2").to respond_with_slack_message('saybye: arg1 arg2') | |||
end | |||
end |
@ -0,0 +1,23 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'sayhi' do |client, data, match| | |||
send_message client, data.channel, "#{match[:command]}: #{match[:expression]}" | |||
end | |||
command 'one', 'two' do |client, data, match| | |||
send_message client, data.channel, "#{match[:command]}: #{match[:expression]}" | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'supports multiple commands' do | |||
expect(message: "#{SlackRubyBot.config.user} sayhi arg1 arg2").to respond_with_slack_message('sayhi: arg1 arg2') | |||
expect(message: "#{SlackRubyBot.config.user} one arg1 arg2").to respond_with_slack_message('one: arg1 arg2') | |||
expect(message: "#{SlackRubyBot.config.user} two arg1 arg2").to respond_with_slack_message('two: arg1 arg2') | |||
end | |||
end |
@ -0,0 +1,38 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot do | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
context 'it responds to direct messages' do | |||
it 'with bot name' do | |||
expect(message: "#{SlackRubyBot.config.user} hi", channel: 'DEADBEEF').to respond_with_slack_message('Hi <@user>!') | |||
end | |||
it 'with bot name capitalized' do | |||
expect(message: "#{SlackRubyBot.config.user.upcase} hi", channel: 'DEADBEEF').to respond_with_slack_message('Hi <@user>!') | |||
end | |||
it 'with bot user id' do | |||
expect(message: "<@#{SlackRubyBot.config.user_id}> hi", channel: 'DEADBEEF').to respond_with_slack_message('Hi <@user>!') | |||
end | |||
it 'without bot name' do | |||
expect(message: 'hi', channel: 'DEADBEEF').to respond_with_slack_message('Hi <@user>!') | |||
end | |||
end | |||
context 'it responds to direct name calling' do | |||
it 'with bot name' do | |||
expect(message: "#{SlackRubyBot.config.user}", channel: 'DEADBEEF').to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
it 'with bot name capitalized' do | |||
expect(message: "#{SlackRubyBot.config.user.upcase}", channel: 'DEADBEEF').to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
it 'with bot user id' do | |||
expect(message: "<@#{SlackRubyBot.config.user_id}>", channel: 'DEADBEEF').to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
it 'with bot user id and a colon' do | |||
expect(message: "<@#{SlackRubyBot.config.user_id}>:", channel: 'DEADBEEF').to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
it 'with bot user id and a colon and a space' do | |||
expect(message: "<@#{SlackRubyBot.config.user_id}>: ", channel: 'DEADBEEF').to respond_with_slack_message(SlackRubyBot::ABOUT) | |||
end | |||
end | |||
end |
@ -0,0 +1,22 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'empty_text' | |||
def self.call(client, data, _match) | |||
send_message client, data.channel, nil | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
let(:client) { app.send(:client) } | |||
it 'sends default text' do | |||
allow(Giphy).to receive(:random) | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_message_with_gif).with(client, 'channel', 'Nothing to see here.', 'nothing', {}) | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} empty_text", channel: 'channel', user: 'user') | |||
end | |||
end |
@ -0,0 +1,10 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands::Help do | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'help' do | |||
expect(message: "#{SlackRubyBot.config.user} help").to respond_with_slack_message('See https://github.com/dblock/slack-ruby-bot, please.') | |||
end | |||
end |
@ -0,0 +1,19 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands::Hi do | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'says hi' do | |||
expect(message: "#{SlackRubyBot.config.user} hi").to respond_with_slack_message('Hi <@user>!') | |||
end | |||
it 'says hi to bot:' do | |||
expect(message: "#{SlackRubyBot.config.user}: hi").to respond_with_slack_message('Hi <@user>!') | |||
end | |||
it 'says hi to @bot' do | |||
expect(message: "<@#{SlackRubyBot.config.user_id}> hi").to respond_with_slack_message('Hi <@user>!') | |||
end | |||
it 'says hi to @bot: ' do | |||
expect(message: "<@#{SlackRubyBot.config.user_id}>: hi").to respond_with_slack_message('Hi <@user>!') | |||
end | |||
end |
@ -0,0 +1,22 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'nil_text' | |||
def self.call(_client, data, _match) | |||
send_message cilent, data.channel, nil | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
let(:client) { app.send(:client) } | |||
it 'ignores nil messages' do | |||
allow(Giphy).to receive(:random) | |||
expect(SlackRubyBot::Commands::Base).to_not receive(:send_message_with_gif) | |||
app.send(:message, client, text: nil, channel: 'channel', user: 'user') | |||
end | |||
end |
@ -0,0 +1,15 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands::Base do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'not_implemented' | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'raises not implemented' do | |||
expect(message: "#{SlackRubyBot.config.user} not_implemented").to respond_with_error(NotImplementedError, 'rubybot not_implemented') | |||
end | |||
end |
@ -0,0 +1,20 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
operator '=' | |||
operator '-' | |||
def self.call(client, data, match) | |||
send_message client, data.channel, "#{match[:operator]}: #{match[:expression]}" | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'supports operators' do | |||
expect(message: '=2+2').to respond_with_slack_message('=: 2+2') | |||
end | |||
end |
@ -0,0 +1,23 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
operator '=' do |client, data, match| | |||
send_message client, data.channel, "#{match[:operator]}: #{match[:expression]}" | |||
end | |||
operator '+', '-' do |client, data, match| | |||
send_message client, data.channel, "#{match[:operator]}: #{match[:expression]}" | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'supports operator blocks' do | |||
expect(message: '=2+2').to respond_with_slack_message('=: 2+2') | |||
expect(message: '+2+2').to respond_with_slack_message('+: 2+2') | |||
expect(message: '-2+2').to respond_with_slack_message('-: 2+2') | |||
end | |||
end |
@ -0,0 +1,32 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'send_gif_spec' do |client, data, _match| | |||
send_gif client, data.channel, 'dummy' | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
let(:client) { app.send(:client) } | |||
let(:gif_image_url) { 'http://media2.giphy.com/media/pzOijFsdDrsS4/giphy.gif' } | |||
it 'sends a gif' do | |||
gif = Giphy::RandomGif.new('image_url' => gif_image_url) | |||
expect(Giphy).to receive(:random).and_return(gif) | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: gif_image_url) | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_gif_spec message", channel: 'channel', user: 'user') | |||
end | |||
it 'eats up the error' do | |||
expect(Giphy).to receive(:random) { fail 'oh no!' } | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: '') | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_gif_spec message", channel: 'channel', user: 'user') | |||
end | |||
it 'eats up nil gif' do | |||
expect(Giphy).to receive(:random).and_return(nil) | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: '') | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_gif_spec message", channel: 'channel', user: 'user') | |||
end | |||
end |
@ -0,0 +1,19 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'send_message_spec' do |client, data, match| | |||
send_message client, data.channel, match['expression'] | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
let(:client) { app.send(:client) } | |||
it 'sends a message' do | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: 'message') | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_message_spec message", channel: 'channel', user: 'user') | |||
end | |||
end |
@ -0,0 +1,32 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'send_message_with_gif_spec' do |client, data, match| | |||
send_message_with_gif client, data.channel, match['expression'], 'dummy' | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
let(:client) { app.send(:client) } | |||
let(:gif_image_url) { 'http://media2.giphy.com/media/pzOijFsdDrsS4/giphy.gif' } | |||
it 'sends a message with gif' do | |||
gif = Giphy::RandomGif.new('image_url' => gif_image_url) | |||
expect(Giphy).to receive(:random).and_return(gif) | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: "message\n#{gif_image_url}") | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_message_with_gif_spec message", channel: 'channel', user: 'user') | |||
end | |||
it 'eats up the error' do | |||
expect(Giphy).to receive(:random) { fail 'oh no!' } | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: 'message') | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_message_with_gif_spec message", channel: 'channel', user: 'user') | |||
end | |||
it 'eats up nil gif' do | |||
expect(Giphy).to receive(:random).and_return(nil) | |||
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: 'message') | |||
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_message_with_gif_spec message", channel: 'channel', user: 'user') | |||
end | |||
end |
@ -0,0 +1,15 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot::Commands::Unknown do | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
let(:client) { app.send(:client) } | |||
it 'invalid command' do | |||
expect(message: "#{SlackRubyBot.config.user} foobar").to respond_with_slack_message("Sorry <@user>, I don't understand that command!") | |||
end | |||
it 'does not respond to sad face' do | |||
expect(SlackRubyBot::Commands::Base).to_not receive(:send_message) | |||
SlackRubyBot::App.new.send(:message, client, text: ':((') | |||
end | |||
end |
@ -0,0 +1,19 @@ | |||
require 'spec_helper' | |||
describe RSpec do | |||
let! :command do | |||
Class.new(SlackRubyBot::Commands::Base) do | |||
command 'raise' | |||
def self.call(_client, _data, match) | |||
fail ArgumentError, match[:command] | |||
end | |||
end | |||
end | |||
def app | |||
SlackRubyBot::App.new | |||
end | |||
it 'respond_with_error' do | |||
expect(message: "#{SlackRubyBot.config.user} raise").to respond_with_error(ArgumentError, 'raise') | |||
end | |||
end |
@ -0,0 +1,7 @@ | |||
require 'spec_helper' | |||
describe SlackRubyBot do | |||
it 'has a version' do | |||
expect(SlackRubyBot::VERSION).to_not be nil | |||
end | |||
end |
@ -0,0 +1 @@ | |||
require 'slack-ruby-bot/rspec' |