lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.3.1 vs lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.4.0

- old
+ new

@@ -1,27 +1,27 @@ module SlackRubyBot module Commands class Base class_attribute :routes - def self.send_message(channel, text, options = { as_user: true }) + def self.send_message(client, channel, text, options = {}) if text && text.length > 0 - chat_postMessage({ channel: channel, text: text }.merge(options)) + send_client_message(client, { channel: channel, text: text }.merge(options)) else - send_message_with_gif channel, 'Nothing to see here.', 'nothing', options + send_message_with_gif client, channel, 'Nothing to see here.', 'nothing', options end end - def self.send_message_with_gif(channel, text, keywords, options = { as_user: true }) + 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 channel, text, options + send_message client, channel, text, options end def self.logger @logger ||= begin $stdout.sync = true @@ -44,23 +44,23 @@ match Regexp.new("^(?<bot>\\w*)[\\s]+(?<command>#{value})$", Regexp::IGNORECASE), &block match Regexp.new("^(?<bot>\\w*)[\\s]+(?<command>#{value})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block end end - def self.invoke(data) + def self.invoke(client, data) self.finalize_routes! expression = data.text called = false routes.each_pair do |route, method| match = route.match(expression) next unless match next if match.names.include?('bot') && match['bot'].downcase != SlackRubyBot.config.user called = true if method - method.call(data, match) + method.call(client, data, match) elsif self.respond_to?(:call) - send(:call, data, match) + send(:call, client, data, match) else fail NotImplementedError, data.text end break end @@ -72,11 +72,11 @@ self.routes[match] = block end private - def self.chat_postMessage(message) - Slack.chat_postMessage(message) + def self.send_client_message(client, data) + client.message(data) end def self.finalize_routes! return if self.routes && self.routes.any? command default_command_name