forked from akira-kuriyama/monday-unfurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
48 lines (39 loc) · 1000 Bytes
/
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
require 'sinatra'
require 'sinatra/reloader' if development?
require 'json'
require_relative 'lib/monday_client'
require_relative 'lib/slack_api_client'
get '/' do
return { hello: 'monday unfurly!' }.to_json
end
post '/' do
p '[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_with_object({}) do |link, memo|
url = link['url']
attachment = MondayClient.fetch(url)
memo[url] = attachment
end
payload = {
channel: channel,
ts: ts,
unfurls: unfurls
}.to_json
p "[LOG] payload=#{payload}"
SlackApiClient.post(payload)
else
p "[LOG] other type. params: #{params}"
end
p '[END]'
return {}.to_json
end