-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
141 lines (115 loc) · 3.91 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"])
require 'sinatra'
require 'sinatra/reloader' if development?
require 'json'
require 'slack-notifier'
require 'open-uri'
require 'fileutils'
require_relative 'lib/cache'
require_relative 'lib/emoji'
require_relative 'lib/esa_emoji_client'
SLACK_WEBHOOK_URL = ENV["SLACK_WEBHOOK_URL"]
ESA_ACCESS_TOKEN = ENV['ESA_ACCESS_TOKEN']
ESA_TEAM_NAME = ENV['ESA_TEAM_NAME']
IMAGE_BUFFER_DIR = "images"
IGNORE_EMOJI_LIST = ENV['IGNORE_EMOJI_LIST'] || '' # e.g. "ignore_emoji1, ignore_emoji2, ignore_emoji3"
if ESA_ACCESS_TOKEN.nil? || ESA_TEAM_NAME.nil? || SLACK_WEBHOOK_URL.nil?
puts "[ERROR]: Require ENV['ESA_ACCESS_TOKEN']." if ESA_ACCESS_TOKEN.nil?
puts "[ERROR]: Require ENV['ESA_TEAM_NAME']." if ESA_TEAM_NAME.nil?
puts "[ERROR]: Require ENV['SLACK_WEBHOOK_URL']." if SLACK_WEBHOOK_URL.nil?
exit
end
class App < Sinatra::Base
get '/' do
return 'ok'
end
post '/' do
json = request.body.read
payload = JSON.parse(json)
case payload['type']
when 'url_verification'
challenge = payload['challenge']
return { challenge: challenge }.to_json
when 'event_callback'
event_id = payload['event_id']
return if Cache.exists?(event_id)
Cache.set(event_id)
event = payload['event']
case event['type']
when 'emoji_changed'
case event['subtype']
when 'add'
name = event['name']
ignore_emojis = IGNORE_EMOJI_LIST.split(',').map(&:strip) || []
return if ignore_emojis.include?(name)
is_alias = event['value'].start_with?('alias:')
if is_alias
target_name = event["value"].gsub(/^alias:/, "")
App.add_emoji_alias(name, target_name)
else
url = event['value']
App.add_emoji(name, url)
end
when 'remove'
names = event['names']
names.each do |name|
ignore_emojis = IGNORE_EMOJI_LIST.split(',').map(&:strip) || []
next if ignore_emojis.include?(name)
App.remove_emoji(name)
end
end
end
end
end
def self.add_emoji(name, url)
emoji = Emoji.new(name, url)
dry_run = ENV['SINATRA_ENV'] == 'test'
path = "./#{IMAGE_BUFFER_DIR}/#{emoji.filename}"
# EmojiをDLする
unless dry_run
# SlackからDLした絵文字画像を保存するフォルダを準備
Dir.mkdir(IMAGE_BUFFER_DIR) if not Dir.exist?(IMAGE_BUFFER_DIR)
URI.open(emoji.url) do |file|
open(path, "w+b") do |out|
out.write(file.read)
end
end
end
esa_emoji_client = EsaEmojiClient.new(ESA_ACCESS_TOKEN, ESA_TEAM_NAME, dry_run)
message = esa_emoji_client.add(emoji, path)
message ||= "esaに :#{emoji.name}: `#{emoji.name}` を追加しました。"
post_to_slack(message)
unless dry_run
FileUtils.rm(path)
end
end
def self.add_emoji_alias(name, target_name)
dry_run = ENV['SINATRA_ENV'] == 'test'
esa_emoji_client = EsaEmojiClient.new(ESA_ACCESS_TOKEN, ESA_TEAM_NAME, dry_run)
message = esa_emoji_client.add_alias(name, target_name)
message ||= "esaに :#{name}: `#{name}` を `#{target_name}` のエイリアスとして追加しました。"
post_to_slack(message)
end
def self.remove_emoji(name)
dry_run = ENV['SINATRA_ENV'] == 'test'
esa_emoji_client = EsaEmojiClient.new(ESA_ACCESS_TOKEN, ESA_TEAM_NAME, dry_run)
message = esa_emoji_client.remove(name)
message ||= "esaから :#{name}: `#{name}` を削除しました。"
post_to_slack(message)
end
def self.post_to_slack(message)
dry_run = ENV['SINATRA_ENV'] == 'test'
notifier = Slack::Notifier.new(SLACK_WEBHOOK_URL)
options = {
attachments: [
{
fallback: message,
text: message,
color: '#4A9994',
},
],
}
notifier.ping(nil, options) unless dry_run
end
end