A Slack Bot that pulls Pixiv information and posts the full image(s) into Slack, with iOS shortcuts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.2 KiB

9 years ago
  1. Slack-Ruby-Bot
  2. ==============
  3. [![Gem Version](https://badge.fury.io/rb/slack-ruby-bot.svg)](http://badge.fury.io/rb/slack-ruby-bot)
  4. [![Build Status](https://travis-ci.org/dblock/slack-ruby-bot.png)](https://travis-ci.org/dblock/slack-ruby-bot)
  5. 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.
  6. ## Usage
  7. ### A Minimal Bot
  8. #### Gemfile
  9. ```ruby
  10. source 'http://rubygems.org'
  11. gem 'slack-ruby-bot'
  12. ```
  13. #### pongbot.rb
  14. ```ruby
  15. require 'slack-ruby-bot'
  16. module PongBot
  17. class App < SlackRubyBot::App
  18. end
  19. class Ping < SlackRubyBot::Commands::Base
  20. command 'ping' do |client, data, _match|
  21. client.message text: 'pong', channel: data.channel
  22. end
  23. end
  24. end
  25. PongBot::App.instance.run
  26. ```
  27. 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.
  28. ![](screenshots/demo.gif)
  29. ### A Production Bot
  30. 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.
  31. ### More Involved Examples
  32. The following examples of production-grade bots based on slack-ruby-bot are listed in growing order of complexity.
  33. * [slack-mathbot](https://github.com/dblock/slack-mathbot): Slack integration with math.
  34. * [slack-google-bot](https://github.com/dblock/slack-google-bot): A Slack bot that searches Google, including CSE.
  35. * [slack-aws](https://github.com/dblock/slack-aws): Slack integration with Amazon Web Services.
  36. * [slack-gamebot](https://github.com/dblock/slack-gamebot): A generic game bot for ping-pong, chess, etc.
  37. ### Commands and Operators
  38. 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.
  39. ```ruby
  40. class Phone < SlackRubyBot::Commands::Base
  41. command 'call'
  42. def self.call(client, data, _match)
  43. send_message client, data.channel, 'called'
  44. end
  45. end
  46. ```
  47. 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).
  48. ```ruby
  49. class Phone < SlackRubyBot::Commands::Base
  50. command 'call'
  51. command '呼び出し'
  52. def self.call(client, data, _match)
  53. send_message client, data.channel, 'called'
  54. end
  55. end
  56. ```
  57. You can combine multiple commands and use a block to implement them.
  58. ```ruby
  59. class Phone < SlackRubyBot::Commands::Base
  60. command 'call', '呼び出し' do |client, data, _match|
  61. send_message client, data.channel, 'called'
  62. end
  63. end
  64. ```
  65. 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.
  66. 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`.
  67. ```ruby
  68. class Calculator < SlackRubyBot::Commands::Base
  69. operator '=' do |_data, _match|
  70. # implementation detail
  71. end
  72. end
  73. ```
  74. Operator match data includes `match['operator']` and `match['expression']`. The `bot` match always checks against the `SlackRubyBot::Config.user` setting.
  75. ### Bot Aliases
  76. 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.
  77. ```
  78. SLACK_RUBY_BOT_ALIASES=:pp: table-tennis
  79. ```
  80. ```ruby
  81. SlackRubyBot.configure do |config|
  82. config.aliases = [':pong:', 'pongbot']
  83. end
  84. ```
  85. This is particularly fun with emoji.
  86. ![](screenshots/aliases.gif)
  87. Bots also will respond to a direct message, with or without the bot name in the message itself.
  88. ![](screenshots/dms.gif)
  89. ### Generic Routing
  90. Commands and operators are generic versions of bot routes. You can respond to just about anything by defining a custom route.
  91. ```ruby
  92. class Weather < SlackRubyBot::Commands::Base
  93. match /^How is the weather in (?<location>\w*)\?$/ do |client, data, match|
  94. send_message client, data.channel, "The weather in #{match[:location]} is nice."
  95. end
  96. end
  97. ```
  98. ![](screenshots/weather.gif)
  99. ### SlackRubyBot::Commands::Base Functions
  100. #### send_message(client, channel, text)
  101. Send text using a RealTime client to a channel.
  102. #### send_message_with_gif(client, channel, text, keyword)
  103. Send text along with a random animated GIF based on a keyword.
  104. ## send_gif(client, channel, keyword)
  105. Send a random animated GIF based on a keyword.
  106. ### Built-In Commands
  107. Slack-ruby-bot comes with several built-in commands. You can re-define built-in commands, normally, as described above.
  108. #### [bot name]
  109. This is also known as the `default` command. Shows bot version and links.
  110. #### [bot name] hi
  111. Politely says 'hi' back.
  112. #### [bot name] help
  113. Get help.
  114. ### Hooks
  115. 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).
  116. 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.
  117. ```ruby
  118. module MyBot
  119. module Hooks
  120. module UserChange
  121. extend SlackRubyBot::Hooks::Base
  122. def user_change(client, data)
  123. # data['user']['id'] contains the user ID
  124. # data['user']['name'] contains the new user name
  125. ...
  126. end
  127. end
  128. end
  129. end
  130. ```
  131. ### RSpec Shared Behaviors
  132. 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`.
  133. * [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.
  134. * [respond with slack message](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb): The bot responds with a message.
  135. * [respond with error](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb): An exception is raised inside a bot command.
  136. ## Contributing
  137. See [CONTRIBUTING](CONTRIBUTING.md).
  138. ## Upgrading
  139. See [CHANGELOG](CHANGELOG.md) for a history of changes and [UPGRADING](UPGRADING.md) for how to upgrade to more recent versions.
  140. ## Copyright and License
  141. Copyright (c) 2015, [Daniel Doubrovkine](https://twitter.com/dblockdotorg), [Artsy](https://www.artsy.net) and [Contributors](CHANGELOG.md).
  142. This project is licensed under the [MIT License](LICENSE.md).