Slack Ruby Bot Server Slack Extensions
An extension to slack-ruby-bot-server that enables handling of Slack slash commands, interactive buttons and events.
Sample
See slack-ruby/slack-ruby-bot-server-events-sample for a working sample.
Usage
Gemfile
Add 'slack-ruby-bot-server-events' to Gemfile.
gem 'slack-ruby-bot-server-events'
Configure
SlackRubyBotServer::Events.configure do |config|
config.signing_secret = 'secret'
end
The following settings are supported.
| setting | description |
|---|---|
| signing_secret | Slack signing secret, defaults is ENV['SLACK_SIGNING_SECRET']. |
| signature_expires_in | Signature expiration window in seconds, default is 300. |
Get the signing secret from your app's Basic Information settings.
Implement Callbacks
This library supports events, actions and commands. When implementing multiple callbacks for each type, the response from the first callback to return a non nil value will be used and no further callbacks will be invoked. Callbacks receive subclasses of SlackRubyBotServer::Events::Requests::Request.
Events
Respond to Slack Events by implementing SlackRubyBotServer::Events::Config#on :event. The following example unfurls URLs and fails any other event type.
SlackRubyBotServer::Events.configure do |config|
config.on :event, ['event_callback', 'link_shared'] do |event|
event[:event][:links].each do |link|
Slack::Web::Client.new(token: '...').chat_unfurl(
channel: event[:event][:channel],
ts: event[:event][:message_ts],
unfurls: {
link[:url] => { text: 'Unfurled URL.' }
}.to_json
)
end
true # return true to avoid invoking further callbacks
end
config.on :event, 'event_callback' do |event|
raise "I don't know how to handle #{event[:event][:type]}."
end
config.on :event do |event|
raise "I don't know how to handle #{event[:type]}."
end
end
Actions
Respond to Interactive Message Buttons by implementing SlackRubyBotServer::Events::Config#on :action.
SlackRubyBotServer::Events.configure do |config|
config.on :action, 'action_id' do |action|
# action[:payload][:callback_id] is 'action_id'
{ text: 'Success!' }
end
config.on :action do |action|
{ text: "I don't know how to handle #{action[:payload][:callback_id]}." }
end
end
Commands
Respond to Slash Commands by implementing SlackRubyBotServer::Events::Config#on :command.
SlackRubyBotServer::Events.configure do |config|
config.on :command, '/test' do
{ text: 'Success!' }
end
config.on :command do |command|
{ text: "I don't know how to handle #{command[:command]}." }
end
end
Copyright & License
Copyright Daniel Doubrovkine and Contributors, 2020