import express from "express";
const app = express();
app.ws("/llm-websocket/:call_id",
async (ws: WebSocket, req: Request) => {
// Retell server asks your LLM what to respond when user talks
ws.on("message", async (data: RawData, isBinary: boolean) => {
const response = JSON.parse(data.toString());
if (response.interaction_type === "response_required" {
// Your LLM answers user's question in text
const response = this.llmClient.DraftResponse(
response.response_id,
response.transcript,
response.interaction_type,
);
// Send LLM text response back to Retell server
ws.send(JSON.stringify(response));
}
})
}
);
import { RetellClient } from "retell-sdk";
const retell = new RetellClient({ apiKey: "YOUR_API_KEY" });
const agentRes = await retell.createAgent({
agentName: "Echo",
voiceId: "11labs-Ryan",
llmWebsocketUrl: "wss://your-server-url/",
});
console.log(agentRes.agent)
// Register twilio voice webhook
const RegisterTwilioApi = (app: expressWs.Application) => {
app.post("/twilio-voice-webhook", async (req: Request, res: Response) => {
const callResponse = await retellClient.registerCall({
agentId: process.env.RETELL_AGENT_ID,
audioWebsocketProtocol: AudioWebsocketProtocol.Twilio,
audioEncoding: AudioEncoding.Mulaw,
sampleRate: 8000,
});
// Start phone call websocket
const response = new VoiceResponse();
const start = response.connect();
const stream = start.stream({
url: `wss://${WsAddress}/websocket/${Response.Detail.Id}`,
});
res.set("Content-Type", "text/xml");
res.send(response.toString());
});
};
// Make a outbound call
const CreatePhoneCall = async (fromNumber: string, toNumber: string) => {
await twilioClient.calls.create({
url: `${ipAddress}/twilio-voice-webhook`,
to: toNumber,
from: fromNumber,
});
console.log(`Call from: ${fromNumber} to: ${toNumber}`);
};