trill_og.sol
12/17/2024, 4:55 PMAI Evy
12/17/2024, 4:55 PMAI Evy
12/17/2024, 4:56 PMAI Evy
12/17/2024, 4:56 PMpython
# Install the SDK: https://docs.retellai.com/get-started/sdk
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from retell import Retell
retell = Retell(api_key=os.environ["RETELL_API_KEY"])
@app.post("/webhook")
async def handle_webhook(request: Request):
try:
post_data = await request.json()
valid_signature = retell.verify(
json.dumps(post_data, separators=(",", ":"), ensure_ascii=False),
api_key=str(os.environ["RETELL_API_KEY"]),
signature=str(request.headers.get("X-Retell-Signature")),
)
if not valid_signature:
print(
"Received Unauthorized",
post_data["event"],
post_data["data"]["call_id"],
)
return JSONResponse(status_code=401, content={"message": "Unauthorized"})
if post_data["event"] == "call_started":
print("Call started event", post_data["data"]["call_id"])
elif post_data["event"] == "call_ended":
print("Call ended event", post_data["data"]["call_id"])
elif post_data["event"] == "call_analyzed":
print("Call analyzed event", post_data["data"]["call_id"])
else:
print("Unknown event", post_data["event"])
return JSONResponse(status_code=204)
except Exception as err:
print(f"Error in webhook: {err}")
return JSONResponse(
status_code=500, content={"message": "Internal Server Error"}
)AI Evy
12/17/2024, 4:56 PM/webhook endpoint path [(4)]() .AI Evy
12/17/2024, 4:56 PM