1+ import client from '../twilioClient'
12import VoiceResponse from "twilio/lib/twiml/VoiceResponse" ;
23
3- import { getConversationByAddressPair } from "../utils/getConversationByAddressPair.util" ;
4-
5- import client from '../twilioClient'
4+ import {
5+ getConversationByAddressPair ,
6+ participantsToDial ,
7+ listConversationParticipants ,
8+ generateConferenceName
9+ } from "../utils/" ;
610
711export const generateTwiml = async ( from : string , to : string ) => {
812 let response = new VoiceResponse ( ) ;
@@ -16,33 +20,18 @@ export const generateTwiml = async (from: string, to: string) => {
1620 language : process . env . CALL_ANNOUCEMENT_LANGUAGE as any ,
1721 } , process . env . OUT_OF_SESSION_MESSAGE_FOR_CALL ) ;
1822 } 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- } , [ ] )
23+ const participants = await listConversationParticipants ( conversation . conversationSid )
24+ const dialList = participantsToDial ( participants , from )
3625
3726 response . say ( {
3827 voice : process . env . CALL_ANNOUCEMENT_VOICE as any ,
3928 language : process . env . CALL_ANNOUCEMENT_LANGUAGE as any
4029 } , process . env . CONNECTING_CALL_ANNOUCEMENT ) ;
4130
42- if ( participantsToDial . length > 1 ) {
43- const conferenceName = ` ${ from } _at_ ${ Date . now ( ) } `
31+ if ( dialList . length > 1 ) {
32+ const conferenceName = generateConferenceName ( from )
4433
45- const callPromises = participantsToDial . map ( pa => {
34+ const callPromises = dialList . map ( pa => {
4635 console . log ( `Dialing ${ pa . address } from ${ pa . proxyAddress } ...` ) ;
4736
4837 return client . calls . create ( {
@@ -52,28 +41,20 @@ export const generateTwiml = async (from: string, to: string) => {
5241 } ) ;
5342 } ) ;
5443
55- try {
56- await Promise . all ( callPromises )
57- } catch ( err ) {
58- console . log ( err )
59- throw new Error ( err )
60- }
44+ await Promise . all ( callPromises )
6145
6246 const dial = response . dial ( ) ;
6347 dial . conference ( {
6448 endConferenceOnExit : true
6549 } , conferenceName ) ;
66-
67-
6850 } else {
69- const callee = participantsToDial [ 0 ]
51+ const callee = dialList [ 0 ]
7052 const dial = response . dial ( {
7153 callerId : callee . proxyAddress
7254 } ) ;
7355
7456 dial . number ( callee . address ) ;
7557 }
76-
7758 }
7859
7960 return response ;
0 commit comments