commit 62ec400ef4507626404fff4cb63e5fdc05aaba23
parent 82f124703320463686777934c830374d9ada3e24
Author: Henry Unite <unitehenry@gmail.com>
Date: Thu, 26 Feb 2026 08:37:47 +0000
refactor: voice
Diffstat:
| M | app.ts | | | 60 | ++---------------------------------------------------------- |
| A | voice.ts | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 56 insertions(+), 58 deletions(-)
diff --git a/app.ts b/app.ts
@@ -1,70 +1,14 @@
import express, { type Request, type Response } from "express";
import log from "./log.ts";
import message from './message.ts';
+import voice from './voice.ts';
import { prompt } from "./prompt.ts";
const app = express();
app.use(express.urlencoded());
-function agentResponse(res: Response, message: string): void {
- res.set("Content-Type", "text/xml");
-
- res.send(`<?xml version="1.0" encoding="UTF-8"?>
- <Response>
- <Gather input="speech" action="/" method="POST">
- <Say>${message}</Say>
- </Gather>
- </Response>
- `);
-}
-
-function smsResponse(res: Response, message: string): void {
- res.set("Content-Type", "text/xml");
-
- res.send(`<?xml version="1.0" encoding="UTF-8"?>
- <Response>
- <Message>
- <Body>${message}</Body>
- </Message>
- </Response>
- `);
-}
-
-function hangup(res: Response): void {
- res.set("Content-Type", "text/xml");
-
- res.send(
- '<?xml version="1.0" encoding="UTF-8"?><Response><Hangup/></Response>',
- );
-}
-
-app.post("/voice", async (req: Request, res: Response): Promise<void> => {
- const callId: string = req.body.CallSid;
-
- log("INFO", `Call webhook received`, { callId });
-
- const speechResult: string = req.body.SpeechResult;
-
- if (speechResult) {
- log("INFO", "Caller message received", { callId, speechResult });
-
- const { sessionId, text } = await prompt(
- speechResult,
- app.get(callId) as string | null,
- );
-
- log("INFO", "Agent responded", { callId, sessionId, text });
-
- app.set(callId, sessionId);
-
- log("INFO", "Session cached", { callId, sessionId });
-
- agentResponse(res, text);
- } else {
- agentResponse(res, "Please say something.");
- }
-});
+app.post("/voice", voice);
app.post("/message", message);
diff --git a/voice.ts b/voice.ts
@@ -0,0 +1,54 @@
+import { type Request, type Response } from "express";
+import { prompt } from './prompt.ts';
+import log from './log.ts';
+
+function buildResponse(message : string) : string {
+ return `<?xml version="1.0" encoding="UTF-8"?>
+ <Response>
+ <Gather input="speech" action="/" method="POST">
+ <Say>${message}</Say>
+ </Gather>
+ </Response>
+ `;
+}
+
+export default async (req: Request, res: Response): Promise<void> => {
+ const app = req.app;
+
+ const callId: string = req.body.CallSid;
+
+ log("INFO", `Call webhook received`, { callId });
+
+ const speechResult: string = req.body.SpeechResult;
+
+ if (!speechResult) {
+ log('INFO', 'No speech result received');
+
+ const response = buildResponse('Please say something.');
+
+ res.set("Content-Type", "text/xml");
+
+ res.send(response);
+
+ return;
+ }
+
+ log("INFO", "Caller message received", { callId, speechResult });
+
+ const { sessionId, text } = await prompt(
+ speechResult,
+ app.get(callId) as string | null,
+ );
+
+ log("INFO", "Agent responded", { callId, sessionId, text });
+
+ app.set(callId, sessionId);
+
+ log("INFO", "Session cached", { callId, sessionId });
+
+ const response = buildResponse('Please say something.');
+
+ res.set("Content-Type", "text/xml");
+
+ res.send(response);
+}