-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
115 lines (105 loc) · 2.57 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
# -*- coding: utf-8 -*-
$LOAD_PATH << File.dirname(__FILE__)
require 'active_support/time'
require 'sinatra'
require 'sinatra/multi_route'
require 'eventmachine'
require 'slack-notifier'
require 'open-uri'
require 'pp'
require 'db'
require 'bot'
notifier = Slack::Notifier.new ENV['SLACK_INCOMING_HOOK_URL']
notifier.ping "Bot started"
bot = Bot.new
EM::defer do
loop do
sleep 10.minutes
#msg = mt.notification_message
#notifier.ping msg if msg && !msg.empty?
end
end
get '/heartbeat' do
"OK"
end
def help
<<EOF
_*list*_
登録タイピング一覧
_*del <typing_id>*_
タイピング削除
_*add <myTyping ID>*_
タイピング追加
_*ranking [typing_id]*_
ランキング表示
_*updates*_
間近3日間の更新表示
_*roukies*_
間近3日間の新規ユーザー
_*sync [only]*_
ランキング更新と間近3日間の更新表示
only をつけると更新のみ
_*settings*_
設定一覧
_*set <name>=<value>*_
設定変更
設定値
watching_days=<integer>
keeping_days=<integer>
_*help*_
show this help
EOF
end
post '/out-going' do
content_type 'application/json; charset=utf-8'
p request.body.read
return {text: nil}.to_json if params[:user_name] == "slackbot"
p params[:text]
text = params[:text]
if text =~ /^\s*add\s+(\d+)\s*$/
response = bot.add($1.to_i)
response = PP.pp(response, '')
elsif text =~ /^\s*list\s*$/
response = bot.list
response = PP.pp(response, '')
elsif text =~ /^\s*ranking/
game_id = $1.to_i if text =~ /ranking\s+(\d+)\s*$/
response = bot.ranking(game_id)
elsif text =~ /^\s*sync/
only = true if text =~ /sync\s+only\s*$/
EM::defer do
res = bot.sync
res = PP.pp(res, '')
unless only
res += "\n"
res += bot.updates
end
notifier.ping res
end
response = "OK"
elsif text =~ /^\s*rookies\s*$/
response = bot.rookies
elsif text =~ /^\s*updates\s*$/
response = bot.updates
elsif text =~ /^\s*settings\s*$/
response = bot.settings
elsif text =~ /^\s*set\s*(.+)=(.+)$/
name = $1.strip
value = $2.strip
if ["watching_days", "keeping_days"].include?(name) == false
response = "Invalid setting name [#{name}]"
elsif value !~ /^\d+$/
response = "Invalid setting value [#{value}]"
else
response = bot.set(name, value.to_i)
end
elsif text =~ /^\s*help\s*$/
response = help
elsif text =~ /^debug/
response = {now: Time.now.to_s}
response = PP.pp(response, '')
else
response = "I can't recognize command. => [#{text}]"
end
{text: response}.to_json
end