-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.rb
105 lines (88 loc) · 2.56 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"])
require "sinatra/custom_logger"
require "logger"
require "digest/sha1"
require_relative "lib/firestore_cache"
require_relative "lib/redis_cache"
class App < Sinatra::Base
helpers Sinatra::CustomLogger
use Sentry::Rack::CaptureExceptions
configure do
debug_logging = ENV["DEBUG_LOGGING"] == "true"
logger = Logger.new(STDOUT)
logger.level = debug_logging ? Logger::DEBUG : Logger::INFO
set :logger, logger
Sentry.init do |config|
config.enabled_environments = %w[production development]
config.release = File.read(File.join(__dir__, "VERSION")).strip
end
end
before do
if App.enabled_redis?
Redis::Objects.redis = ConnectionPool.new(size: 5, timeout: 5) { Redis.new(url: ENV["REDIS_URL"]) }
end
end
get "/" do
"It works"
end
post "/" do
json = request.body.read
payload = JSON.parse(json)
logger.debug { "payload=#{payload}, json=#{json}" }
case payload["type"]
when "url_verification"
challenge = payload["challenge"]
{ challenge: challenge }.to_json
when "event_callback"
event = payload["event"]
case event["type"]
when "emoji_changed"
case event["subtype"]
when "add"
emoji_name = event["name"]
message = "A new emoji is added :#{emoji_name}: `#{emoji_name}`"
if event["value"].start_with?("alias:")
origin_emoji = event["value"].gsub(/^alias:/, "")
message << " (alias of `#{origin_emoji}`)"
end
if App.enabled_redis?
RedisCache.with_once(message) do
App.post_slack(message)
end
elsif App.enabled_firestore?
FirestoreCache.with_once(ENV["FIRESTORE_COLLECTION"], message) do
App.post_slack(message)
end
else
App.post_slack(message)
end
end
else
raise "Unknown callback event: #{event["type"]}"
end
""
else
raise "Unknown event: #{payload["type"]}"
end
end
def self.enabled_redis?
ENV["REDIS_URL"] && !ENV["REDIS_URL"].empty?
end
def self.enabled_firestore?
ENV["FIRESTORE_COLLECTION"] && !ENV["FIRESTORE_COLLECTION"].empty?
end
def self.post_slack(message)
notifier = Slack::Notifier.new(ENV["SLACK_WEBHOOK_URL"])
options = {
attachments: [
{
fallback: message,
text: message,
color: "good",
},
],
}
notifier.ping(nil, options)
end
end