From ed2b6b00e50274c5b7d9b8319146102be5902ee8 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Sun, 5 Mar 2023 04:53:23 -0500 Subject: [PATCH] fix: forgotten import on completeCall --- src/call.js | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/call.js diff --git a/src/call.js b/src/call.js new file mode 100644 index 0000000..943b711 --- /dev/null +++ b/src/call.js @@ -0,0 +1,123 @@ +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;