yuripeyamashita
commited on
Commit
•
ed0747c
1
Parent(s):
76efe7b
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
import os
|
2 |
-
import re
|
3 |
-
import json
|
4 |
import requests
|
5 |
from flask import Flask, request
|
6 |
from simpleeval import simple_eval
|
@@ -44,22 +42,37 @@ def api():
|
|
44 |
|
45 |
matrix = [[0 for _ in range(len(users))] for _ in range(len(users))]
|
46 |
|
|
|
|
|
47 |
for msg_id, data in message_list.items():
|
48 |
quoted_msg_id = data.get("quoted_msg_id")
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
if quoted_msg_id:
|
52 |
fr = users.index(data.get("user_id"))
|
53 |
to = users.index(message_list.get(quoted_msg_id).get("user_id"))
|
54 |
matrix[fr][to] += data.get("amount")
|
55 |
|
56 |
-
if not quoted_msg_id and quoted_number == 0: # 要平分的情況
|
57 |
-
to = users.index(data.get("user_id"))
|
58 |
-
for row in matrix:
|
59 |
-
row[to] += (data.get("amount") / users_number)
|
60 |
-
|
61 |
print(matrix)
|
62 |
|
|
|
|
|
|
|
63 |
matrix_copy = [[0 for _ in range(len(matrix))] for _ in range(len(matrix))]
|
64 |
|
65 |
for i in range(len(matrix_copy)):
|
@@ -70,19 +83,25 @@ def api():
|
|
70 |
for r in matrix_copy:
|
71 |
print(r)
|
72 |
|
73 |
-
|
74 |
|
75 |
for i in range(len(matrix_copy)):
|
76 |
for j in range(len(matrix_copy)):
|
77 |
amount = matrix_copy[i][j]
|
78 |
if amount > 0:
|
79 |
-
|
80 |
if amount < 0:
|
81 |
-
|
|
|
|
|
82 |
|
83 |
-
for r in
|
84 |
print(r)
|
85 |
|
|
|
|
|
|
|
|
|
86 |
raise ValueError("Action Success")
|
87 |
|
88 |
if "$" not in payload.get("msg_text"):
|
@@ -123,20 +142,6 @@ def api():
|
|
123 |
send_text(payload.get("token"), f"$ {amount-paid} 未付清", data.get("quote_token"))
|
124 |
break
|
125 |
|
126 |
-
# if quoted_msg_id:
|
127 |
-
# fr = users.index(data.get("user_id"))
|
128 |
-
# to = users.index(message_list.get(quoted_msg_id).get("user_id"))
|
129 |
-
# matrix[fr][to] += data.get("amount")
|
130 |
-
|
131 |
-
# if not quoted_msg_id and quoted_number == 0: # 要平分的情況
|
132 |
-
# to = users.index(data.get("user_id"))
|
133 |
-
# for row in matrix:
|
134 |
-
# row[to] += (data.get("amount") / users_number)
|
135 |
-
|
136 |
-
# username = get_username(payload)
|
137 |
-
# text = payload.get("msg_text")
|
138 |
-
# print(username)
|
139 |
-
# send_text(payload.get("token"), f"{username}:{text}")
|
140 |
except Exception as e:
|
141 |
print(f"An error occurred: {e}")
|
142 |
|
@@ -184,12 +189,14 @@ def send_flex_text(token, bubble):
|
|
184 |
|
185 |
|
186 |
def get_username(user_id: str):
|
|
|
|
|
187 |
url = f"https://api.line.me/v2/bot/group/{GROUP_ID}/member/{user_id}"
|
188 |
try:
|
189 |
res_json = requests.get(url, headers={"Authorization": "Bearer " + LINE_CHANNEL_ACCESS_TOKEN}).json()
|
190 |
return res_json.get("displayName")
|
191 |
except:
|
192 |
-
return "
|
193 |
|
194 |
|
195 |
def get_users_number() -> int:
|
@@ -216,12 +223,12 @@ def get_amount(text: str) -> float | None:
|
|
216 |
|
217 |
def get_summary_bubble(title: str, payer: str, borrowers: list):
|
218 |
total = 0
|
219 |
-
|
220 |
|
221 |
for borrower in borrowers:
|
222 |
amount = float(borrower.get("amount"))
|
223 |
total += amount
|
224 |
-
|
225 |
"type": "box",
|
226 |
"layout": "horizontal",
|
227 |
"contents": [
|
@@ -272,14 +279,14 @@ def get_summary_bubble(title: str, payer: str, borrowers: list):
|
|
272 |
},
|
273 |
{
|
274 |
"type": "separator",
|
275 |
-
"margin": "
|
276 |
},
|
277 |
{
|
278 |
"type": "box",
|
279 |
"layout": "vertical",
|
280 |
-
"margin": "
|
281 |
"spacing": "sm",
|
282 |
-
"contents":
|
283 |
},
|
284 |
{
|
285 |
"type": "separator",
|
@@ -297,7 +304,7 @@ def get_summary_bubble(title: str, payer: str, borrowers: list):
|
|
297 |
},
|
298 |
{
|
299 |
"type": "text",
|
300 |
-
"text": f"${total}",
|
301 |
"size": "sm",
|
302 |
"color": "#111111",
|
303 |
"align": "end",
|
@@ -316,5 +323,118 @@ def get_summary_bubble(title: str, payer: str, borrowers: list):
|
|
316 |
}
|
317 |
|
318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
319 |
if __name__ == "__main__":
|
320 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
import os
|
|
|
|
|
2 |
import requests
|
3 |
from flask import Flask, request
|
4 |
from simpleeval import simple_eval
|
|
|
42 |
|
43 |
matrix = [[0 for _ in range(len(users))] for _ in range(len(users))]
|
44 |
|
45 |
+
should_checkout = True
|
46 |
+
|
47 |
for msg_id, data in message_list.items():
|
48 |
quoted_msg_id = data.get("quoted_msg_id")
|
49 |
+
quoted_msg_list = {key: value for key, value in message_list.items() if value.get("quoted_msg_id") == msg_id}
|
50 |
+
|
51 |
+
if not quoted_msg_id and len(quoted_msg_list) != 0: # 檢查是否付清
|
52 |
+
amount: float = data.get("amount")
|
53 |
+
paid: float = 0.0
|
54 |
+
for _, value in quoted_msg_list.items():
|
55 |
+
paid += value.get("amount")
|
56 |
+
if amount-paid > 1:
|
57 |
+
send_text(payload.get("token"), f"$ {amount-paid} 未付清", data.get("quote_token"))
|
58 |
+
should_checkout = False
|
59 |
+
break
|
60 |
+
|
61 |
+
if not quoted_msg_id and len(quoted_msg_list) == 0: # 要平分的情況
|
62 |
+
to = users.index(data.get("user_id"))
|
63 |
+
for row in matrix:
|
64 |
+
row[to] += (data.get("amount") / users_number)
|
65 |
|
66 |
if quoted_msg_id:
|
67 |
fr = users.index(data.get("user_id"))
|
68 |
to = users.index(message_list.get(quoted_msg_id).get("user_id"))
|
69 |
matrix[fr][to] += data.get("amount")
|
70 |
|
|
|
|
|
|
|
|
|
|
|
71 |
print(matrix)
|
72 |
|
73 |
+
if not should_checkout:
|
74 |
+
return
|
75 |
+
|
76 |
matrix_copy = [[0 for _ in range(len(matrix))] for _ in range(len(matrix))]
|
77 |
|
78 |
for i in range(len(matrix_copy)):
|
|
|
83 |
for r in matrix_copy:
|
84 |
print(r)
|
85 |
|
86 |
+
result = []
|
87 |
|
88 |
for i in range(len(matrix_copy)):
|
89 |
for j in range(len(matrix_copy)):
|
90 |
amount = matrix_copy[i][j]
|
91 |
if amount > 0:
|
92 |
+
result.append({"from": get_username(users[i]), "to": get_username(users[j]), "amount": amount})
|
93 |
if amount < 0:
|
94 |
+
result.append({"from": get_username(users[j]), "to": get_username(users[i]), "amount": -amount})
|
95 |
+
|
96 |
+
sorted_result = sorted(result, key=lambda x: x["from"])
|
97 |
|
98 |
+
for r in sorted_result:
|
99 |
print(r)
|
100 |
|
101 |
+
bubble = get_checkout_bubble(sorted_result)
|
102 |
+
print(bubble)
|
103 |
+
send_flex_text(payload.get("token"), bubble)
|
104 |
+
|
105 |
raise ValueError("Action Success")
|
106 |
|
107 |
if "$" not in payload.get("msg_text"):
|
|
|
142 |
send_text(payload.get("token"), f"$ {amount-paid} 未付清", data.get("quote_token"))
|
143 |
break
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
except Exception as e:
|
146 |
print(f"An error occurred: {e}")
|
147 |
|
|
|
189 |
|
190 |
|
191 |
def get_username(user_id: str):
|
192 |
+
if user_id == "others":
|
193 |
+
return "其他人"
|
194 |
url = f"https://api.line.me/v2/bot/group/{GROUP_ID}/member/{user_id}"
|
195 |
try:
|
196 |
res_json = requests.get(url, headers={"Authorization": "Bearer " + LINE_CHANNEL_ACCESS_TOKEN}).json()
|
197 |
return res_json.get("displayName")
|
198 |
except:
|
199 |
+
return "未知"
|
200 |
|
201 |
|
202 |
def get_users_number() -> int:
|
|
|
223 |
|
224 |
def get_summary_bubble(title: str, payer: str, borrowers: list):
|
225 |
total = 0
|
226 |
+
borrower_contents = []
|
227 |
|
228 |
for borrower in borrowers:
|
229 |
amount = float(borrower.get("amount"))
|
230 |
total += amount
|
231 |
+
borrower_contents.append({
|
232 |
"type": "box",
|
233 |
"layout": "horizontal",
|
234 |
"contents": [
|
|
|
279 |
},
|
280 |
{
|
281 |
"type": "separator",
|
282 |
+
"margin": "lg"
|
283 |
},
|
284 |
{
|
285 |
"type": "box",
|
286 |
"layout": "vertical",
|
287 |
+
"margin": "lg",
|
288 |
"spacing": "sm",
|
289 |
+
"contents": borrower_contents
|
290 |
},
|
291 |
{
|
292 |
"type": "separator",
|
|
|
304 |
},
|
305 |
{
|
306 |
"type": "text",
|
307 |
+
"text": f"${round(total,2)}",
|
308 |
"size": "sm",
|
309 |
"color": "#111111",
|
310 |
"align": "end",
|
|
|
323 |
}
|
324 |
|
325 |
|
326 |
+
def get_checkout_bubble(checkout_list: list):
|
327 |
+
|
328 |
+
current_user = checkout_list[0].get("from")
|
329 |
+
current_total = 0
|
330 |
+
current_pay_to_contents = []
|
331 |
+
carousel_contents = []
|
332 |
+
|
333 |
+
for item in checkout_list:
|
334 |
+
|
335 |
+
if current_user != item.get("from"):
|
336 |
+
carousel_contents.append({
|
337 |
+
"type": "bubble",
|
338 |
+
"body": {
|
339 |
+
"type": "box",
|
340 |
+
"layout": "vertical",
|
341 |
+
"contents": [
|
342 |
+
{
|
343 |
+
"type": "text",
|
344 |
+
"text": "結算明細",
|
345 |
+
"weight": "bold",
|
346 |
+
"color": "#1DB446",
|
347 |
+
"size": "sm"
|
348 |
+
},
|
349 |
+
{
|
350 |
+
"type": "text",
|
351 |
+
"text": current_user,
|
352 |
+
"weight": "bold",
|
353 |
+
"size": "xxl",
|
354 |
+
"margin": "md"
|
355 |
+
},
|
356 |
+
{
|
357 |
+
"type": "text",
|
358 |
+
"text": "需付款給:",
|
359 |
+
"size": "sm",
|
360 |
+
"color": "#aaaaaa",
|
361 |
+
"wrap": True,
|
362 |
+
"margin": "lg"
|
363 |
+
},
|
364 |
+
{
|
365 |
+
"type": "box",
|
366 |
+
"layout": "vertical",
|
367 |
+
"margin": "md",
|
368 |
+
"spacing": "sm",
|
369 |
+
"contents": current_pay_to_contents},
|
370 |
+
{
|
371 |
+
"type": "separator",
|
372 |
+
"margin": "lg"
|
373 |
+
},
|
374 |
+
{
|
375 |
+
"type": "box",
|
376 |
+
"layout": "horizontal",
|
377 |
+
"contents": [
|
378 |
+
{
|
379 |
+
"type": "text",
|
380 |
+
"text": "合計",
|
381 |
+
"size": "sm",
|
382 |
+
"color": "#555555"
|
383 |
+
},
|
384 |
+
{
|
385 |
+
"type": "text",
|
386 |
+
"text": f"${round(current_total,2)}",
|
387 |
+
"size": "sm",
|
388 |
+
"color": "#111111",
|
389 |
+
"align": "end",
|
390 |
+
"weight": "bold"
|
391 |
+
}
|
392 |
+
],
|
393 |
+
"margin": "lg"
|
394 |
+
}
|
395 |
+
]
|
396 |
+
},
|
397 |
+
"styles": {
|
398 |
+
"footer": {
|
399 |
+
"separator": True
|
400 |
+
}
|
401 |
+
}
|
402 |
+
}
|
403 |
+
)
|
404 |
+
|
405 |
+
current_user = item.get("from")
|
406 |
+
current_total = 0
|
407 |
+
current_pay_to_contents = []
|
408 |
+
|
409 |
+
amount = float(item.get("amount"))
|
410 |
+
current_total += amount
|
411 |
+
|
412 |
+
current_pay_to_contents.append({
|
413 |
+
"type": "box",
|
414 |
+
"layout": "horizontal",
|
415 |
+
"contents": [
|
416 |
+
{
|
417 |
+
"type": "text",
|
418 |
+
"text": item.get("to"),
|
419 |
+
"size": "sm",
|
420 |
+
"color": "#555555",
|
421 |
+
"flex": 0
|
422 |
+
},
|
423 |
+
{
|
424 |
+
"type": "text",
|
425 |
+
"text": f"${round(amount,2)}",
|
426 |
+
"size": "sm",
|
427 |
+
"color": "#111111",
|
428 |
+
"align": "end"
|
429 |
+
}
|
430 |
+
]
|
431 |
+
},)
|
432 |
+
|
433 |
+
return {
|
434 |
+
"type": "carousel",
|
435 |
+
"contents": carousel_contents
|
436 |
+
}
|
437 |
+
|
438 |
+
|
439 |
if __name__ == "__main__":
|
440 |
app.run(host="0.0.0.0", port=7860)
|