-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c0d9ef
commit 57ff640
Showing
4 changed files
with
87 additions
and
352 deletions.
There are no files selected for viewing
104 changes: 48 additions & 56 deletions
104
express-texml-call-mask/controllers/dtmfDialController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.