Last active
September 17, 2020 07:26
-
-
Save seratch/fa84c639aaa3e678ccadc333dd4f174d to your computer and use it in GitHub Desktop.
Slack app built with Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'slack-ruby-client' | |
Slack.configure do |config| | |
config.token = ENV['SLACK_BOT_TOKEN'] | |
end | |
client = Slack::Web::Client.new | |
modal = { | |
'type': 'modal', | |
'title': { | |
'type': 'plain_text', | |
'text': 'My App' | |
}, | |
'submit': { | |
'type': 'plain_text', | |
'text': 'Submit' | |
}, | |
'blocks': [ | |
{ | |
'type': 'input', | |
'block_id': 'b', | |
'element': { | |
'type': 'plain_text_input' | |
}, | |
'label': { | |
'type': 'plain_text', | |
'text': 'Label' | |
} | |
} | |
] | |
} | |
post '/slack/events' do | |
# TODO: verify signature here | |
if request.params['command'] | |
client.views_open( | |
trigger_id: request.params['trigger_id'], | |
view: modal | |
) | |
[200, {}, ''] | |
else | |
body = JSON.parse(request.params['payload']) | |
if body['type'] == 'view_submission' | |
response_body = { | |
'response_action': 'errors', | |
'errors': { | |
'b': 'The value is not valid' | |
} | |
} | |
[200, {'Content-Type' => 'application/json'}, JSON.dump(response_body)] | |
else | |
[200, {}, ''] | |
end | |
end | |
end | |
# ruby app.rb -p 3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
source "https://rubygems.org" | |
gem "sinatra" | |
gem "slack-ruby-client" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment