Skip to content

Commit ee2861e

Browse files
committed
feat: twilio endpoints
1 parent 95b5c0f commit ee2861e

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

twilio-shim/main.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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>"""

twilio-shim/requirements.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
annotated-types==0.7.0
2+
anyio==4.10.0
3+
click==8.2.1
4+
fastapi==0.116.1
5+
h11==0.16.0
6+
idna==3.10
7+
pydantic==2.11.7
8+
pydantic-core==2.33.2
9+
python-multipart==0.0.20
10+
sniffio==1.3.1
11+
starlette==0.47.2
12+
typing-extensions==4.14.1
13+
typing-inspection==0.4.1
14+
uvicorn==0.35.0

0 commit comments

Comments
 (0)