|
| 1 | +from fastapi import FastAPI, Body, Form |
| 2 | +from fastapi.responses import HTMLResponse, JSONResponse |
| 3 | +from typing import List, Dict |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +app = FastAPI() |
| 7 | +MESSAGES: List[Dict] = [] |
| 8 | + |
| 9 | +@app.post("/inbox") |
| 10 | +async def inbox(payload: Dict = Body(...)): |
| 11 | + payload["ts"] = datetime.utcnow().isoformat() + "Z" |
| 12 | + MESSAGES.append(payload) |
| 13 | + if len(MESSAGES) > 100: |
| 14 | + del MESSAGES[:-100] |
| 15 | + return {"ok": True} |
| 16 | + |
| 17 | +@app.get("/inbox") |
| 18 | +def list_inbox(): |
| 19 | + return {"messages": MESSAGES} |
| 20 | + |
| 21 | +# Twilio-compatible endpoint |
| 22 | +@app.post("/2010-04-01/Accounts/{sid}/Messages.json") |
| 23 | +async def send_sms(sid: str, To: str = Form(...), From: str = Form(...), Body: str = Form(...)): |
| 24 | + print(f"[FAKE-TWILIO] SID={sid} To={To} From={From} Body={Body}") |
| 25 | + MESSAGES.append({ |
| 26 | + "to": To, |
| 27 | + "frm": From, |
| 28 | + "body": Body, |
| 29 | + "ts": datetime.utcnow().isoformat() + "Z" |
| 30 | + }) |
| 31 | + return JSONResponse(status_code=201, content={ |
| 32 | + "sid": "SM_fake123", |
| 33 | + "status": "queued", |
| 34 | + "to": To, |
| 35 | + "from": From, |
| 36 | + "body": Body, |
| 37 | + }) |
| 38 | + |
| 39 | +@app.get("/", response_class=HTMLResponse) |
| 40 | +def home(): |
| 41 | + rows = "".join( |
| 42 | + f"<tr><td>{m.get('to','')}</td><td>{m.get('frm','')}</td><td>{m.get('body','')}</td><td>{m.get('ts','')}</td></tr>" |
| 43 | + for m in reversed(MESSAGES) |
| 44 | + ) |
| 45 | + return f""" |
| 46 | + <html><head><meta charset="utf-8"><title>SMS Capture</title> |
| 47 | + <style>table{{width:100%;border-collapse:collapse}}td,th{{border:1px solid #333;padding:6px}}</style> |
| 48 | + </head><body> |
| 49 | + <h3>Captured SMS (dev)</h3> |
| 50 | + <table><thead><tr><th>To</th><th>From</th><th>Body</th><th>Time (UTC)</th></tr></thead><tbody>{rows}</tbody></table> |
| 51 | + </body></html>""" |
0 commit comments