lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-2.2.0 vs lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-2.3.0

- old
+ new

@@ -6,10 +6,11 @@ module Ruboty module Adapters class SlackRTM < Base env :SLACK_TOKEN, "Account's token. get one on https://api.slack.com/web#basics" env :SLACK_EXPOSE_CHANNEL_NAME, "if this set to 1, message.to will be channel name instead of id", optional: true + env :SLACK_IGNORE_BOT_MESSAGE, "If this set to 1, bot ignores bot_messages", optional: true env :SLACK_IGNORE_GENERAL, "if this set to 1, bot ignores all messages on #general channel", optional: true env :SLACK_GENERAL_NAME, "Set general channel name if your Slack changes general name", optional: true def run init @@ -23,11 +24,11 @@ channel = resolve_channel_id(channel[1..-1]) end return unless channel - realtime.send( + realtime.send_message( type: 'message', channel: channel, text: message[:code] ? "```\n#{message[:body]}\n```" : resolve_send_mention(message[:body]), mrkdwn: true ) @@ -47,11 +48,11 @@ make_channels_cache make_usergroups_cache end def bind - realtime.on(:message) do |data| + realtime.on_text do |data| method_name = "on_#{data['type']}".to_sym send(method_name, data) if respond_to?(method_name, true) end end @@ -101,10 +102,14 @@ data = resolve_mention!(data) user = user_info(data['user']) || {} channel = channel_info(data['channel']) + if data['type'] == 'bot_message' && ENV['SLACK_IGNORE_BOT_MESSAGE'] == '1' + return + end + if channel return if channel['name'] == (ENV['SLACK_GENERAL_NAME'] || 'general') && ENV['SLACK_IGNORE_GENERAL'] == '1' channel_to = expose_channel_name? ? "##{channel['name']}" : channel['id'] else # direct message @@ -121,10 +126,12 @@ mention_to: data['mention_to'], time: Time.at(data['ts'].to_f) ) end + alias on_bot_message on_message + def on_channel_change(data) make_channels_cache end alias_method :on_channel_deleted, :on_channel_change alias_method :on_channel_renamed, :on_channel_change @@ -287,10 +294,10 @@ end end def resolve_channel_id(name) ret_id = nil - @channel_info_cahces.each_pair do |id, channel| + @channel_info_caches.each_pair do |id, channel| if channel['name'] == name ret_id = id break end end