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

Commit

Permalink
Convert Slack-style @-mentions to Lita style.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Dec 10, 2014
1 parent 1ccb5ca commit f87fddb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/lita/adapters/slack/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def handle
attr_reader :type

def body
data["text"]
data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}")
end

def channel
Expand Down
24 changes: 23 additions & 1 deletion spec/lita/adapters/slack/message_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Lita::Adapters::Slack::MessageHandler, lita: true do
subject { described_class.new(robot, robot_id, data) }

let(:robot) { instance_double('Lita::Robot', name: 'Lita') }
let(:robot) { instance_double('Lita::Robot', name: 'Lita', mention_name: 'lita') }
let(:robot_id) { 'U12345678' }

describe "#handle" do
Expand Down Expand Up @@ -37,13 +37,35 @@
room: "C2147483705"
).and_return(source)
allow(Lita::Message).to receive(:new).with(robot, "Hello", source).and_return(message)
allow(robot).to receive(:receive).with(message)
end

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

subject.handle
end

context "when the message starts with a Slack-style @-mention" do
let(:data) do
{
"type" => "message",
"channel" => "C2147483705",
"user" => "U023BECGF",
"text" => "<@#{robot_id}>: Hello"
}
end

it "converts it to a Lita-style @-mention" do
expect(Lita::Message).to receive(:new).with(
robot,
"@lita: Hello",
source
).and_return(message)

subject.handle
end
end
end

context "with a message with an unsupported subtype" do
Expand Down

0 comments on commit f87fddb

Please sign in to comment.