Skip to content

Commit

Permalink
commands helper
Browse files Browse the repository at this point in the history
  • Loading branch information
accessd committed May 3, 2016
1 parent fb2e340 commit b4ee1fa
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 31 deletions.
4 changes: 4 additions & 0 deletions examples/market/marketbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
require 'yahoo-finance'

class MarketBot < SlackRubyBot::Bot
title 'MarketBot'
desc 'Shows the last finance quotes'
long_desc 'Understands commands like: How\'s AMZN today?'

scan(/([A-Z]{2,5}+)/) do |client, data, stocks|
YahooFinance::Client.new.quotes(stocks, [:name, :symbol, :last_trade_price, :change, :change_in_percent]).each do |quote|
next if quote.symbol == 'N/A'
Expand Down
4 changes: 4 additions & 0 deletions examples/minimal/pongbot.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'slack-ruby-bot'

class Bot < SlackRubyBot::Bot
title 'ping'
desc 'Sends you pong.'
long_desc ''

command 'ping' do |client, data, _match|
client.say(text: 'pong', channel: data.channel)
end
Expand Down
4 changes: 4 additions & 0 deletions examples/weather/weatherbot.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'slack-ruby-bot'

class WeatherBot < SlackRubyBot::Bot
title 'WeatherBot'
desc 'Shows how the weather'
long_desc 'Command format: How is the weather in Paris?'

match(/^How is the weather in (?<location>\w*)\?$/i) do |client, data, match|
client.say(channel: data.channel, text: "The weather in #{match[:location]} is nice.")
end
Expand Down
1 change: 1 addition & 0 deletions lib/slack-ruby-bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ def config
require 'slack-ruby-bot/commands'
require 'slack-ruby-bot/client'
require 'slack-ruby-bot/server'
require 'slack-ruby-bot/support/commands_helper'
require 'slack-ruby-bot/app'
require 'slack-ruby-bot/bot'
1 change: 1 addition & 0 deletions lib/slack-ruby-bot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(options = {})
Slack.configure do |config|
config.token = SlackRubyBot.config.token
end
CommandsHelper.validate_attrs if SlackRubyBot.config.help_attrs_required?
super
end

Expand Down
14 changes: 13 additions & 1 deletion lib/slack-ruby-bot/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SlackRubyBot
module Commands
class Base
include Loggable
class_attribute :routes
class_attribute :routes, :command_name, :command_desc, :command_long_desc

class << self
def send_message(client, channel, text, options = {})
Expand Down Expand Up @@ -78,6 +78,18 @@ def scan(match, &block)
self.routes[match] = { match_method: :scan, call: block }
end

def title(title)
self.command_name = title
end

def desc(desc)
self.command_desc = desc
end

def long_desc(long_desc)
self.command_long_desc = long_desc
end

private

def parse(client, data)
Expand Down
27 changes: 0 additions & 27 deletions lib/slack-ruby-bot/commands/commands.rb

This file was deleted.

37 changes: 35 additions & 2 deletions lib/slack-ruby-bot/commands/help.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
module SlackRubyBot
module Commands
class Help < Base
def self.call(client, data, _match)
client.say(channel: data.channel, text: 'See https://github.com/dblock/slack-ruby-bot, please.', gif: 'help')
title 'help'
desc 'Shows help information.'
long_desc ''

command 'help' do |client, data, match|
command = match[:expression]

text = if command.present?
CommandsHelper.command_full_desc(command)
else
general_text
end

client.say(channel: data.channel, text: text, gif: 'help')
end

class << self

private

def general_text
bots_descs = CommandsHelper.all_bots_descs
command_descs = CommandsHelper.all_commands_descs
<<TEXT
#{bots_descs.join("\n")}
*Possible commands:*
#{command_descs.join("\n")}
For getting description of the command use: *help <command>*
For more information see https://github.com/dblock/slack-ruby-bot, please.
TEXT
end

end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/slack-ruby-bot/commands/hi.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module SlackRubyBot
module Commands
class Hi < Base
title 'hi'
desc 'Says hello.'
long_desc 'When a user types "hi" the bot will reply "hello". This helps everyone stay polite.'

def self.call(client, data, _match)
client.say(channel: data.channel, text: "Hi <@#{data.user}>!", gif: 'hi')
end
Expand Down
7 changes: 6 additions & 1 deletion lib/slack-ruby-bot/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ module SlackRubyBot
module Config
extend self

ATTRS = [:token, :url, :aliases, :user, :user_id, :team, :team_id, :allow_message_loops, :send_gifs].freeze
ATTRS = [:token, :url, :aliases, :user, :user_id, :team, :team_id, :allow_message_loops, :send_gifs,
:help_attrs_required].freeze
attr_accessor(*ATTRS)

def allow_message_loops?
allow_message_loops
end

def help_attrs_required?
help_attrs_required
end

def send_gifs?
v = boolean_from_env('SLACK_RUBY_BOT_SEND_GIFS')
v.nil? ? (send_gifs.nil? || send_gifs) : v
Expand Down
79 changes: 79 additions & 0 deletions lib/slack-ruby-bot/support/commands_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module SlackRubyBot
class CommandsHelper
BUILTIN_COMMAND_CLASSES = [SlackRubyBot::Commands::Help, SlackRubyBot::Commands::Hi].freeze
HELP_ATTR_METHODS_MAP = {
command_name: :title,
command_desc: :desc,
command_long_desc: :long_desc
}.freeze

class << self
def validate_attrs
all_command_classes.each do |k|
HELP_ATTR_METHODS_MAP.each do |attr, setter_method|
raise "#{k}: #{setter_method} is not present" if k.public_send(attr).nil?
end
end
end

def all_bots_descs
commands_info(bot_classes).map do |command_info|
"#{command_name_and_desc(command_info)}\n#{command_info[:long_desc]}"
end
end

def all_commands_descs
commands_info(command_classes_only).map do |command_info|
command_name_and_desc(command_info)
end
end

def command_full_desc(name)
info = commands_info(command_classes_only).find { |command_info| command_info[:title] == name }
return "There's no command #{name}" unless info
return "There's no description for command #{name}" if info[:long_desc].blank?
"#{command_name_and_desc(info)}\n\n#{info[:long_desc]}"
end

private

def command_name_and_desc(command_info)
desc = command_info[:desc].present? ? "- #{command_info[:desc]}" : ''
"*#{command_info[:title]}* #{desc}"
end

def commands_info(command_classes)
commands_with_present_names = command_classes.select { |k| k.command_name.present? }
commands_with_present_names.inject([]) do |data, klass|
info = {}
HELP_ATTR_METHODS_MAP.each do |attr, setter_method|
info[setter_method] = klass.public_send(attr)
end
data << info
end
end

def bot_classes
all_command_classes.select { |k| k.superclass == SlackRubyBot::Bot }
end

def command_classes_only
all_command_classes.reject { |k| k.superclass == SlackRubyBot::Bot }
end

def all_command_classes
BUILTIN_COMMAND_CLASSES + external_command_classes
end

def command_classes
SlackRubyBot::Commands::Base.descendants
end

def external_command_classes
command_classes.reject do |k|
k.name && k.name.start_with?('SlackRubyBot::Commands') || k == SlackRubyBot::Bot
end
end
end
end
end

0 comments on commit b4ee1fa

Please sign in to comment.