userbymahadi commited on
Commit
d82c270
·
verified ·
1 Parent(s): 5e8aaea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -89
app.py CHANGED
@@ -1,10 +1,6 @@
1
- from fastapi import FastAPI, Request, UploadFile, File
2
- from fastapi.responses import StreamingResponse
3
  import threading
4
- import os
5
- import zipfile
6
  import time
7
- from io import BytesIO
8
 
9
  app = FastAPI()
10
  lock = threading.Lock()
@@ -13,101 +9,50 @@ lock = threading.Lock()
13
  channels = {}
14
  update_ids = {}
15
 
16
- file_channels = {}
17
 
18
- UPLOAD_DIR = "uploads"
19
- os.makedirs(UPLOAD_DIR, exist_ok=True)
20
-
21
-
22
- # -------- SEND TEXT --------
23
  @app.post("/sendMessage/{channel}")
24
- async def send_message(channel: str, req: Request):
25
- data = await req.body()
26
- text = data.decode("utf-8", errors="ignore").strip()
27
-
28
- with lock:
29
- if channel not in channels:
30
- channels[channel] = []
31
- update_ids[channel] = 0
32
-
33
- update_ids[channel] += 1
34
-
35
- channels[channel].append({
36
- "update_id": update_ids[channel],
37
- "text": text
38
- })
39
-
40
- return {"ok": True}
41
-
42
-
43
- # -------- SEND FILE --------
44
- @app.post("/sendFile/{channel}")
45
- async def send_file(channel: str, files: list[UploadFile] = File(...)):
46
- saved_files = []
47
 
48
- with lock:
49
- if channel not in file_channels:
50
- file_channels[channel] = []
51
 
52
- for file in files:
53
- filename = f"{int(time.time()*1000)}_{file.filename}"
54
- filepath = os.path.join(UPLOAD_DIR, filename)
 
55
 
56
- try:
57
- content = await file.read()
58
- with open(filepath, "wb") as f:
59
- f.write(content)
60
 
61
- with lock:
62
- file_channels[channel].append(filepath)
 
 
 
63
 
64
- saved_files.append(filename)
65
 
66
- except Exception as e:
67
- saved_files.append(f"error:{file.filename}")
68
 
69
- return {"ok": True, "files": saved_files}
70
 
71
-
72
- # -------- GET UPDATES (TEXT + FILE) --------
73
  @app.get("/getUpdates/{channel}")
74
  def get_updates(channel: str):
 
 
 
 
75
 
76
- with lock:
77
- msgs = channels.get(channel, []).copy()
78
- channels[channel] = []
79
-
80
- files = file_channels.get(channel, []).copy()
81
- file_channels[channel] = []
82
-
83
- # -------- CASE 1: FILE EXISTS --------
84
- if files:
85
- mem_zip = BytesIO()
86
-
87
- with zipfile.ZipFile(mem_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
88
-
89
- # add files
90
- for file_path in files:
91
- if os.path.exists(file_path):
92
- zipf.write(file_path, os.path.basename(file_path))
93
-
94
- # add text হিসেবে messages.txt
95
- if msgs:
96
- text_content = "\n".join([m.get("text", "") for m in msgs])
97
- zipf.writestr("messages.txt", text_content)
98
-
99
- mem_zip.seek(0)
100
-
101
- return StreamingResponse(
102
- mem_zip,
103
- media_type="application/zip",
104
- headers={
105
- "Content-Disposition": f"attachment; filename={channel}_data.zip"
106
- }
107
- )
108
 
109
- # -------- CASE 2: ONLY TEXT --------
110
- return {
111
- "ok": True,
112
- "result": msgs if msgs else []
113
- }
 
1
+ from fastapi import FastAPI, Request, Form
 
2
  import threading
 
 
3
  import time
 
4
 
5
  app = FastAPI()
6
  lock = threading.Lock()
 
9
  channels = {}
10
  update_ids = {}
11
 
 
12
 
13
+ # -------- SEND MESSAGE --------
 
 
 
 
14
  @app.post("/sendMessage/{channel}")
15
+ async def send_message(channel: str, text: str = Form(None), req: Request = None):
16
+ try:
17
+ # support both form-data & raw body
18
+ if not text:
19
+ data = await req.body()
20
+ text = data.decode("utf-8", errors="ignore").strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ if not text:
23
+ return {"ok": False, "error": "Empty message"}
 
24
 
25
+ with lock:
26
+ if channel not in channels:
27
+ channels[channel] = []
28
+ update_ids[channel] = 0
29
 
30
+ update_ids[channel] += 1
 
 
 
31
 
32
+ channels[channel].append({
33
+ "update_id": update_ids[channel],
34
+ "text": text,
35
+ "time": int(time.time())
36
+ })
37
 
38
+ return {"ok": True}
39
 
40
+ except Exception as e:
41
+ return {"ok": False, "error": str(e)}
42
 
 
43
 
44
+ # -------- GET UPDATES (consume-on-read) --------
 
45
  @app.get("/getUpdates/{channel}")
46
  def get_updates(channel: str):
47
+ try:
48
+ with lock:
49
+ msgs = channels.get(channel, []).copy()
50
+ channels[channel] = []
51
 
52
+ return {
53
+ "ok": True,
54
+ "result": msgs if msgs else []
55
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ except Exception as e:
58
+ return {"ok": False, "error": str(e)}