Search code examples
javatwilioconference

Can we call a number in middle of a conference call in Twilio?


I am new in this field. coming to question. I want to call a number during the conference call and add that participant to the current conference. I have tried a sample code for the conference given here in Java. Is there any way to gather the input then call the number and add the participant to the same conference.

here is what I tried. I have created a conference and which will return the following response

<Response>
<Dial hangupOnStar="true">
<Conference startConferenceOnEnter="true" endConferenceOnExit="true">My Conference</Conference>
</Dial>
<Gather timeout="10" action="/twilio-tut/add/participant?confName=My%20Conference" finishOnKey="#">
<Say>Please enter the number you want to connect followed by hash key.</Say></Gather>
</Response>

Now One of the participants in conference say A press * and dialed a number of another person to whom he wants to add to the conference.

Now on action of Gather verb, I am dialing a number the code as shown below

Number number = 
                    new Number.Builder(some_valid_phone_number)
                    .statusCallback("https://xxxxxxx.ngrok.io/twilio-tut/to/conference")
                    .statusCallbackMethod(Method.POST)
                    .statusCallbackEvents(Arrays.asList(Event.ANSWERED))
                    .build();

            Dial dial = new Dial.Builder()
                    .number(number)
                    .conference(new Conference.Builder(conferenceName).build())
                    .build();

            twiml = new VoiceResponse.Builder().dial(dial)
                    .build();

On statusCallback, I am updating the call to redirect to conference for both caller and callee where caller is the one who left the conference by pressing * i.e. A and callee is some_valid_phone_number. Code is as shown below

Call callee = Call.updater(callSid)
                    .setUrl("https://xxxxx.ngrok.io/twilio-tut/voice").setMethod(HttpMethod.POST).update();
            Call caller = Call.updater(parentCallSid)
                    .setUrl("https://xxxxx.ngrok.io/twilio-tut/voice").setMethod(HttpMethod.POST).update();

above code transfer the callee and disconnect the caller with Exception

com.twilio.exception.ApiException: Call is not in-progress. Cannot redirect.

What I want to do is A call some other number and finally they will connect to same conference. And A should be capable of calling other numbers and add them in same conference. I am using mobile phone to connect the numbers.

Thanks in advance.


Solution

  • Twilio developer evangelist here.

    The issue you have is that you are trying to perform two dials within the TwiML in the response to the <Gather>. Rather than making a <Dial> with a <Number> to the number you dial with the <Gather>, you should create that call using the REST API and use the TwiML to direct the person on the phone back into the original conference.

    To put that into clear steps, it should be something like this:

    1. User calls Twilio number
    2. TwiML responds, adds user to conference with hangUpOnStar
    3. User presses star and Gather asks for a number to dial
    4. In the response to the number from Gather, create call using REST API and direct that call to the original inbound URL ("/conference")
    5. In the response to the Gather action, return TwiML to return original caller to conference (with redirect to original inbound URL)

    I'm not a Java developer, so this might be wrong, but you want something that looks a bit like this:

    @WebServlet("/dial/participant")
    public class AddParticipantToConference extends HttpServlet {
    
        public static final String MODERATOR = System.getenv("MY_PHONE_NUMBER");
        public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
        public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
    
        @Override
        protected void doPost(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
                throws IOException {    
            String selectedOption = servletRequest.getParameter("Digits");
    
            VoiceResponse twiml;
    
            if(selectedOption != null){
                Call call = Call.creator(new PhoneNumber("+" + selectedOption), new PhoneNumber(MODERATOR),
            new URI("https://example.com/conference")).create();
            }
    
            twiml = new VoiceResponse.Builder().redirect("/conference").build();
    
            servletResponse.setContentType("text/xml");
    
            try {
                servletResponse.getWriter().print(twiml.toXml());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    You don't need the statusCallback for this now.

    Let me know if that helps at all