From 80e2321eef35e6bf464b262867f43e6c2098160b Mon Sep 17 00:00:00 2001
From: dB <dblock@dblock.org>
Date: Fri, 21 Aug 2015 15:57:46 -0400
Subject: [PATCH] You can address the bot by its slack @id. Closes #13.

---
 CHANGELOG.md                                                | 4 +++-
 README.md                                                   | 4 ++--
 examples/minimal/Gemfile.lock                               | 5 +----
 lib/slack-ruby-bot/commands/about.rb                        | 2 +-
 lib/slack-ruby-bot/commands/base.rb                         | 4 ++--
 lib/slack-ruby-bot/config.rb                                | 2 +-
 .../rspec/support/slack_ruby_bot_configure.rb               | 1 +
 spec/slack-ruby-bot/commands/about_spec.rb                  | 3 +++
 spec/slack-ruby-bot/commands/hi_spec.rb                     | 6 ++++++
 9 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 157c6c5..9f7d6d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,6 @@
-### 0.4.3 (Next)
+### 0.4.3 (8/21/2015)
+
+* [#13](https://github.com/dblock/slack-ruby-bot/issues/13): You can now address the bot by its Slack @id - [@dblock](https://githubcom/dblock).
 
 * Your contribution here.
 
diff --git a/README.md b/README.md
index 20f4622..68616e5 100644
--- a/README.md
+++ b/README.md
@@ -56,7 +56,7 @@ The following examples of production-grade bots based on slack-ruby-bot are list
 
 ### Commands and Operators
 
-Bots are addressed by name and respond to commands and operators. By default a command class responds, case-insensitively, to its name. A class called `Phone` that inherits from `SlackRubyBot::Commands::Base` responds to `phone` and `Phone` and calls the `call` method when implemented.
+Bots are addressed by name and respond to commands and operators. By default a command class responds, case-insensitively, to its name and Slack ID. A class called `Phone` that inherits from `SlackRubyBot::Commands::Base` responds to `phone` and `Phone` and calls the `call` method when implemented.
 
 ```ruby
 class Phone < SlackRubyBot::Commands::Base
@@ -107,7 +107,7 @@ Operator match data includes `match['operator']` and `match['expression']`. The
 
 ### Bot Aliases
 
-A bot will always respond to its name, but you can specify multiple aliases via the `SLACK_RUBY_BOT_ALIASES` environment variable or via an explicit configuration.
+A bot will always respond to its name and Slack ID, but you can specify multiple aliases via the `SLACK_RUBY_BOT_ALIASES` environment variable or via an explicit configuration.
 
 ```
 SLACK_RUBY_BOT_ALIASES=:pp: table-tennis
diff --git a/examples/minimal/Gemfile.lock b/examples/minimal/Gemfile.lock
index bb108f0..31fe172 100644
--- a/examples/minimal/Gemfile.lock
+++ b/examples/minimal/Gemfile.lock
@@ -1,7 +1,7 @@
 PATH
   remote: ../..
   specs:
-    slack-ruby-bot (0.4.2)
+    slack-ruby-bot (0.4.3)
       activesupport
       giphy (~> 2.0.2)
       hashie
@@ -60,6 +60,3 @@ PLATFORMS
 
 DEPENDENCIES
   slack-ruby-bot!
-
-BUNDLED WITH
-   1.10.6
diff --git a/lib/slack-ruby-bot/commands/about.rb b/lib/slack-ruby-bot/commands/about.rb
index 6d2c566..9950c8f 100644
--- a/lib/slack-ruby-bot/commands/about.rb
+++ b/lib/slack-ruby-bot/commands/about.rb
@@ -2,7 +2,7 @@ module SlackRubyBot
   module Commands
     class Default < Base
       command 'about'
-      match(/^(?<bot>\w*)$/)
+      match(/^(?<bot>[\w[:punct:]@<>]*)$/)
 
       def self.call(client, data, _match)
         send_message_with_gif client, data.channel, SlackRubyBot::ABOUT, 'selfie'
diff --git a/lib/slack-ruby-bot/commands/base.rb b/lib/slack-ruby-bot/commands/base.rb
index 9a891fb..53a8d39 100644
--- a/lib/slack-ruby-bot/commands/base.rb
+++ b/lib/slack-ruby-bot/commands/base.rb
@@ -41,8 +41,8 @@ def self.operator(*values, &block)
 
       def self.command(*values, &block)
         values.each do |value|
-          match Regexp.new("^(?<bot>[\\w[:punct:]]*)[\\s]+(?<command>#{value})$", Regexp::IGNORECASE), &block
-          match Regexp.new("^(?<bot>[\\w[:punct:]]*)[\\s]+(?<command>#{value})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block
+          match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})$", Regexp::IGNORECASE), &block
+          match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block
         end
       end
 
diff --git a/lib/slack-ruby-bot/config.rb b/lib/slack-ruby-bot/config.rb
index defdbd5..f382cb0 100644
--- a/lib/slack-ruby-bot/config.rb
+++ b/lib/slack-ruby-bot/config.rb
@@ -11,7 +11,7 @@ module Config
     attr_accessor :team_id
 
     def names
-      [user, aliases].compact.flatten
+      [user, aliases, "<@#{user_id.downcase}>", "<@#{user_id.downcase}>:"].compact.flatten
     end
 
     def name?(name)
diff --git a/lib/slack-ruby-bot/rspec/support/slack_ruby_bot_configure.rb b/lib/slack-ruby-bot/rspec/support/slack_ruby_bot_configure.rb
index ae01016..ec162ab 100644
--- a/lib/slack-ruby-bot/rspec/support/slack_ruby_bot_configure.rb
+++ b/lib/slack-ruby-bot/rspec/support/slack_ruby_bot_configure.rb
@@ -3,6 +3,7 @@
     SlackRubyBot.configure do |c|
       c.token = 'testtoken'
       c.user = 'rubybot'
+      c.user_id = 'DEADBEEF'
     end
   end
 end
diff --git a/spec/slack-ruby-bot/commands/about_spec.rb b/spec/slack-ruby-bot/commands/about_spec.rb
index 8f8395f..3fa2a04 100644
--- a/spec/slack-ruby-bot/commands/about_spec.rb
+++ b/spec/slack-ruby-bot/commands/about_spec.rb
@@ -10,4 +10,7 @@ def app
   it 'upcase' do
     expect(message: SlackRubyBot.config.user.upcase).to respond_with_slack_message(SlackRubyBot::ABOUT)
   end
+  it 'id' do
+    expect(message: "<@#{SlackRubyBot.config.user_id}>").to respond_with_slack_message(SlackRubyBot::ABOUT)
+  end
 end
diff --git a/spec/slack-ruby-bot/commands/hi_spec.rb b/spec/slack-ruby-bot/commands/hi_spec.rb
index 66f0104..d2f6dfc 100644
--- a/spec/slack-ruby-bot/commands/hi_spec.rb
+++ b/spec/slack-ruby-bot/commands/hi_spec.rb
@@ -7,4 +7,10 @@ def app
   it 'says hi' do
     expect(message: "#{SlackRubyBot.config.user} hi").to respond_with_slack_message('Hi <@user>!')
   end
+  it 'says hi to @bot' do
+    expect(message: "<@#{SlackRubyBot.config.user_id}> hi").to respond_with_slack_message('Hi <@user>!')
+  end
+  it 'says hi to @bot: ' do
+    expect(message: "<@#{SlackRubyBot.config.user_id}>: hi").to respond_with_slack_message('Hi <@user>!')
+  end
 end