Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Enable Lita to handle messages from other bots (again) #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gem "lita-slack"
* `proxy` (String) – Specify a HTTP proxy URL. (e.g. "http://squid.example.com:3128")
* `unfurl_links` (Boolean) – Set to `true` to automatically add previews for all links in messages sent by Lita.
* `unfurl_media` (Boolean) – Set to `false` to prevent automatic previews for media files in messages sent by Lita.
* `handle_bot_messages` (Boolean) - Set to `false` to prevent Lita from handling messages from other bots.

**Note**: When using lita-slack, the adapter will overwrite the bot's name and mention name with the values set on the server, so `config.robot.name` and `config.robot.mention_name` will have no effect.

Expand Down
1 change: 1 addition & 0 deletions lib/lita/adapters/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Slack < Adapter
config :link_names, type: [true, false]
config :unfurl_links, type: [true, false]
config :unfurl_media, type: [true, false]
config :handle_bot_messages, type: [true, false], default: true

# Provides an object for Slack-specific features.
def chat_service
Expand Down
5 changes: 3 additions & 2 deletions lib/lita/adapters/slack/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module Adapters
class Slack < Adapter
# @api private
class MessageHandler
def initialize(robot, robot_id, data)
def initialize(robot, robot_id, config, data)
@robot = robot
@robot_id = robot_id
@config = config
@data = data
@type = data["type"]
end
Expand Down Expand Up @@ -196,7 +197,7 @@ def log

# Types of messages Lita should dispatch to handlers.
def supported_message_subtypes
%w(me_message)
%w(me_message) + (@config.handle_bot_messages ? %w(bot_message) : [])
end

def supported_subtype?
Expand Down
2 changes: 1 addition & 1 deletion lib/lita/adapters/slack/rtm_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def payload_for(channel, string)
def receive_message(event)
data = MultiJson.load(event.data)

EventLoop.defer { MessageHandler.new(robot, robot_id, data).handle }
EventLoop.defer { MessageHandler.new(robot, robot_id, config, data).handle }
end

def safe_payload_for(channel, string)
Expand Down
32 changes: 30 additions & 2 deletions spec/lita/adapters/slack/message_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec_helper"

describe Lita::Adapters::Slack::MessageHandler, lita: true do
subject { described_class.new(robot, robot_id, data) }
subject { described_class.new(robot, robot_id, config, data) }

before do
allow(robot).to receive(:trigger)
Expand All @@ -11,6 +11,7 @@
let(:robot) { instance_double('Lita::Robot', name: 'Lita', mention_name: 'lita') }
let(:robot_id) { 'U12345678' }
let(:channel) { Lita::Adapters::Slack::SlackChannel.new('C2147483705', 'general', 1360782804, 'U023BECGF', raw_data) }
let(:config) { Lita::Adapters::Slack.configuration_builder.build }
let(:raw_data) { Hash.new }

describe "#handle" do
Expand All @@ -25,7 +26,7 @@
end

context "with a normal message" do
let(:data) do
let(:base_data) do
{
"type" => "message",
"channel" => "C2147483705",
Expand All @@ -34,6 +35,7 @@
"ts" => "1234.5678"
}
end
let(:data) { base_data }
let(:message) { instance_double('Lita::Message', command!: false, extensions: {}) }
let(:source) { instance_double('Lita::Source', private_message?: false) }
let(:user) { instance_double('Lita::User', id: 'U023BECGF') }
Expand Down Expand Up @@ -459,6 +461,32 @@


end

context "with a message from another bot" do
let(:data) do
base_data.merge({
"subtype" => "bot_message",
"user" => "R0BBY",
})
end

it "dispatches the message to Lita" do
expect(robot).to receive(:receive)

subject.handle
end


context "when configured to ignore other bots" do
before { config.handle_bot_messages = false }

it "does not dispatch the message to Lita" do
expect(robot).not_to receive(:receive)

subject.handle
end
end
end
end

context "with a message with an unsupported subtype" do
Expand Down
1 change: 1 addition & 0 deletions spec/lita/adapters/slack/rtm_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def with_websocket(subject, queue)
allow(Lita::Adapters::Slack::MessageHandler).to receive(:new).with(
robot,
'U12345678',
config,
{},
).and_return(message_handler)

Expand Down