Slack Ruby Bot Server Events App Mentions
An extension to slack-ruby-bot-server-events that makes it easier to handle app mentions - message events that directly mention your bot user.
Sample
See slack-ruby/slack-ruby-bot-server-events-app-mentions-sample for a working sample.
Usage
Gemfile
Add 'slack-ruby-bot-server-events-app-mentions' to Gemfile.
gem 'slack-ruby-bot-server-events-app-mentions'
Configure OAuth Scopes
The app_mentions:read
OAuth scope is required to receive mentions in channels and im:history
to receive direct messages.
SlackRubyBotServer.configure do |config|
config.oauth_version = :v2
config.oauth_scope = ['app_mentions:read', 'im:history']
end
Implement Mentions
Define a mention
and implement a call
class method that takes event data that has been extended with team
and a regular expression match
object.
class Ping < SlackRubyBotServer::Events::AppMentions::Mention
mention 'ping'
def self.call(data)
client = Slack::Web::Client.new(token: data.team.token)
client.chat_postMessage(channel: data.channel, text: 'pong')
end
end
Mentions can be free-formed regular expressions.
class PingWithNumber < SlackRubyBotServer::Events::AppMentions::Mention
mention(/ping[[:space:]]+(?<number>[[:digit:]]+)$/)
def self.call(data)
client = Slack::Web::Client.new(token: data.team.token)
client.chat_postMessage(channel: data.channel, text: "pong #{data.match['number']}")
end
end
Configure Handlers
By default this library will attempt to match any mention that inherits from SlackRubyBotServer::Events::AppMentions::Mention
in the order of class loading. You can also configure the list of handlers.
SlackRubyBotServer::Events::AppMentions.configure do |config|
config.handlers = [ Ping, Ring ]
end
Copyright & License
Copyright Daniel Doubrovkine and Contributors, 2020