hang up
This commit is contained in:
@@ -1,15 +1,14 @@
|
|||||||
console.log('INIT ELEVENLABS')
|
|
||||||
|
|
||||||
import { Conversation } from 'https://cdn.skypack.dev/@elevenlabs/client';
|
import { Conversation } from 'https://cdn.skypack.dev/@elevenlabs/client';
|
||||||
|
|
||||||
|
|
||||||
let conversation = null;
|
let conversation = null;
|
||||||
|
let inCall = false;
|
||||||
|
|
||||||
console.log("INIT")
|
|
||||||
document.addEventListener('JSsucks', () => {
|
document.addEventListener('JSsucks', () => {
|
||||||
console.log("JSsucks")
|
|
||||||
const callBtn = document.getElementById('callBtn');
|
const callBtn = document.getElementById('callBtn');
|
||||||
|
|
||||||
callBtn.addEventListener('click', async () => {
|
callBtn.addEventListener('click', async () => {
|
||||||
|
if (!inCall) {
|
||||||
console.log('Requesting microphone access...');
|
console.log('Requesting microphone access...');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -22,15 +21,18 @@ callBtn.addEventListener('click', async () => {
|
|||||||
console.log('Connecting to agent...');
|
console.log('Connecting to agent...');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
conversation = await Conversation.startSession({
|
conversation = await Conversation.startSession({
|
||||||
agentId: 'agent_01jx2xm2w4exwvjhbq52js687f',
|
agentId: 'agent_01jx2xm2w4exwvjhbq52js687f',
|
||||||
connectionType: 'websocket', // or "webrtc"
|
connectionType: 'websocket',
|
||||||
onConnect: () => {
|
onConnect: () => {
|
||||||
console.log('✅ Connected to AI Agent!');
|
console.log('✅ Connected to AI Agent!');
|
||||||
|
callBtn.style.color = 'red';
|
||||||
|
inCall = true;
|
||||||
},
|
},
|
||||||
onDisconnect: () => {
|
onDisconnect: () => {
|
||||||
console.log('🔌 Disconnected.');
|
console.log('🔌 Disconnected.');
|
||||||
|
callBtn.style.color = 'grey';
|
||||||
|
inCall = false;
|
||||||
},
|
},
|
||||||
onError: (err) => {
|
onError: (err) => {
|
||||||
console.log(`❌ Error: ${err.message || err}`);
|
console.log(`❌ Error: ${err.message || err}`);
|
||||||
@@ -43,6 +45,18 @@ callBtn.addEventListener('click', async () => {
|
|||||||
console.log('Failed to start session.');
|
console.log('Failed to start session.');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
})
|
} else {
|
||||||
|
console.log('Hanging up...');
|
||||||
|
try {
|
||||||
|
await conversation.endSession();
|
||||||
|
conversation = null;
|
||||||
|
inCall = false;
|
||||||
|
callBtn.style.color = 'grey';
|
||||||
|
console.log('✅ Call ended.');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error ending the call:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user