forked from FromAtom/Kujaku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
60 lines (49 loc) · 1.42 KB
/
main.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
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"])
require 'sinatra'
require 'sinatra/reloader' if development?
require 'json'
require_relative 'lib/esa_client'
require_relative 'lib/slack_api_client'
require_relative 'lib/cache'
ESA_TEAM_NAME = ENV['ESA_TEAM_NAME']
post '/' do
puts '[START]'
params = JSON.parse(request.body.read)
case params['type']
when 'url_verification'
# サーバの正当性検証
challenge = params['challenge']
return {
challenge: challenge
}.to_json
when 'event_callback'
channel = params.dig('event', 'channel')
ts = params.dig('event', 'message_ts')
links = params.dig('event', 'links')
unfurls = {}
links.each do |link|
url = link['url']
if url =~ /\Ahttps:\/\/#{ESA_TEAM_NAME}.esa.io\/posts\/(\d+)#comment-(\d+).*\z/
post_number = $1
comment_number = $2
esa = EsaClient.new
attachment = esa.get_comment(post_number, comment_number)
unfurls[url] = attachment
elsif url =~ /\Ahttps:\/\/#{ESA_TEAM_NAME}.esa.io\/posts\/(\d+).*\z/
post_number = $1
esa = EsaClient.new
attachment = esa.get_post(post_number)
unfurls[url] = attachment
end
end
payload = {
channel: channel,
ts: ts,
unfurls: unfurls
}.to_json
slackApiClient = SlackApiClient.new
slackApiClient.request(payload)
end
return {}.to_json
end