yuripeyamashita commited on
Commit
0de3336
·
verified ·
1 Parent(s): 52c763e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -3
app.py CHANGED
@@ -96,10 +96,15 @@ def api():
96
  print(amount, paid)
97
 
98
  if amount-paid <= 1 and payload.get("quoted_msg_id") == msg_id:
99
- s = f'{data.get("msg_text")} paid by {get_username(data.get("user_id"))}\n\n'
 
100
  for _, q_data in quoted_msg_list.items():
101
- s += f'{get_username(q_data.get("user_id"))} : {q_data.get("amount")}\n'
102
- send_text(payload.get("token"), s, data.get("quote_token"))
 
 
 
 
103
  break
104
 
105
  if amount-paid > 1 and payload.get("quoted_msg_id") != msg_id:
@@ -152,6 +157,20 @@ def send_text(token: str, text: str, quote_token: str | None = None):
152
  })
153
 
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  def get_username(user_id: str):
156
  url = f"https://api.line.me/v2/bot/group/{GROUP_ID}/member/{user_id}"
157
  try:
@@ -183,5 +202,107 @@ def get_amount(text: str) -> float | None:
183
  return None
184
 
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  if __name__ == "__main__":
187
  app.run(host="0.0.0.0", port=7860)
 
96
  print(amount, paid)
97
 
98
  if amount-paid <= 1 and payload.get("quoted_msg_id") == msg_id:
99
+ # s = f'{data.get("msg_text")} paid by {get_username(data.get("user_id"))}\n\n'
100
+ borrowers = []
101
  for _, q_data in quoted_msg_list.items():
102
+ borrowers.append({"name": get_username(q_data.get("user_id")), "amount": q_data.get("amount")})
103
+ # s += f'{get_username(q_data.get("user_id"))} : {q_data.get("amount")}\n'
104
+
105
+ # send_text(payload.get("token"), s, data.get("quote_token"))
106
+ bubble = get_summary_bubble(data.get("msg_text"), get_username(data.get("user_id")), borrowers)
107
+ send_flex_text(payload.get("token"), bubble)
108
  break
109
 
110
  if amount-paid > 1 and payload.get("quoted_msg_id") != msg_id:
 
157
  })
158
 
159
 
160
+ def send_flex_text(token, bubble):
161
+ requests.post("https://api.line.me/v2/bot/message/reply", headers={
162
+ "Content-Type": "application/json; charset=UTF-8",
163
+ "Authorization": "Bearer " + LINE_CHANNEL_ACCESS_TOKEN
164
+ }, json={
165
+ "replyToken": token,
166
+ "messages": [{
167
+ "type": "flex",
168
+ "altText": "您有一則新訊息",
169
+ "contents": bubble
170
+ }]
171
+ })
172
+
173
+
174
  def get_username(user_id: str):
175
  url = f"https://api.line.me/v2/bot/group/{GROUP_ID}/member/{user_id}"
176
  try:
 
202
  return None
203
 
204
 
205
+ def get_summary_bubble(title: str, payer: str, borrowers: list):
206
+ total = 0
207
+ borrower_list = []
208
+
209
+ for borrower in borrowers:
210
+ amount = float(borrower.get("amount"))
211
+ total += amount
212
+ borrower_list.append({
213
+ "type": "box",
214
+ "layout": "horizontal",
215
+ "contents": [
216
+ {
217
+ "type": "text",
218
+ "text": borrower.get("name"),
219
+ "size": "sm",
220
+ "color": "#555555",
221
+ "flex": 0
222
+ },
223
+ {
224
+ "type": "text",
225
+ "text": f"${amount}",
226
+ "size": "sm",
227
+ "color": "#111111",
228
+ "align": "end"
229
+ }
230
+ ]
231
+ })
232
+
233
+ return {
234
+ "type": "bubble",
235
+ "body": {
236
+ "type": "box",
237
+ "layout": "vertical",
238
+ "contents": [
239
+ {
240
+ "type": "text",
241
+ "text": "對帳明細",
242
+ "weight": "bold",
243
+ "color": "#1DB446",
244
+ "size": "sm"
245
+ },
246
+ {
247
+ "type": "text",
248
+ "text": title,
249
+ "weight": "bold",
250
+ "size": "xxl",
251
+ "margin": "md"
252
+ },
253
+ {
254
+ "type": "text",
255
+ "text": f"由 {payer} 付款",
256
+ "size": "sm",
257
+ "color": "#aaaaaa",
258
+ "wrap": True,
259
+ "weight": "bold"
260
+ },
261
+ {
262
+ "type": "separator",
263
+ "margin": "xl"
264
+ },
265
+ {
266
+ "type": "box",
267
+ "layout": "vertical",
268
+ "margin": "xl",
269
+ "spacing": "sm",
270
+ "contents": borrower_list
271
+ },
272
+ {
273
+ "type": "separator",
274
+ "margin": "lg"
275
+ },
276
+ {
277
+ "type": "box",
278
+ "layout": "horizontal",
279
+ "contents": [
280
+ {
281
+ "type": "text",
282
+ "text": "合計",
283
+ "size": "sm",
284
+ "color": "#555555"
285
+ },
286
+ {
287
+ "type": "text",
288
+ "text": f"${total}",
289
+ "size": "sm",
290
+ "color": "#111111",
291
+ "align": "end",
292
+ "weight": "bold"
293
+ }
294
+ ],
295
+ "margin": "lg"
296
+ }
297
+ ]
298
+ },
299
+ "styles": {
300
+ "footer": {
301
+ "separator": True
302
+ }
303
+ }
304
+ }
305
+
306
+
307
  if __name__ == "__main__":
308
  app.run(host="0.0.0.0", port=7860)