A simple way to build slack interations inside a Rails app.
A simple way to build Slack interations inside a Rails app. Check out our
blog post for the story behind
this gem!
Add this line to your application’s Gemfile:
gem 'slackathon'
Also, add this to config/routes.rb
mount Slackathon::Engine => "/slack"
And then execute:
$ bundle
Set up https://ngrok.com/
This gives you a public URL for Slack to reach your development machine.
On a Mac, you can also install it via brew cask install ngrok
.
Assuming the Rails app is already running on port 3000, you can expose the
Rails app with ngrok http 3000
, which should give you a public URL likehttp://00bea6f5.ngrok.io.
Create a Slack app at https://api.slack.com/apps
The “App Name” will be used as the display name when the app is replying to
commands.
To get everything working perfectly, you probably want to mirror all the
settings on the production app, the here are the most important bits.
Go to “Your App > Settings > Install App” to add it to your Slack.
Create your command under “Your App > Features > Slash Commands”
/monkey
.http://00bea6f5.ngrok.io/slack/commands
.Create a monkey_command.rb
in app/slack
monkey_command.rb
) and the class name (MonkeyCommand
)/monkey
).Slackathon::Command
class.params
hash.call
method.Example:
class MonkeyCommand < Slackathon::Command
def call
{
response_type: "in_channel",
text: "#{user} said #{params[:text]}
"
}
end
private
def user
"<@#{params[:user_id]}>"
end
end
In this section, we will modify the MonkeyBot
and let the user pick which
monkey emoji to use.
Enable “Your App > Features > Interactive Components”
http://00bea6f5.ngrok.io/slack/interactions
.Instead of immediately posting to the channel, we will reply to the user
only, asking for their emoji preference:
class MonkeyCommand < Slackathon::Command
def call
{
response_type: "ephemeral",
attachments: [{
callback_id: "monkey",
text: "Please pick a style",
actions: [{
type: "button",
text: "Click to use
",
name: "post_in_channel",
value: "#{params[:text]}
"
}, {
type: "button",
text: "Click to use
",
name: "post_in_channel",
value: "#{params[:text]}
"
}, {
type: "button",
text: "Click to use
",
name: "post_in_channel",
value: "#{params[:text]}
"
}]
}]
}
end
def post_in_channel(value)
# do something with value, see below...
end
end
ephemeral
response type (as opposed to in_channel
attachments
array.callback_id
need to match the name of your command (e.g. monkey
inactions
array has the button(s) you want to include.text
is the label of the button (e.g. “Click me!!!”).name
is the name of the method to call when the button is clickedvalue
is an optional string that will be passed to the method (seeWhen the user clicks on one of the buttons, it will call the method you
specified:
class MonkeyCommand < Slackathon::Command
# def call ...
def post_in_channel(value)
{
response_type: "in_channel",
delete_original: true,
text: "<@#{params[:user][:id]}> said #{value}"
}
end
end
Here, value
is the string that we attached to the original buttons.
Remove the command from your development Slack app.
Create a production Slack app and your command/interactive component
following the instructions above (with the URLs pointing to your
production Rails app).
Find the “Verification Token” from https://api.slack.com/apps/<your app>
.
Assign its value to the SLACK_VERIFICATION_TOKEN
ENV variable in
your production environment (or set it with Slackathon.verification_token = ...
).
Make sure your Active Job adapter is configured to process the slack
queue (e.g. bundle exec sidekiq -q default -q slack ...
). Alternatively,
you can change the queue with Slackathon.queue = ...
.
Deploy your changes!
The gem is available as open source under the terms of the MIT License.