Pepguy commited on
Commit
0bf76da
Β·
verified Β·
1 Parent(s): e7cbf71

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +84 -1
app.js CHANGED
@@ -334,4 +334,87 @@ app.post("/webhook/call", async (req, res) => {
334
  const voice = getActiveVoice();
335
  const exactPhrase = encodeURIComponent("am I speaking with the business owner?");
336
  await telnyxCmd("playback_start", {
337
- audio_url: `${AI_SERVER_URL}/play-aud
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  const voice = getActiveVoice();
335
  const exactPhrase = encodeURIComponent("am I speaking with the business owner?");
336
  await telnyxCmd("playback_start", {
337
+ audio_url: `${AI_SERVER_URL}/play-audio/${voice}/${exactPhrase}`,
338
+ target_legs: "self"
339
+ });
340
+ }, 1500);
341
+
342
+ // ── FIX: Clean auto-hangup (no client_state) ──
343
+ setTimeout(async () => {
344
+ log("HANGUP", `Enforcing 60-second limit on call ${callId}`);
345
+ await telnyxCmd("hangup", {});
346
+ }, 60000);
347
+ break;
348
+
349
+ case "call.transcription":
350
+ const data = payload?.transcription_data;
351
+ if (data && data.is_final) {
352
+ const transcript = data.transcript;
353
+ const voice = getActiveVoice();
354
+ log("TRANSCRIPT", `User said: "${transcript}"`);
355
+ updateLog({ last_transcript: transcript });
356
+
357
+ try {
358
+ const aiRes = await fetch(`${AI_SERVER_URL}/api/process-text-turn`, {
359
+ method: "POST",
360
+ headers: { "Content-Type": "application/json" },
361
+ body: JSON.stringify({ text: transcript, voice: voice })
362
+ });
363
+
364
+ if (aiRes.ok) {
365
+ const aiData = await aiRes.json();
366
+ if (aiData.speak_url) {
367
+ log("AI_REPLY", `Instructing Telnyx to play generated reply.`);
368
+ await telnyxCmd("playback_start", {
369
+ audio_url: aiData.speak_url,
370
+ target_legs: "self"
371
+ });
372
+ }
373
+ }
374
+ } catch (e) {
375
+ log("AI_SERVER_ERROR", e.message);
376
+ }
377
+ }
378
+ break;
379
+
380
+ case "call.recording.saved":
381
+ const rawUrl = payload.recording_urls.mp3;
382
+ log("RECORDING", `Saved. URL: ${rawUrl}`);
383
+ updateLog({
384
+ status: "completed",
385
+ raw_telnyx_url: rawUrl,
386
+ recording_url: `/play-recording/${callId}`
387
+ });
388
+ break;
389
+
390
+ case "call.hangup":
391
+ updateLog({ status: "completed" });
392
+ log("CALL ENDED", `Call ${callId} has cleanly terminated.`);
393
+ break;
394
+ }
395
+
396
+ res.sendStatus(200);
397
+ });
398
+
399
+ // ─── SMS WEBHOOK ─────────────────────────────
400
+ app.post("/webhook/sms", (req, res) => {
401
+ const type = req.body?.data?.event_type;
402
+ if (type === "message.received") {
403
+ log("SMS INBOUND", `from=${req.body?.data?.payload?.from?.phone_number}`);
404
+ }
405
+ res.sendStatus(200);
406
+ });
407
+
408
+ wss.on("connection", (ws, req) => {
409
+ log("WS", `Legacy media connection from ${req.socket.remoteAddress}`);
410
+ ws.on("message", (raw) => {});
411
+ });
412
+
413
+ const requiredEnv = ["TELNYX_API_KEY", "TELNYX_CONNECTION_ID", "TELNYX_FROM_NUMBER", "AI_SERVER_URL"];
414
+ requiredEnv.forEach((key) => {
415
+ if (!process.env[key]) log("STARTUP WARNING", `Missing env var: ${key}`);
416
+ });
417
+
418
+ server.listen(PORT, "0.0.0.0", () => {
419
+ log("STARTUP", `Telecom Server running on port ${PORT}`);
420
+ });