From a958234388084955044a6e3aae43fb64264a9263 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Sun, 5 Mar 2023 04:56:59 -0500 Subject: [PATCH] fix: fixed duplicate call.js --- src/api/call.js | 7 ++- src/call.js | 123 ------------------------------------------------ 2 files changed, 5 insertions(+), 125 deletions(-) delete mode 100644 src/call.js diff --git a/src/api/call.js b/src/api/call.js index b30d716..943b711 100644 --- a/src/api/call.js +++ b/src/api/call.js @@ -19,8 +19,9 @@ import { transferSession, checkOperatorReady, postSummary, -} from "../lib/session.js"; -import { chat, summarize } from "../lib/chat.js"; + completeCall, +} from "./session.js"; +import { chat, summarize } from "./chat.js"; const app = Router(); app.use(json()); @@ -112,7 +113,9 @@ app.post("/summarize", async (req, res) => { app.post("/status", async (req, res) => { let callId = req.body.CallSid; let callStatus = req.body.CallStatus; + if (callStatus === "completed") { + console.log(`completing call ${callId}`); await completeCall(callId); } }); diff --git a/src/call.js b/src/call.js deleted file mode 100644 index 943b711..0000000 --- a/src/call.js +++ /dev/null @@ -1,123 +0,0 @@ -import * as dotenv from "dotenv"; -dotenv.config(); - -import { Router, json } from "express"; -import log from "loglevel"; - -import twilio from "twilio"; -const accountSid = process.env.TWILIO_ACCOUNT_SID; -const authToken = process.env.TWILIO_AUTH_TOKEN; -const phone_number = process.env.TWILIO_PHONE_NUMBER; -const VoiceResponse = twilio.twiml.VoiceResponse; - -import { Role } from "@prisma/client"; - -import { - addMessage, - createSession, - findSessionByCallId, - transferSession, - checkOperatorReady, - postSummary, - completeCall, -} from "./session.js"; -import { chat, summarize } from "./chat.js"; - -const app = Router(); -app.use(json()); - -app.post("/receive", async (req, res) => { - let callId = req.body.CallSid; - console.log(`CallSid: ${callId}`); - const twiml = new VoiceResponse(); - - await createSession(req.body.CallSid, req.body.From); - - const gather = twiml.gather({ - action: "/call/respond", - method: "POST", - input: "speech", - language: "en-US", - speechTimeout: "auto", - model: "experimental_conversations", - }); - - gather.say("Welcome to the suicide hotline. Tell me what's going on?."); - - console.log(twiml.toString()); - - twiml.redirect("/call/receive"); - - res.type("text/xml"); - res.send(twiml.toString()); -}); - -app.post("/respond", async (req, res) => { - let callId = req.body.CallSid; - console.log(`CallSid: ${callId}`); - const twiml = new VoiceResponse(); - - let transcription = req.body.SpeechResult; - await addMessage(callId, Role.USER, transcription); - - let operatorReady = await checkOperatorReady(callId); - - if (operatorReady) { - let session = await findSessionByCallId(callId); - let operatorPhone = session.operatorPhone; - console.log(`transferring call ${callId} to ${operatorPhone}`); - - transferSession(req.body.CallSid, operatorPhone); - twiml.say("We're connecting you to a counselor now."); - - const dial = twiml.dial({}); - dial.number(operatorPhone); - res.type("text/xml"); - res.send(twiml.toString()); - return; - } - - const gather = twiml.gather({ - action: "/call/respond", - method: "POST", - input: "speech", - language: "en-US", - speechTimeout: "auto", - model: "experimental_conversations", - }); - - let response = await chat(callId); - - gather.say(response); - await addMessage(callId, Role.BOT, response); - - twiml.redirect("/call/respond"); - - res.type("text/xml"); - res.send(twiml.toString()); -}); - -app.post("/summarize", async (req, res) => { - let sessionId = req.body.SessionId; - console.log(`summarizing ${sessionId}`); - - let summary = await summarize(sessionId); - console.log(summary); - - await postSummary(sessionId, summary); - - res.type("application/json"); - res.send(JSON.stringify({ SessionId: sessionId, summary: summary })); -}); - -app.post("/status", async (req, res) => { - let callId = req.body.CallSid; - let callStatus = req.body.CallStatus; - - if (callStatus === "completed") { - console.log(`completing call ${callId}`); - await completeCall(callId); - } -}); - -export default app;