WIP - use dynamic client using the user id
This commit is contained in:
@@ -164,25 +164,40 @@ export class VoiceController {
|
|||||||
this.logger.log(`Full body: ${JSON.stringify(body)}`);
|
this.logger.log(`Full body: ${JSON.stringify(body)}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Extract tenant domain from phone number mapping
|
// Extract tenant domain from Host header
|
||||||
// For now, we'll need to determine which tenant owns this phone number
|
const host = req.headers.host || '';
|
||||||
// TODO: Add phone number to tenant mapping in database
|
const tenantDomain = host.split('.')[0]; // e.g., "tenant1" from "tenant1.routebox.co"
|
||||||
|
|
||||||
// For multi-tenant, we need to route to the correct user's browser Device
|
this.logger.log(`Extracted tenant domain: ${tenantDomain}`);
|
||||||
// The Device identity is the userId we used when generating the access token
|
|
||||||
|
// Get all connected users for this tenant
|
||||||
|
const connectedUsers = this.voiceGateway.getConnectedUsers(tenantDomain);
|
||||||
|
|
||||||
// For now, let's dial ALL connected clients (you can refine this later)
|
this.logger.log(`Connected users for tenant ${tenantDomain}: ${connectedUsers.length}`);
|
||||||
// Using a simple approach: dial the most recently connected user
|
|
||||||
|
if (connectedUsers.length === 0) {
|
||||||
|
// No users online - send to voicemail or play message
|
||||||
|
const twiml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Response>
|
||||||
|
<Say>Sorry, no agents are currently available. Please try again later.</Say>
|
||||||
|
<Hangup/>
|
||||||
|
</Response>`;
|
||||||
|
this.logger.log(`No users online - returning unavailable message`);
|
||||||
|
return res.type('text/xml').send(twiml);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build TwiML to dial all connected clients (first to answer gets the call)
|
||||||
|
const clientElements = connectedUsers.map(userId => ` <Client>${userId}</Client>`).join('\n');
|
||||||
|
|
||||||
const twiml = `<?xml version="1.0" encoding="UTF-8"?>
|
const twiml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<Response>
|
<Response>
|
||||||
<Say>Connecting your call</Say>
|
<Say>Connecting your call</Say>
|
||||||
<Dial>
|
<Dial timeout="30" action="${host}/api/voice/webhook/dial-status">
|
||||||
<Client>e6d45fa3-a108-4085-81e5-a8e05e85e6fb</Client>
|
${clientElements}
|
||||||
</Dial>
|
</Dial>
|
||||||
</Response>`;
|
</Response>`;
|
||||||
|
|
||||||
this.logger.log(`Returning inbound TwiML - dialing Client`);
|
this.logger.log(`Returning inbound TwiML - dialing ${connectedUsers.length} client(s)`);
|
||||||
res.type('text/xml').send(twiml);
|
res.type('text/xml').send(twiml);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
this.logger.error(`Error generating inbound TwiML: ${error.message}`);
|
this.logger.error(`Error generating inbound TwiML: ${error.message}`);
|
||||||
|
|||||||
@@ -290,4 +290,20 @@ export class VoiceGateway
|
|||||||
socket.emit('ai:action', data);
|
socket.emit('ai:action', data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get connected users for a tenant
|
||||||
|
*/
|
||||||
|
getConnectedUsers(tenantDomain?: string): string[] {
|
||||||
|
const userIds: string[] = [];
|
||||||
|
|
||||||
|
for (const [userId, socket] of this.connectedUsers.entries()) {
|
||||||
|
// If tenantDomain specified, filter by tenant
|
||||||
|
if (!tenantDomain || socket.tenantSlug === tenantDomain) {
|
||||||
|
userIds.push(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return userIds;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user