@@ -3,6 +3,8 @@ import VoiceResponse from "twilio/lib/twiml/VoiceResponse";
33import { getConversationByAddressPair } from "../utils/getConversationByAddressPair.util" ;
44
55import client from '../twilioClient'
6+ import { participantsToDial } from "../utils/participantsToDial.util" ;
7+ import { listConversationParticipants } from "../utils" ;
68
79export const generateTwiml = async ( from : string , to : string ) => {
810 let response = new VoiceResponse ( ) ;
@@ -16,33 +18,18 @@ export const generateTwiml = async (from: string, to: string) => {
1618 language : process . env . CALL_ANNOUCEMENT_LANGUAGE as any ,
1719 } , process . env . OUT_OF_SESSION_MESSAGE_FOR_CALL ) ;
1820 } 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- } , [ ] )
21+ const participants = await listConversationParticipants ( conversation . conversationSid )
22+ const dialList = participantsToDial ( participants , from )
3623
3724 response . say ( {
3825 voice : process . env . CALL_ANNOUCEMENT_VOICE as any ,
3926 language : process . env . CALL_ANNOUCEMENT_LANGUAGE as any
4027 } , process . env . CONNECTING_CALL_ANNOUCEMENT ) ;
4128
42- if ( participantsToDial . length > 1 ) {
29+ if ( dialList . length > 1 ) {
4330 const conferenceName = `${ from } _at_${ Date . now ( ) } `
4431
45- const callPromises = participantsToDial . map ( pa => {
32+ const callPromises = dialList . map ( pa => {
4633 console . log ( `Dialing ${ pa . address } from ${ pa . proxyAddress } ...` ) ;
4734
4835 return client . calls . create ( {
@@ -66,7 +53,7 @@ export const generateTwiml = async (from: string, to: string) => {
6653
6754
6855 } else {
69- const callee = participantsToDial [ 0 ]
56+ const callee = dialList [ 0 ]
7057 const dial = response . dial ( {
7158 callerId : callee . proxyAddress
7259 } ) ;
0 commit comments