Skip to content

Commit

Permalink
Don't respond to messages sent by the robot itself
Browse files Browse the repository at this point in the history
Sometimes the slack adapter sends for processing messages from the bot
itself and trying to reply to those messages fails with “Slack API call
to im.open returned an error: cannot_dm_bot”
  • Loading branch information
cristianbica committed Jun 23, 2016
1 parent caac8e1 commit 04780fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/lita/handlers/cleverbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ def reply(message)
end

def should_reply?(message)
message.command? || message.body =~ /#{aliases.join('|')}/i
!sent_by_robot?(message) && ( command?(message) || robot_mentioned?(message) )
end

def command?(message)
message.command?
end

def sent_by_robot?(message)
message.user.mention_name == robot.mention_name
end

def robot_mentioned?(message)
message.body =~ /#{aliases.join('|')}/i
end

def build_response(message)
Expand Down
2 changes: 1 addition & 1 deletion lita-cleverbot.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "lita-cleverbot"
spec.version = "1.0.0"
spec.version = "1.0.1"
spec.authors = ["Cristian Bica"]
spec.email = ["cristian.bica@gmail.com"]
spec.description = "Lita AI via cleverbot.io"
Expand Down
6 changes: 6 additions & 0 deletions spec/lita/handlers/cleverbot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
send_message("test", from: room)
end

it "shouldn't reply to messages sent by the bot" do
expect_any_instance_of(Lita::Handlers::Cleverbot).to receive(:reply).never
robot_user = Lita::User.new(100, name: robot.mention_name)
send_message("Lita: test", as: robot_user, from: room)
end

it "should try to reply to messages addressed to the robot" do
expect_any_instance_of(Lita::Handlers::Cleverbot).to receive(:reply).once
send_message("hey Lita, who am I?", from: room)
Expand Down

0 comments on commit 04780fd

Please sign in to comment.