cartier5
07/19/2024, 1:06 AMimport React, { useState, useEffect, useCallback } from 'react';
import { RetellWebClient } from "retell-client-js-sdk";
import { Mic, PhoneOff } from 'lucide-react';
import {api} from './axiosInstance'
const RetellAIWebCall = () => {
const [isCallActive, setIsCallActive] = useState(false);
const [status, setStatus] = useState('Click to start call');
const [retellWebClient, setRetellWebClient] = useState(null);
const AGENT_ID = '';
useEffect(() => {
const client = new RetellWebClient();
setRetellWebClient(client);
return () => {
if (client) {
client.stopCall();
}
};
}, []);
const setupEventListeners = useCallback((client) => {
client.on("call_started", () => {
console.log("call started");
setStatus("Call in progress");
});
client.on("call_ended", () => {
console.log("call ended");
setIsCallActive(false);
setStatus("Call ended");
});
client.on("agent_start_talking", () => {
console.log("agent_start_talking");
setStatus("AI is speaking...");
});
client.on("agent_stop_talking", () => {
console.log("agent_stop_talking");
setStatus("AI finished speaking");
});
client.on("update", (update) => {
console.log("Update:", update);
});
client.on("error", (error) => {
console.error("An error occurred:", error);
setStatus(`Error: ${error.message}`);
client.stopCall();
});
// Setup event listeners
client.on("conversationStarted", () => {
console.log("conversationStarted");
});
// client.on("audio", (audio: Uint8Array) => {
client.on("audio", (audio) => {
console.log("There is audio");
});
client.on("conversationEnded", ({ code, reason }) => {
console.log("Closed with code:", code, ", reason:", reason);
setIsCallActive(false);
});
client.on("error", (error) => {
console.error("An error occurred:", error);
setIsCallActive(false);
});
client.on("update", (update) => {
// Print live transcript as needed
console.log("update", update);
});
}, []);
const startCall = async () => {
if (!retellWebClient) return;
try {
setStatus("Initializing call...");
// Request access token from your backend using Axios
const response = await api.post('/create-web-call', {
agent_id: AGENT_ID,
// Add any metadata or retell_llm_dynamic_variables if needed
});
setupEventListeners(retellWebClient);
await retellWebClient.startCall({
accessToken: response.data.access_token,
sampleRate: 24000,
});
setIsCallActive(true);
} catch (error) {
console.error("Failed to start call:", error);
setStatus(`Failed to start call: ${error.response?.data?.detail || error.message}`);
}
};
const stopCall = () => {
if (retellWebClient) {
retellWebClient.stopCall();
setIsCallActive(false);
setStatus("Call ended");
}
};
const toggleCall = () => {
if (isCallActive) {
stopCall();
} else {
startCall();
}
};
return (
<div className="flex flex-col items-center justify-center min-h-screen bg-black text-green-500">
<button
onClick={toggleCall}
className={`w-24 h-24 rounded-full flex items-center justify-center transition-all duration-300 mb-4 ${
isCallActive ? 'bg-red-500' : 'bg-green-500 hover:bg-green-600'
}`}
>
{isCallActive ? <PhoneOff size={48} color="black" /> : <Mic size={48} color="black" />}
</button>
<p className="mt-4 text-center">{status}</p>
</div>
);
};
export default RetellAIWebCall;