|
1 | 1 | 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 (to: string, from: 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.messagingBinding.proxy_address |
68 | | - }); |
69 | | - |
70 | | - dial.number(callee.messagingBinding.address); |
71 | | - } |
72 | | - |
73 | | - } |
74 | | - |
75 | | - return response; |
76 | | - |
77 | | -} |
| 2 | +import { generateTwiml } from "../services/inboundCall.service"; |
78 | 3 |
|
79 | 4 | export const post = async ( |
80 | 5 | req: Request, |
81 | 6 | res: Response |
82 | 7 | ) => { |
83 | | - const { body } = req.body; |
84 | | - const { to, from } = body; |
| 8 | + const from = req.body.From |
| 9 | + const to = req.body.Called |
85 | 10 |
|
86 | | - const twiml = await generateTwiml(to, from); |
| 11 | + const twiml = await generateTwiml(from, to); |
87 | 12 |
|
88 | 13 | res.set('Content-Type', 'text/xml') |
89 | 14 | res.send(twiml.toString()) |
|
0 commit comments