-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.rb
79 lines (67 loc) · 2.1 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
require 'figlet'
require_relative './emoji_machine'
class API < Sinatra::Base
# testing only
get '/demo' do
font = Figlet::Font.new(font_path('banner'))
figlet = Figlet::Typesetter.new(font)
status 200
body figlet[params['text']]
.gsub!('#', ":#{params['one']}:")
.gsub!(' ', ":#{params['two']}:")
end
# production
# payload:
# {
# "token"=>"o8oredactedgjqc",
# "team_id"=>"T0redactedE8",
# "team_domain"=>"domain",
# "channel_id"=>"channel_id",
# "channel_name"=>"directmessage",
# "user_id"=>"U03S4redactedG",
# "user_name"=>"user",
# "command"=>"/figlet",
# "text"=>"100",
# "response_url"=>"https://hooks.slack.com/Mredactedl0xlYHx",
# "trigger_id"=>"435641testtesttest3b0b0710d"
# }
post '/command' do
STDERR.puts params unless ENV['RACK_ENV'] == 'test'
# validate the request comes from Slack
unless SLACK_CONFIG[:slack_verification_token] == params['token']
halt 403, "Invalid Slack verification token received: #{params['token']}"
end
# help command
if params['text'].squish == 'help'
status 200
return 'Need help? No worries :hugging_face: Visit https://figlet.fun/help ' \
'to learn everything about using Figlet with Slack!'
end
machine = EmojiMachine.new(params['text'])
# user forgot to provide text
if machine.text.blank?
status 200
return 'Oops! That command did not contain any text :sweat_smile: ' \
'Visit https://figlet.fun/help to learn everything about ' \
'using Figlet with Slack!'
end
team = Team.find_by_external_id(params['team_id'])
client = Slack::Web::Client.new(token: team.access_token)
client.chat_postMessage(
channel: params['channel_id'],
text: machine.generate_text
)
status 200
rescue Slack::Web::Api::Errors::SlackError
status 200
"We're sorry, #{params['user_name']}, we can't post to direct messages yet 😅. " \
"We're busy working on a fix!"
end
get '/privacy' do
erb :privacy
end
get '/help' do
erb :help
end
end