forked from freyjasdottir/chowdabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
67 lines (55 loc) · 1.29 KB
/
app.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
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/reloader'
require 'sinatra/flash'
require 'json'
require 'dotenv'
require 'httparty'
enable :sessions
configure :development, :test do
require 'pry'
end
configure do
set :views, 'app/views'
end
Dir[File.join(File.dirname(__FILE__), 'app', '**', '*.rb')].each do |file|
require file
also_reload file
end
Dotenv.load
env_configs = {}
ENV['SLACK_CONFIGS'].split(' ').each do |config|
auth_token = ENV['SLASH_COMMAND_AUTH_TOKEN_' + config]
incoming_webhook = ENV['INCOMING_WEBHOOK_URL_' + config]
channel = ENV['CHANNEL_' + config]
env_configs[auth_token] = {webhook: incoming_webhook, channel: channel}
end
InvalidTokenError = Class.new(Exception)
def adminize_message(webhook, channel, msg)
message = {
"username": "chowdabot",
"channel": channel,
"text": "<!channel> " + msg
}.to_json
HTTParty.post(
webhook,
:body => message,
:headers => {
'Content-type' => 'application/json'
}
)
end
get '/' do
@title = "Hello World"
erb :index
end
post '/' do
if env_configs.has_key?(params[:token])
config = env_configs[params[:token]]
adminize_message(config[:webhook], config[:channel], params['text'])
status 200
else
raise InvalidTokenError
status 401
end
end