from fastapi import FastAPI, Request, Form import threading import time app = FastAPI() lock = threading.Lock() # -------- STORAGE -------- channels = {} update_ids = {} # -------- SEND MESSAGE -------- @app.post("/sendMessage/{channel}") async def send_message(channel: str, text: str = Form(None), req: Request = None): try: # support both form-data & raw body if not text: data = await req.body() text = data.decode("utf-8", errors="ignore").strip() if not text: return {"ok": False, "error": "Empty message"} with lock: if channel not in channels: channels[channel] = [] update_ids[channel] = 0 update_ids[channel] += 1 channels[channel].append({ "update_id": update_ids[channel], "text": text, "time": int(time.time()) }) return {"ok": True} except Exception as e: return {"ok": False, "error": str(e)} # -------- GET UPDATES (consume-on-read) -------- @app.get("/getUpdates/{channel}") def get_updates(channel: str): try: with lock: msgs = channels.get(channel, []).copy() channels[channel] = [] return { "ok": True, "result": msgs if msgs else [] } except Exception as e: return {"ok": False, "error": str(e)}