├── .gitignore ├── .rspec ├── .travis.yml ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── images └── preview.gif ├── lib ├── simple_slack_bot.rb └── simple_slack_bot │ ├── client.rb │ ├── command.rb │ ├── config.rb │ └── version.rb ├── simple-slack-bot.gemspec └── spec ├── client_spec.rb ├── command_spec.rb ├── config_spec.rb ├── spec_helper.rb └── version_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.gem 3 | /.bundle/ 4 | /.yardoc 5 | /Gemfile.lock 6 | /_yardoc/ 7 | /coverage/ 8 | /doc/ 9 | /pkg/ 10 | /spec/reports/ 11 | /tmp/ 12 | *.bundle 13 | *.so 14 | *.o 15 | *.a 16 | mkmf.log -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format documentation -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - "2.0.0" 4 | - "2.2.0" 5 | - "2.2.2" 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Lee Sun-Hyoup 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleSlackBot 2 | [![Build Status](https://travis-ci.org/kciter/simple-slack-bot.svg?branch=master)](https://travis-ci.org/kciter/simple-slack-bot) 3 | 4 | This library wrapping [slack-ruby-client](https://github.com/dblock/slack-ruby-client) library 5 | 6 | ## Preview 7 | Preview gif 8 | 9 | ## Installation 10 | Add this line to your application's Gemfile: 11 | ```ruby 12 | gem 'simple-slack-bot' 13 | ``` 14 | And then execute: 15 | ``` 16 | $ bundle 17 | ``` 18 | Or install it yourself as: 19 | ``` 20 | $ gem install simple-slack-bot 21 | ``` 22 | 23 | ## Simple Usage 24 | This is simple! 25 | ```ruby 26 | require 'simple-slack-bot' 27 | 28 | bot = SlackBot::Client.new 29 | 30 | bot.configure do |config| 31 | config.join_message = 'Hello!' 32 | config.debug = true # 33 | config.token = 'YOUR SLACK BOT TOKEN' 34 | end 35 | 36 | bot.add_command /Hello/ do |data| 37 | bot.message(data['channel'], "Hello. <@#{data['user']}>!") 38 | end 39 | 40 | bot.start! 41 | ``` 42 | Refer to the Slack event message for [data](https://api.slack.com/events/message) 43 | 44 | ## License 45 | The MIT License (MIT) 46 | 47 | Copyright (c) 2016 Lee Sun-Hyoup 48 | 49 | Permission is hereby granted, free of charge, to any person obtaining a copy 50 | of this software and associated documentation files (the "Software"), to deal 51 | in the Software without restriction, including without limitation the rights 52 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 53 | copies of the Software, and to permit persons to whom the Software is 54 | furnished to do so, subject to the following conditions: 55 | 56 | The above copyright notice and this permission notice shall be included in all 57 | copies or substantial portions of the Software. 58 | 59 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 60 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 61 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 62 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 63 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 64 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 65 | SOFTWARE. 66 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler' 3 | require 'bundler/gem_tasks' 4 | require 'rspec/core/rake_task' 5 | 6 | task :default => :spec 7 | RSpec::Core::RakeTask.new -------------------------------------------------------------------------------- /images/preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kciter/simple-slack-bot/c77c17806907d6f58c05b6c9b1f2a1f058d17387/images/preview.gif -------------------------------------------------------------------------------- /lib/simple_slack_bot.rb: -------------------------------------------------------------------------------- 1 | require 'simple_slack_bot/command' 2 | require 'simple_slack_bot/config' 3 | require 'simple_slack_bot/client' 4 | -------------------------------------------------------------------------------- /lib/simple_slack_bot/client.rb: -------------------------------------------------------------------------------- 1 | require 'slack-ruby-client' 2 | 3 | module SlackBot 4 | class Client 5 | attr_accessor :slack_web_client, :slack_realtime_client 6 | attr_accessor :commands 7 | attr_accessor(*Config::ATTRIBUTES) 8 | 9 | def initialize 10 | @commands = [] 11 | end 12 | 13 | def add_command(regex) 14 | command = Command.new(self) 15 | command.regex = regex 16 | command.action = -> (data) { yield(data) } 17 | @commands << command 18 | end 19 | 20 | def start! 21 | if config.token.nil? 22 | puts 'You must setting a token.' 23 | return 24 | end 25 | 26 | slack_init 27 | message_event_init 28 | 29 | EM.run do 30 | @slack_realtime_client.start! 31 | end 32 | end 33 | 34 | def config 35 | Config 36 | end 37 | 38 | def configure 39 | block_given? ? yield(Config) : Config 40 | end 41 | 42 | def web_message(channel, text) 43 | @slack_web_client.chat_postMessage(channel: channel, text: text, as_user: true) 44 | end 45 | 46 | def message(channel, text) 47 | @slack_realtime_client.message channel: channel, text: text 48 | end 49 | 50 | protected 51 | 52 | def slack_init 53 | Slack.configure do |config| 54 | config.token = self.config.token 55 | end 56 | 57 | Slack::RealTime.configure do |config| 58 | config.concurrency = Slack::RealTime::Concurrency::Eventmachine 59 | end 60 | 61 | @slack_web_client = Slack::Web::Client.new 62 | @slack_realtime_client = Slack::RealTime::Client.new 63 | end 64 | 65 | def message_event_init 66 | client = @slack_realtime_client 67 | client.on :message do |data| 68 | File.open('bot.log', 'a') { |file| file.write(data.to_s + "\n") } 69 | puts data if config.debug.eql?(true) 70 | 71 | case data['subtype'] 72 | when 'channel_join' then 73 | client.message channel: data['channel'], text: config.join_message 74 | end 75 | 76 | @commands.each do |command| 77 | command.execute(data) if command.match?(data['text']) 78 | end 79 | end 80 | end 81 | end 82 | end 83 | -------------------------------------------------------------------------------- /lib/simple_slack_bot/command.rb: -------------------------------------------------------------------------------- 1 | module SlackBot 2 | class Command 3 | attr_accessor :bot_client, :regex, :help_message, :action 4 | 5 | def initialize(bot_client) 6 | @bot_client = bot_client 7 | end 8 | 9 | def match?(str) 10 | (@regex.nil? || @regex.match(str).nil?) ? false : true 11 | end 12 | 13 | def execute(data) 14 | return if @action.nil? 15 | @action.call(data) 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/simple_slack_bot/config.rb: -------------------------------------------------------------------------------- 1 | module SlackBot 2 | module Config 3 | extend self 4 | 5 | ATTRIBUTES = [ 6 | :debug, 7 | :join_message, 8 | :token 9 | ] 10 | 11 | attr_accessor(*Config::ATTRIBUTES) 12 | 13 | self.debug = false 14 | self.join_message = 'Hello!' 15 | self.token = 'TOKEN' 16 | end 17 | 18 | module_function 19 | 20 | def configure 21 | block_given? ? yield(Config) : Config 22 | end 23 | 24 | def config 25 | Config 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/simple_slack_bot/version.rb: -------------------------------------------------------------------------------- 1 | module SlackBot 2 | VERSION = '0.1.2' 3 | end -------------------------------------------------------------------------------- /simple-slack-bot.gemspec: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.push File.expand_path('../lib', __FILE__) 2 | require 'simple_slack_bot/version' 3 | 4 | Gem::Specification.new do |s| 5 | s.name = 'simple-slack-bot' 6 | s.version = SlackBot::VERSION 7 | s.authors = ['Lee Sun-Hyoup'] 8 | s.email = ['kciter@naver.com'] 9 | 10 | s.files = `git ls-files`.split("\n") 11 | s.homepage = 'http://github.com/kciter/simple-slack-bot' 12 | s.licenses = ['MIT'] 13 | s.summary = %q{Simple Slack Bot for Ruby.} 14 | s.description = %q{You can easily make Slack Bot!!!} 15 | 16 | s.add_dependency 'slack-ruby-client', '~> 0.5.3' 17 | s.add_dependency 'eventmachine', '~> 1.0', '>= 1.0.9' 18 | s.add_dependency 'faye-websocket', '~> 0.10.2' 19 | # s.add_dependency 'rufus-scheduler', '~> 3.2.0' 20 | 21 | s.add_development_dependency 'bundler' 22 | s.add_development_dependency 'rake' 23 | s.add_development_dependency 'rspec' 24 | end -------------------------------------------------------------------------------- /spec/client_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe SlackBot::Client do 4 | describe '#initialize' do 5 | it 'have an empty commands array' do 6 | expect(SlackBot::Client.new.commands).to eq [] 7 | end 8 | end 9 | 10 | describe '.add_command' do 11 | bot = SlackBot::Client.new 12 | 13 | it 'add command to commands array' do 14 | result = bot.add_command('Hello') 15 | expect(result).to eq bot.commands 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /spec/command_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe SlackBot::Command do 4 | describe '#initialize' do 5 | bot = SlackBot::Client.new 6 | 7 | it 'have a bot_client' do 8 | command = SlackBot::Command.new(bot) 9 | expect(command.bot_client).to eq bot 10 | end 11 | end 12 | 13 | describe '.match?' do 14 | bot = SlackBot::Client.new 15 | bot.add_command('Hello') 16 | 17 | it 'iterate commands' do 18 | bot.commands.each do |command| 19 | expect(command.regex).to eq 'Hello' 20 | end 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /spec/config_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe SlackBot::Config do 4 | describe '#initialize' do 5 | it 'sets config' do 6 | expect(SlackBot.config.join_message).to eq 'Hello!' 7 | expect(SlackBot.config.debug).to eq false 8 | expect(SlackBot.config.token).to eq 'TOKEN' 9 | end 10 | end 11 | 12 | describe '#configure' do 13 | before do 14 | SlackBot.configure do |config| 15 | config.join_message = 'Hi!' 16 | config.debug = true 17 | config.token = 'Invalid Token' 18 | end 19 | end 20 | 21 | it 'sets config' do 22 | expect(SlackBot.config.join_message).to eq 'Hi!' 23 | expect(SlackBot.config.debug).to eq true 24 | expect(SlackBot.config.token).to eq 'Invalid Token' 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'bundler/setup' 2 | Bundler.setup 3 | 4 | require 'simple_slack_bot' 5 | 6 | RSpec.configure do |config| 7 | end 8 | -------------------------------------------------------------------------------- /spec/version_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe SlackBot do 4 | it 'has a version' do 5 | expect(SlackBot::VERSION).to_not be nil 6 | end 7 | end --------------------------------------------------------------------------------