-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
223 lines (186 loc) · 6.52 KB
/
Copy pathmain.py
File metadata and controls
223 lines (186 loc) · 6.52 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import gc
import random
import socket
import requests
import json
import re
import sys
import subprocess
import time
from datetime import datetime
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage
center_list = "./config/center_list.json"
config_list = "./config/bot_config.json"
app = Flask(__name__)
with open(config_list, 'r', encoding='utf-8') as config_file:
bot_config = json.load(config_file)
config_file.close()
admin_list = bot_config['admins']
LINE_CHANNEL_ACCESS_TOKEN = bot_config['line_config']['channel_token']
LINE_CHANNEL_SECRET = bot_config['lind_config']['channel_secret']
bind_port = bot_config['bind_port']
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(LINE_CHANNEL_SECRET)
@app.route("/linebot", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info(f"Request body: {body}")
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
def timestamp_to_hms(timestamp):
dt = datetime.fromtimestamp(timestamp+9*3600)
return dt.strftime("%H:%M:%S")
def read_center():
global center
with open(center_list, 'r', encoding='utf-8') as file:
center = json.load(file)
file.close()
def get_num_of_people():
global center
num_of_people = 0
updated = False
result = "これは現在、山梨県内のゲームセンターの人数状況ですよ!\n-------------------------\n"
read_center()
for key, value in center.items():
if value['last_time'] :
result += f"{key}: {value['num']}({timestamp_to_hms(value['last_time'])})\n"
num_of_people += value['num']
updated = True
if not updated :
result = "まだ誰も人数状況を更新していません··"
else :
result += f"-------------------------\n現在、全てのゲームセンターに合計で{num_of_people}人いますよ!"
return result
def get_num_of_center(ctnm):
read_center()
found = False
for key, value in center.items():
if ctnm in value["nknm"]:
found = True
if value['last_time'] :
result = f"{key}: {value['num']}({timestamp_to_hms(value['last_time'])})"
else :
result = f"{key}: まだ誰も人数状況を更新していません··"
break
if found :
return result
else :
return "このゲームセンターが見つかりません··"
def update_num(user_id, cmd):
global center
read_center()
type = 0
match = re.search(r'(\d+)$', cmd)
new_num = int(match.group(1))
ctnm = cmd[:-len(match.group(1))].strip()
if ctnm.endswith("+"):
type = 1
ctnm = ctnm[:-1]
elif ctnm.endswith("-"):
type = 2
ctnm = ctnm[:-1]
elif ctnm.endswith("="):
type = 3
ctnm = ctnm[:-1]
found = False
for key, value in center.items():
if ctnm in value["nknm"]:
found = True
value['last_time'] = time.time()
if type == 1:
new_num = value['num'] + new_num
elif type == 2:
new_num = value['num'] - new_num
if new_num < 0:
new_num = 0
user_name = line_bot_api.get_profile(user_id).display_name
value['people'] += f"{user_name}: {value['num']}->{new_num}({timestamp_to_hms(value['last_time'])})\n"
result = f"[UPDATED]\n{key}: {value['num']}->{new_num}({timestamp_to_hms(value['last_time'])})"
value['num'] = new_num
break
with open(center_list, "w", encoding="utf-8") as file:
json.dump(center, file, ensure_ascii=False, indent=4)
file.close();
if found :
return result
else :
return "このゲームセンターが見つかりません··"
def get_nickname(ctnm):
global center
read_center()
found = False
for key, value in center.items():
if ctnm in value["nknm"]:
found = True
nknm_list = '\n - '.join(value['nknm'])
result = f"{key}のニックネーム:\n - {nknm_list}"
break
if found :
return result
else :
return "このゲームセンターが見つかりません··"
def get_people(ctnm):
global center
read_center()
found = False
for key, value in center.items():
if ctnm in value["nknm"]:
found = True
if value['last_time'] :
result = f"{key}:\n{value['people'][:-1]}"
else:
result = f"{key}: まだ誰も人数状況を更新していません··"
break
if found :
return result
else :
return "このゲームセンターが見つかりません··"
def clear_center():
global center
read_center()
for key, value in center.items():
value['last_time'] = 0
value['num'] = 0
value['people'] = ""
with open(center_list, "w", encoding="utf-8") as file:
json.dump(center, file, ensure_ascii=False, indent=4)
file.close();
result = "クリア完了!"
return result
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
user_message = event.message.text
user_id = event.source.user_id
need_reply = False
if user_message.strip() == "人数チェック":
reply_message = get_num_of_people()
need_reply = True
elif user_message.strip().endswith(("何人","どう")):
reply_message = get_num_of_center(user_message.strip()[:-2])
need_reply = True
elif user_message.strip().endswith("人"):
reply_message = update_num(user_id, user_message.strip()[:-1])
need_reply = True
elif user_message.strip().endswith("ニック"):
reply_message = get_nickname(user_message.strip()[:-3])
need_reply = True
elif user_message.strip().endswith("誰"):
reply_message = get_people(user_message.strip()[:-1])
need_reply = True
elif user_message.strip() == "clear" and user_id in admin_list:
reply_message = clear_center()
need_reply = True
if need_reply :
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=reply_message)
)
if __name__ == "__main__":
app.run(port = bind_port)