Slack Ruby Bot Server Events Extension
An extension to slack-ruby-bot-server that makes it easy to handle Slack slash commands, interactive buttons and events.
Table of Contents
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
OAuth
Configure your app's OAuth version and scopes as needed by your application.
SlackRubyBotServer.configure do |config|
config.oauth_version = :v2
config.oauth_scope = ['users:read', 'channels:read', 'groups:read', 'chat:write', 'commands', 'incoming-webhook']
end
Events
Configure events-specific settings.
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.
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
end
config.on :event, 'event_callback' do |event|
false
end
config.on :event do |event|
false
end
end
Actions
Respond to Shortcuts and Interactive Messages as well as Attached Interactive Message Buttons(Outmoded) by implementing SlackRubyBotServer::Events::Config#on :action
.
The following example posts an ephemeral message that counts the letters in a message shortcut.
SlackRubyBotServer::Events.configure do |config|
config.on :action, 'interactive_message', 'action_id' do |action|
payload = action[:payload]
message = payload[:message]
Faraday.post(payload[:response_url], {
text: "The text \"#{message[:text]}\" has #{message[:text].size} letter(s).",
response_type: 'ephemeral'
}.to_json, 'Content-Type' => 'application/json')
{ ok: true }
end
config.on :action do |action|
false
end
end
The following example responds to an interactive message.
You can compose rich message layouts using Block Kit Builder.
SlackRubyBotServer::Events.configure do |config|
config.on :action, 'block_actions', 'your_action_id' do |action|
payload = action[:payload]
Faraday.post(payload[:response_url], {
text: "The action \"your_action_id\" has been invoked.",
response_type: 'ephemeral'
}.to_json, 'Content-Type' => 'application/json')
{ ok: true }
end
end
Commands
Respond to Slash Commands by implementing SlackRubyBotServer::Events::Config#on :command
.
The following example responds to /ping
with pong
.
SlackRubyBotServer::Events.configure do |config|
config.on :command, '/ping' do
{ text: 'pong' }
end
config.on :command do |command|
false
end
end
Copyright & License
Copyright Daniel Doubrovkine and Contributors, 2020
MIT License