This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
90 lines (77 loc) · 3.43 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
# frozen_string_literal: true
require 'sinatra/base'
require 'net/http'
require 'net/https'
require 'uri'
require 'time'
require 'json'
require_relative 'config'
require_relative 'kibela'
class SyakusiApp < Sinatra::Base
post '/' do
params = JSON.parse request.body.read
case params['type']
when 'url_verification'
challenge = params['challenge']
return { challenge: challenge }.to_json
when 'event_callback'
return unless params.dig('event', 'type') == 'link_shared'
channel = params.dig('event', 'channel')
ts = params.dig('event', 'message_ts')
links = params.dig('event', 'links')
unfurls = {
channel: channel,
ts: ts,
unfurls: {}
}
links.each do |link|
url = URI.parse link['url']
next unless url.path.start_with?("/notes/") || url.path.start_with?("/@")
note = KibelaClient.get_note(url.path)
if url.fragment&.start_with?('comment_')
match = /^comment_(?<id>\d+)/i.match url.fragment
if not match.nil?
id = match.named_captures['id']
comment = KibelaClient.get_comment(id)
attachment = {
author_link: comment.author.url,
author_name: "@#{comment.author.account}",
title: "「#{note.title}」へのコメント",
title_link: link['url'],
# Kibela サイトでの挙動 (ブラウザーがホワイトスペースを詰める) にあわせる
text: comment.summary.gsub(/\s+/, ' '),
footer: 'Kibela',
footer_icon: 'https://cdn.kibe.la/assets/shortcut_icon-99b5d6891a0a53624ab74ef26a28079e37c4f953af6ea62396f060d3916df061.png',
ts: Time.parse(comment.published_at).to_i
}
unfurls[:unfurls][url] = attachment
next
end
end
attachment = {
author_link: note.author.url,
author_name: "@#{note.author.account}",
title: note.title,
title_link: note.url,
# Kibela サイトでの挙動 (ブラウザーがホワイトスペースを詰める) にあわせる
text: note.summary.gsub(/\s+/, ' '),
footer: 'Kibela',
footer_icon: 'https://cdn.kibe.la/assets/shortcut_icon-99b5d6891a0a53624ab74ef26a28079e37c4f953af6ea62396f060d3916df061.png',
ts: Time.parse(note.published_at).to_i
}
unfurls[:unfurls][url] = attachment
end
req = Net::HTTP::Post.new(Config::SLACK_UNFURL_API.path)
req.body = unfurls.to_json
req['Content-Type'] = 'application/json'
req['Authorization'] = "Bearer #{Config::SLACK_TOKEN}"
https = Net::HTTP.new(Config::SLACK_UNFURL_API.host, Config::SLACK_UNFURL_API.port)
https.use_ssl = true
res = https.request(req)
else
halt 400, {}.to_json
end
return {}.to_json
end
run! if app_file == $0
end