Skip to content

Commit

Permalink
use texml translator
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Oct 14, 2024
1 parent 3c0d9ef commit 57ff640
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 352 deletions.
104 changes: 48 additions & 56 deletions express-texml-call-mask/controllers/dtmfDialController.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,69 @@
import express from "express";
import * as db from "../models/db";
import twilio from "twilio";
import Telnyx from "telnyx";
import * as db from "../models/db";
import * as texml from "../packages/texml";

const router = express.Router();
const VoiceResponse = twilio.twiml.VoiceResponse;

const hangupSentence = "Thank you for the call, hanging up";
type TelnyxTexmlTranslatorGatherCallback = {
AccountSid: string;
CallSid: string;
CallSidLegacy: string;
Digits: string;
From: string;
To: string;
};

router.route("/inbound").post(async (req, res) => {
const defaultGatherSentence =
"Hello, please enter the phone number with country code you would like to dial followed by the pound sign";
const event = req.body as Telnyx.events.CallAnsweredEvent;
console.log(event);
const response = new VoiceResponse();
const gather = response.gather({
action: "gather",
method: "POST",
numDigits: 10,
finishOnKey: "#",
});
gather.say(defaultGatherSentence);
response.say(hangupSentence);
response.hangup();
res.type("application/xml");
res.send(response.toString());
});
router
.route("/inbound")
.post<{}, unknown, Telnyx.events.CallEvent>(async (req, res) => {
const gatherSentence =
"Hello, please enter the phone number with country code you would like to dial followed by the pound sign";
const event = req.body;
console.log(event);

router.route("/gather").post(async (req, res) => {
const event = req.body as Telnyx.events.CallGatherEndedEvent;
console.log(event);
const phoneNumber = event.data?.payload?.digits;
const userRecord = db.lookupUserByPSTNPhoneNumber(event.data?.payload?.from!);
res.type("application/xml");
res.send(texml.gatherTeXML(gatherSentence, "#", 10, 15));
});

// Connect inbound caller to conf
// Create outbound dial to desired PSTN number
// when that call answers, add to conf
// save conf-id
// every {duration} play audio to conf-id
router
.route("/gather")
.post<{}, unknown, TelnyxTexmlTranslatorGatherCallback>(async (req, res) => {
const event = req.body;
console.log(event);
const phoneNumber = event.Digits;
const userRecord = db.lookupUserByPSTNPhoneNumber(event.From);

const response = new VoiceResponse();
response.dial(
{
callerId: `${userRecord?.telnyxPhoneNumber}`,
record: "record-from-answer-dual",
recordingStatusCallback: "recordStatus",
action: "dialFinished",
method: "POST",
},
`+${phoneNumber}`
);
res.type("application/xml");
res.send(response.toString());
});
// Connect inbound caller to conf
// Create outbound dial to desired PSTN number
// when that call answers, add to conf
// save conf-id
// every {duration} play audio to conf-id

router.route("/recordFinished").post(async (req, res) => {
const event = req.body as Telnyx.events.CallRecordingSavedEvent;
console.log(event);
res.type("application/xml");
res.send(texml.hangupTeXML(hangupSentence));
});
res.type("application/xml");
res.send(
texml.dialTeXML(
`${userRecord?.telnyxPhoneNumber}`,
"/dialFinished",
"POST",
"record-from-answer-dual",
"recordFinished",
`+${phoneNumber}`
)
);
});

router.route("/recordStatus").post(async (req, res) => {
const event = req.body as Telnyx.events.CallRecordingSavedEvent;
router.route("/dialFinished").post(async (req, res) => {
const event = req.body;
console.log(event);
res.sendStatus(200);
});

router.route("/dialFinished").post(async (req, res) => {
router.route("/recordFinished").post(async (req, res) => {
const event = req.body;
console.log(event);
res.sendStatus(200);
res.type("application/xml");
res.send(texml.hangupTeXML("Thank you for the call, hanging up"));
});

export default router;
Loading

0 comments on commit 57ff640

Please sign in to comment.