-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
62 lines (51 loc) · 1.71 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# main.py
import json
import uvicorn
from bot import run_bot
from fastapi import FastAPI, WebSocket
from fastapi.middleware.cors import CORSMiddleware
from twilio.twiml.voice_response import VoiceResponse, Connect, Stream
from fastapi.responses import Response
# Initialize FastAPI app
app = FastAPI()
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def handle_incoming_call():
"""
Handle incoming calls using Twilio Python client
Returns TwiML response for incoming call handling
"""
# Create a new TwiML response using the Python helper library
response = VoiceResponse()
# Add Connect verb with Stream
connect = Connect()
stream = Stream(url='wss://pipebot-twilio-051d2942e0ab.herokuapp.com/ws')
stream.parameter('timeout', '60')
stream.parameter('maxDuration', '14400')
connect.append(stream)
response.append(connect)
# Add Start verb with Stream
start = response.start()
start.stream(name='stream_1')
# Return the TwiML response
return Response(content=str(response), media_type="application/xml")
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
# Get initial stream data
start_data = websocket.iter_text()
await start_data.__anext__()
call_data = json.loads(await start_data.__anext__())
print(call_data, flush=True)
stream_sid = call_data["start"]["streamSid"]
print("WebSocket connection accepted")
await run_bot(websocket, stream_sid)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8765)