From 73688270cd3c3a08cd6511f69262ca7b5bab5854 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Sat, 4 Mar 2023 17:23:33 -0500 Subject: [PATCH] feat: added gathering to twilio calling api --- call.js | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/call.js b/call.js index 074fa48..124e69a 100644 --- a/call.js +++ b/call.js @@ -9,18 +9,54 @@ const authToken = process.env.TWILIO_AUTH_TOKEN; const phone_number = process.env.TWILIO_PHONE_NUMBER; const client = new twilio.Twilio(accountSid, authToken); +const VoiceResponse = twilio.twiml.VoiceResponse; + const app = express(); +app.use(express.urlencoded({ extended: false })); app.post("/voice", (request, response) => { - console.log("got call!"); - const voiceResponse = new twilio.twiml.VoiceResponse(); - voiceResponse.say({ voice: "alice" }, "hello world!"); + const city = request.body.FromCity; + console.log(`They're from ${city}!`); + + // Use the Twilio Node.js SDK to build an XML response + const twiml = new VoiceResponse(); + twiml.say({ voice: "alice" }, `Never gonna give you up ${city}.`); + twiml.play({}, "https://demo.twilio.com/docs/classic.mp3"); // Render the response as XML in reply to the webhook request response.type("text/xml"); response.send(twiml.toString()); }); +app.post("/call", (req, res) => { + // Use the Twilio Node.js SDK to build an XML response + const twiml = new VoiceResponse(); + + // Use to record and transcribe the caller's message + const gather = twiml.gather({ + action: "https://eowj0ew8hq7190e.m.pipedream.net/respond", + method: "POST", + input: "speech", + language: "en-US", + speechTimeout: "auto", + }); + + gather.say("Tell us what makes you sad."); + + twiml.redirect("/call"); + + res.type("text/xml"); + res.send(twiml.toString()); +}); + +app.post("/respond", (req, res) => { + let transcription = req.body.TranscriptionText; + console.log(transcription); + + const twiml = new VoiceResponse(); + twiml.say(`You said, {$transcription}`); +}); + // Create an HTTP server and listen for requests on port 3000 app.listen(3000, () => { console.log(