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.

203 lines
5.0 KiB

9 years ago
  1. ## Production Bot Tutorial
  2. In this tutorial we'll implement [slack-mathbot](https://github.com/dblock/slack-mathbot).
  3. ### Introduction
  4. 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`.
  5. ### Getting Started
  6. #### Gemfile
  7. 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).
  8. ```ruby
  9. source 'http://rubygems.org'
  10. gem 'slack-ruby-bot'
  11. gem 'puma'
  12. gem 'sinatra'
  13. group :development, :test do
  14. gem 'rake'
  15. gem 'foreman'
  16. end
  17. group :test do
  18. gem 'rspec'
  19. gem 'rack-test'
  20. end
  21. ```
  22. Run `bundle install` to get all the gems.
  23. #### Application
  24. Create a folder called `slack-mathbot` and inside of it create `app.rb`.
  25. ```ruby
  26. module SlackMathbot
  27. class App < SlackRubyBot::App
  28. end
  29. end
  30. ```
  31. #### Commands
  32. Create a folder called `slack-mathbot/commands` and inside of it create `calculate.rb`. For now this calculator will always return 4.
  33. ```ruby
  34. module SlackMathbot
  35. module Commands
  36. class Calculate < SlackRubyBot::Commands::Base
  37. command 'calculate' do |client, data, _match|
  38. send_message client, data.channel, '4'
  39. end
  40. end
  41. end
  42. end
  43. ```
  44. #### Require Everything
  45. Create a `slack-mathbot.rb` at the root and require the above files.
  46. ```ruby
  47. require 'slack-ruby-bot'
  48. require 'slack-mathbot/commands/calculate'
  49. require 'slack-mathbot/app'
  50. ```
  51. #### Web Server
  52. We will need to keep the bot alive on Heroku, so create `web.rb`.
  53. ```ruby
  54. require 'sinatra/base'
  55. module SlackMathbot
  56. class Web < Sinatra::Base
  57. get '/' do
  58. 'Math is good for you.'
  59. end
  60. end
  61. end
  62. ```
  63. #### Config.ru
  64. Tie all the pieces togehter in `config.ru` which creates a thread for the bot and runs the web server on the main thread.
  65. ```ruby
  66. $LOAD_PATH.unshift(File.dirname(__FILE__))
  67. require 'slack-mathbot'
  68. require 'web'
  69. Thread.new do
  70. begin
  71. SlackMathbot::App.instance.run
  72. rescue Exception => e
  73. STDERR.puts "ERROR: #{e}"
  74. STDERR.puts e.backtrace
  75. raise e
  76. end
  77. end
  78. run SlackMathbot::Web
  79. ```
  80. ### Create a Bot User
  81. In Slack administration create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot).
  82. ![](screenshots/register-bot.png)
  83. On the next screen, note the API token.
  84. #### .env
  85. Create a `.env` file with the API token from above and make sure to add it to `.gitignore`.
  86. ```
  87. SLACK_API_TOKEN=...
  88. ```
  89. ### Procfile
  90. Create a `Procfile` which `foreman` will use when you run the `foreman start` command below.
  91. ```
  92. web: bundle exec puma -p $PORT
  93. ```
  94. ### Run the Bot
  95. Run `foreman start`. Your bot should be running.
  96. ```
  97. 14:32:32 web.1 | Puma starting in single mode...
  98. 14:32:32 web.1 | * Version 2.11.3 (ruby 2.1.6-p336), codename: Intrepid Squirrel
  99. 14:32:32 web.1 | * Min threads: 0, max threads: 16
  100. 14:32:32 web.1 | * Environment: development
  101. 14:32:35 web.1 | * Listening on tcp://0.0.0.0:5000
  102. 14:32:35 web.1 | Use Ctrl-C to stop
  103. 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/.
  104. 14:32:36 web.1 | I, [2015-07-10T14:32:36.766955 #98948] INFO -- : Successfully connected to https://xyz.slack.com/.
  105. ```
  106. ### Try
  107. 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.
  108. ### Write Tests
  109. #### Spec Helper
  110. Create `spec/spec_helper.rb` that includes the bot files and shared RSpec support from slack-ruby-bot.
  111. ```ruby
  112. $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
  113. require 'slack-ruby-bot/rspec'
  114. require 'slack-mathbot'
  115. ```
  116. #### Test the Bot Application
  117. Create a test for the bot application itself in `spec/slack-mathbot/app_spec.rb`.
  118. ```ruby
  119. require 'spec_helper'
  120. describe SlackMathbot::App do
  121. def app
  122. SlackMathbot::App.new
  123. end
  124. it_behaves_like 'a slack ruby bot'
  125. end
  126. ```
  127. #### Test a Command
  128. Create a test for the `calculate` command in `spec/slack-mathbot/commands/calculate_spec.rb`. The bot is addressed by its user name.
  129. ```ruby
  130. require 'spec_helper'
  131. describe SlackMathbot::Commands::Calculate do
  132. def app
  133. SlackMathbot::App.new
  134. end
  135. it 'returns 4' do
  136. expect(message: "#{SlackRubyBot.config.user} calculate 2+2", channel: 'channel').to respond_with_slack_message('4')
  137. end
  138. end
  139. ```
  140. See [lib/slack-ruby-bot/rspec/support/slack-ruby-bot](lib/slack-ruby-bot/rspec/support/slack-ruby-bot) for other shared RSpec behaviors.
  141. ### Deploy
  142. See [DEPLOYMENT](DEPLOYMENT.md) for how to deploy your bot to production.