Skip to content

Commit f542618

Browse files
committed
Extract twiml logic from inboundCall controller to service
1 parent 9c1e59a commit f542618

File tree

3 files changed

+78
-87
lines changed

3 files changed

+78
-87
lines changed

src/controllers/inboundCall.ts

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,14 @@
11
import type { Request, Response } from "express";
2-
import VoiceResponse from "twilio/lib/twiml/VoiceResponse";
3-
4-
import { getConversationByAddressPair } from "../utils/getConversationByAddressPair.util";
5-
6-
import client from '../twilioClient'
7-
8-
const generateTwiml = async (from: string, to: string) => {
9-
let response = new VoiceResponse();
10-
11-
const conversation = await getConversationByAddressPair(from, to)
12-
13-
if (!conversation) {
14-
console.log(`No active session (conversation) for ${from}.`)
15-
response.say({
16-
voice: process.env.CALL_ANNOUCEMENT_VOICE as any,
17-
language: process.env.CALL_ANNOUCEMENT_LANGUAGE as any,
18-
}, process.env.OUT_OF_SESSION_MESSAGE_FOR_CALL);
19-
} else {
20-
const participants = await client.conversations
21-
.conversations(conversation.conversationSid)
22-
.participants
23-
.list()
24-
25-
const participantsToDial = participants.reduce((result, p) => {
26-
if (p.messagingBinding.type === "sms" && p.messagingBinding.address != from) {
27-
console.log(`Adding ${p.messagingBinding.address} to list of numbers to dial.\n`)
28-
29-
result.push({
30-
address: p.messagingBinding.address,
31-
proxyAddress: p.messagingBinding.proxy_address
32-
})
33-
}
34-
35-
return result;
36-
}, [])
37-
38-
response.say({
39-
voice: process.env.CALL_ANNOUCEMENT_VOICE as any,
40-
language: process.env.CALL_ANNOUCEMENT_LANGUAGE as any
41-
}, process.env.CONNECTING_CALL_ANNOUCEMENT);
42-
43-
if (participantsToDial.length > 1) {
44-
const conferenceName = `${from}_at_${Date.now()}`
45-
46-
const callPromises = participantsToDial.map(pa => {
47-
console.log(`Dialing ${pa.address} from ${pa.proxyAddress}...`);
48-
49-
return client.calls.create({
50-
url: `https://${process.env.DOMAIN}/join-conference?conferenceName=${encodeURIComponent(conferenceName)}`,
51-
to: pa.address,
52-
from: pa.proxyAddress
53-
});
54-
});
55-
56-
await Promise.all(callPromises)
57-
58-
const dial = response.dial();
59-
dial.conference({
60-
endConferenceOnExit: true
61-
}, conferenceName);
62-
63-
64-
} else {
65-
const callee = participantsToDial[0]
66-
const dial = response.dial({
67-
callerId: callee.proxyAddress
68-
});
69-
70-
dial.number(callee.address);
71-
}
72-
73-
}
74-
75-
return response;
76-
77-
}
2+
import { generateTwiml } from "../services/inboundCall.service";
783

794
export const post = async (
805
req: Request,
816
res: Response
827
) => {
83-
const { body } = req.body;
84-
858
const from = req.body.From
86-
const called = req.body.Called
87-
console.log(req.body.From)
88-
console.log(req.body.Called)
9+
const to = req.body.Called
8910

90-
const twiml = await generateTwiml(from, called);
11+
const twiml = await generateTwiml(from, to);
9112

9213
res.set('Content-Type', 'text/xml')
9314
res.send(twiml.toString())
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import VoiceResponse from "twilio/lib/twiml/VoiceResponse";
2+
3+
import { getConversationByAddressPair } from "../utils/getConversationByAddressPair.util";
4+
5+
import client from '../twilioClient'
6+
7+
export const generateTwiml = async (from: string, to: string) => {
8+
let response = new VoiceResponse();
9+
10+
const conversation = await getConversationByAddressPair(from, to)
11+
12+
if (!conversation) {
13+
console.log(`No active session (conversation) for ${from}.`)
14+
response.say({
15+
voice: process.env.CALL_ANNOUCEMENT_VOICE as any,
16+
language: process.env.CALL_ANNOUCEMENT_LANGUAGE as any,
17+
}, process.env.OUT_OF_SESSION_MESSAGE_FOR_CALL);
18+
} else {
19+
const participants = await client.conversations
20+
.conversations(conversation.conversationSid)
21+
.participants
22+
.list()
23+
24+
const participantsToDial = participants.reduce((result, p) => {
25+
if (p.messagingBinding.type === "sms" && p.messagingBinding.address != from) {
26+
console.log(`Adding ${p.messagingBinding.address} to list of numbers to dial.\n`)
27+
28+
result.push({
29+
address: p.messagingBinding.address,
30+
proxyAddress: p.messagingBinding.proxy_address
31+
})
32+
}
33+
34+
return result;
35+
}, [])
36+
37+
response.say({
38+
voice: process.env.CALL_ANNOUCEMENT_VOICE as any,
39+
language: process.env.CALL_ANNOUCEMENT_LANGUAGE as any
40+
}, process.env.CONNECTING_CALL_ANNOUCEMENT);
41+
42+
if (participantsToDial.length > 1) {
43+
const conferenceName = `${from}_at_${Date.now()}`
44+
45+
const callPromises = participantsToDial.map(pa => {
46+
console.log(`Dialing ${pa.address} from ${pa.proxyAddress}...`);
47+
48+
return client.calls.create({
49+
url: `https://${process.env.DOMAIN}/join-conference?conferenceName=${encodeURIComponent(conferenceName)}`,
50+
to: pa.address,
51+
from: pa.proxyAddress
52+
});
53+
});
54+
55+
await Promise.all(callPromises)
56+
57+
const dial = response.dial();
58+
dial.conference({
59+
endConferenceOnExit: true
60+
}, conferenceName);
61+
62+
63+
} else {
64+
const callee = participantsToDial[0]
65+
const dial = response.dial({
66+
callerId: callee.proxyAddress
67+
});
68+
69+
dial.number(callee.address);
70+
}
71+
72+
}
73+
74+
return response;
75+
}

src/services/participant.service.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)