Skip to content

Commit 9e64149

Browse files
committed
feat: use selected voice for emergency contact calls
1 parent ea00f03 commit 9e64149

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/lib/voice-call.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,26 @@ export async function callEmergencyContact(elderlyProfileId: string) {
104104
return;
105105
}
106106

107-
const escapedName = escapeXml(profile.name);
108-
const twiml = `<Response>
109-
<Say>This is an urgent message from Seniora Care. We have been unable to reach ${escapedName} after multiple reminder call attempts. Please check on them as soon as possible. Thank you.</Say>
110-
</Response>`;
107+
const emergencyScript = `This is an urgent message from Seniora Care. We have been unable to reach ${profile.name} after multiple reminder call attempts. Please check on them as soon as possible. Thank you.`;
108+
109+
let twiml: string;
110+
try {
111+
const audioBuffer = await textToSpeech(emergencyScript, profile.voiceId || undefined);
112+
const audioDir = path.join(process.cwd(), "public", "audio");
113+
await mkdir(audioDir, { recursive: true });
114+
const audioFileName = `emergency-${profile.id}-${Date.now()}.mp3`;
115+
const audioPath = path.join(audioDir, audioFileName);
116+
await writeFile(audioPath, audioBuffer);
117+
const audioUrl = `${process.env.NEXT_PUBLIC_APP_URL}/api/audio/${audioFileName}`;
118+
119+
twiml = `<Response><Play>${audioUrl}</Play></Response>`;
120+
} catch (ttsError) {
121+
console.warn("ElevenLabs TTS failed for emergency call, using Twilio Say fallback:", ttsError);
122+
const escapedName = escapeXml(profile.name);
123+
twiml = `<Response>
124+
<Say>This is an urgent message from Seniora Care. We have been unable to reach ${escapedName} after multiple reminder call attempts. Please check on them as soon as possible. Thank you.</Say>
125+
</Response>`;
126+
}
111127

112128
try {
113129
await twilioClient.calls.create({

0 commit comments

Comments
 (0)