lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.4.3 vs lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.4.4
- old
+ new
@@ -10,20 +10,26 @@
send_message_with_gif client, channel, 'Nothing to see here.', 'nothing', options
end
end
def self.send_message_with_gif(client, channel, text, keywords, options = {})
- gif = begin
- Giphy.random(keywords)
- rescue StandardError => e
- logger.warn "Giphy.random: #{e.message}"
- nil
- end
- text = text + "\n" + gif.image_url.to_s if gif
- send_message client, channel, text, options
+ get_gif_and_send({
+ client: client,
+ channel: channel,
+ text: text,
+ keywords: keywords
+ }.merge(options))
end
+ def self.send_gif(client, channel, keywords, options = {})
+ get_gif_and_send({
+ client: client,
+ channel: channel,
+ keywords: keywords
+ }.merge(options))
+ end
+
def self.logger
@logger ||= begin
$stdout.sync = true
Logger.new(STDOUT)
end
@@ -46,11 +52,11 @@
end
end
def self.invoke(client, data)
self.finalize_routes!
- expression = data.text
+ expression = parse(data)
called = false
routes.each_pair do |route, method|
match = route.match(expression)
next unless match
next if match.names.include?('bot') && !SlackRubyBot.config.name?(match['bot'])
@@ -72,16 +78,42 @@
self.routes[match] = block
end
private
- def self.send_client_message(client, data)
- client.message(data)
+ def self.parse(data)
+ text = data.text
+ return text unless data.channel && data.channel[0] == 'D' && data.user && data.user != SlackRubyBot.config.user_id
+ SlackRubyBot.config.names.each do |name|
+ text.downcase.tap do |td|
+ return text if td == name || td.starts_with?("#{name} ")
+ end
+ end
+ "#{SlackRubyBot.config.user} #{text}"
end
def self.finalize_routes!
return if self.routes && self.routes.any?
command default_command_name
+ end
+
+ def self.get_gif_and_send(options = {})
+ options = options.dup
+ gif = begin
+ keywords = options.delete(:keywords)
+ Giphy.random(keywords)
+ rescue StandardError => e
+ logger.warn "Giphy.random: #{e.message}"
+ nil
+ end
+ client = options.delete(:client)
+ text = options.delete(:text)
+ text = [text, gif && gif.image_url.to_s].compact.join("\n")
+ send_client_message(client, { text: text }.merge(options))
+ end
+
+ def self.send_client_message(client, data)
+ client.message(data)
end
end
end
end