Repository: kciter/simple-slack-bot Branch: master Commit: c77c17806907 Files: 18 Total size: 8.6 KB Directory structure: gitextract_l28ift1u/ ├── .gitignore ├── .rspec ├── .travis.yml ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── lib/ │ ├── simple_slack_bot/ │ │ ├── client.rb │ │ ├── command.rb │ │ ├── config.rb │ │ └── version.rb │ └── simple_slack_bot.rb ├── simple-slack-bot.gemspec └── spec/ ├── client_spec.rb ├── command_spec.rb ├── config_spec.rb ├── spec_helper.rb └── version_spec.rb ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .idea *.gem /.bundle/ /.yardoc /Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ *.bundle *.so *.o *.a mkmf.log ================================================ FILE: .rspec ================================================ --color --format documentation ================================================ FILE: .travis.yml ================================================ language: ruby rvm: - "2.0.0" - "2.2.0" - "2.2.2" ================================================ FILE: Gemfile ================================================ source 'https://rubygems.org' gemspec ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2016 Lee Sun-Hyoup 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. ================================================ FILE: README.md ================================================ # SimpleSlackBot [![Build Status](https://travis-ci.org/kciter/simple-slack-bot.svg?branch=master)](https://travis-ci.org/kciter/simple-slack-bot) This library wrapping [slack-ruby-client](https://github.com/dblock/slack-ruby-client) library ## Preview Preview gif ## Installation Add this line to your application's Gemfile: ```ruby gem 'simple-slack-bot' ``` And then execute: ``` $ bundle ``` Or install it yourself as: ``` $ gem install simple-slack-bot ``` ## Simple Usage This is simple! ```ruby require 'simple-slack-bot' bot = SlackBot::Client.new bot.configure do |config| config.join_message = 'Hello!' config.debug = true # config.token = 'YOUR SLACK BOT TOKEN' end bot.add_command /Hello/ do |data| bot.message(data['channel'], "Hello. <@#{data['user']}>!") end bot.start! ``` Refer to the Slack event message for [data](https://api.slack.com/events/message) ## License The MIT License (MIT) Copyright (c) 2016 Lee Sun-Hyoup 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. ================================================ FILE: Rakefile ================================================ require 'rubygems' require 'bundler' require 'bundler/gem_tasks' require 'rspec/core/rake_task' task :default => :spec RSpec::Core::RakeTask.new ================================================ FILE: lib/simple_slack_bot/client.rb ================================================ require 'slack-ruby-client' module SlackBot class Client attr_accessor :slack_web_client, :slack_realtime_client attr_accessor :commands attr_accessor(*Config::ATTRIBUTES) def initialize @commands = [] end def add_command(regex) command = Command.new(self) command.regex = regex command.action = -> (data) { yield(data) } @commands << command end def start! if config.token.nil? puts 'You must setting a token.' return end slack_init message_event_init EM.run do @slack_realtime_client.start! end end def config Config end def configure block_given? ? yield(Config) : Config end def web_message(channel, text) @slack_web_client.chat_postMessage(channel: channel, text: text, as_user: true) end def message(channel, text) @slack_realtime_client.message channel: channel, text: text end protected def slack_init Slack.configure do |config| config.token = self.config.token end Slack::RealTime.configure do |config| config.concurrency = Slack::RealTime::Concurrency::Eventmachine end @slack_web_client = Slack::Web::Client.new @slack_realtime_client = Slack::RealTime::Client.new end def message_event_init client = @slack_realtime_client client.on :message do |data| File.open('bot.log', 'a') { |file| file.write(data.to_s + "\n") } puts data if config.debug.eql?(true) case data['subtype'] when 'channel_join' then client.message channel: data['channel'], text: config.join_message end @commands.each do |command| command.execute(data) if command.match?(data['text']) end end end end end ================================================ FILE: lib/simple_slack_bot/command.rb ================================================ module SlackBot class Command attr_accessor :bot_client, :regex, :help_message, :action def initialize(bot_client) @bot_client = bot_client end def match?(str) (@regex.nil? || @regex.match(str).nil?) ? false : true end def execute(data) return if @action.nil? @action.call(data) end end end ================================================ FILE: lib/simple_slack_bot/config.rb ================================================ module SlackBot module Config extend self ATTRIBUTES = [ :debug, :join_message, :token ] attr_accessor(*Config::ATTRIBUTES) self.debug = false self.join_message = 'Hello!' self.token = 'TOKEN' end module_function def configure block_given? ? yield(Config) : Config end def config Config end end ================================================ FILE: lib/simple_slack_bot/version.rb ================================================ module SlackBot VERSION = '0.1.2' end ================================================ FILE: lib/simple_slack_bot.rb ================================================ require 'simple_slack_bot/command' require 'simple_slack_bot/config' require 'simple_slack_bot/client' ================================================ FILE: simple-slack-bot.gemspec ================================================ $LOAD_PATH.push File.expand_path('../lib', __FILE__) require 'simple_slack_bot/version' Gem::Specification.new do |s| s.name = 'simple-slack-bot' s.version = SlackBot::VERSION s.authors = ['Lee Sun-Hyoup'] s.email = ['kciter@naver.com'] s.files = `git ls-files`.split("\n") s.homepage = 'http://github.com/kciter/simple-slack-bot' s.licenses = ['MIT'] s.summary = %q{Simple Slack Bot for Ruby.} s.description = %q{You can easily make Slack Bot!!!} s.add_dependency 'slack-ruby-client', '~> 0.5.3' s.add_dependency 'eventmachine', '~> 1.0', '>= 1.0.9' s.add_dependency 'faye-websocket', '~> 0.10.2' # s.add_dependency 'rufus-scheduler', '~> 3.2.0' s.add_development_dependency 'bundler' s.add_development_dependency 'rake' s.add_development_dependency 'rspec' end ================================================ FILE: spec/client_spec.rb ================================================ require 'spec_helper' describe SlackBot::Client do describe '#initialize' do it 'have an empty commands array' do expect(SlackBot::Client.new.commands).to eq [] end end describe '.add_command' do bot = SlackBot::Client.new it 'add command to commands array' do result = bot.add_command('Hello') expect(result).to eq bot.commands end end end ================================================ FILE: spec/command_spec.rb ================================================ require 'spec_helper' describe SlackBot::Command do describe '#initialize' do bot = SlackBot::Client.new it 'have a bot_client' do command = SlackBot::Command.new(bot) expect(command.bot_client).to eq bot end end describe '.match?' do bot = SlackBot::Client.new bot.add_command('Hello') it 'iterate commands' do bot.commands.each do |command| expect(command.regex).to eq 'Hello' end end end end ================================================ FILE: spec/config_spec.rb ================================================ require 'spec_helper' describe SlackBot::Config do describe '#initialize' do it 'sets config' do expect(SlackBot.config.join_message).to eq 'Hello!' expect(SlackBot.config.debug).to eq false expect(SlackBot.config.token).to eq 'TOKEN' end end describe '#configure' do before do SlackBot.configure do |config| config.join_message = 'Hi!' config.debug = true config.token = 'Invalid Token' end end it 'sets config' do expect(SlackBot.config.join_message).to eq 'Hi!' expect(SlackBot.config.debug).to eq true expect(SlackBot.config.token).to eq 'Invalid Token' end end end ================================================ FILE: spec/spec_helper.rb ================================================ require 'bundler/setup' Bundler.setup require 'simple_slack_bot' RSpec.configure do |config| end ================================================ FILE: spec/version_spec.rb ================================================ require 'spec_helper' describe SlackBot do it 'has a version' do expect(SlackBot::VERSION).to_not be nil end end