Hi everyone, What is the best (least race-conditi...
# egress
b
Hi everyone, What is the best (least race-conditiony) way to post both egress recordings and the call transcripts to my backend (Django but doens't matter): Right now I have a webhook for egress, and this egress code:
Copy code
timestamp = int(time.time())
company_id = os.getenv("COMPANY_ID")
filepath = f"inbound-calls/{company_id}/{timestamp}.mp4"

req = api.RoomCompositeEgressRequest(
    room_name=ctx.room.name,
    audio_only=True,
    file_outputs=[api.EncodedFileOutput(
        file_type=api.EncodedFileType.MP4,
        filepath=filepath,
        s3=api.S3Upload(
            bucket=os.getenv("AWS_BUCKET_NAME"),
            region=os.getenv("AWS_REGION"),
            access_key=os.getenv("AWS_ACCESS_KEY_ID"),
            secret=os.getenv("AWS_SECRET_ACCESS_KEY"),
        ),
    )],
)

if ENABLE_EGRESS:
    lkapi = api.LiveKitAPI()
    await lkapi.egress.start_room_composite_egress(req)
    await lkapi.aclose()
    logger.info(f"Recording started: {filepath}")
Django receives the webhook from livekit with all the data needed to add the recording path to a
Recording
model on the backend. And then to pull the transcript I have this code;
Copy code
async def get_chat_history():
    userdata = session.userdata

    report = ctx.make_session_report()
    report_dict = report.to_dict()
    room_id = report_dict['room_id']

    # Build clean raw chat history
    chat_items = report_dict['chat_history']['items']
    raw_chat_history = []
    for item in chat_items:
        if item['type'] == 'message' and not item.get('interrupted', False):
            role = item['role']
            text = item['content'][0] if item['content'] else ""
            raw_chat_history.append({
                "role": role,
                "text": text.strip()
            })

    structured_summary = {
        "customer_name": userdata.customer_name or None,
        "customer_phone": userdata.customer_phone or None,
        "customer_address": userdata.customer_address or None,
        "issue_description": userdata.issue_description or None
    }

    # Final payload — perfect for Django
    payload = {
        "room_id": room_id,
        "raw_chat_history": raw_chat_history,
        "summary": structured_summary,
        "session_ended_at": datetime.fromtimestamp(report_dict.get('timestamp', time.time())).isoformat(),
        "recording_path": filepath if ENABLE_EGRESS else None,
        "source": "voice_agent",
    }


    import httpx
    await httpx.post("<https://webhook.com/api/egress/{company_id}>", json=payload, timeout=10)
These will both update the same
Recording
model (at different times) Ideally there would be a way where I can deliver the transcript after egress is complete. There are many possibilities. Anyone cross this bridge? Thanks.