0
votes

In the next sample code, Twilio puts in conversation OPERATOR_PHONE_NUMBER to CLIENT_PHONE_NUMBER, and records the call.

But I don't know what should be the code to control some things, one or both of the phones...:

  1. does not exist.

  2. exists but communicates.

  3. exists, does not communicate but does not pick up.

  4. exists, does not communicate, picks up and the conversation takes place.

    import com.twilio.Twilio;
    import com.twilio.rest.api.v2010.account.Call;
    import com.twilio.rest.api.v2010.account.CallCreator;
    import com.twilio.type.PhoneNumber;
    import com.twilio.type.Twiml;
    
    public class SimpleCallWithRecording2 {
    private static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    private static final String AUTH_TOKEN  = "9ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
    
    private static final String ASSIGNED_PHONE_NUMBER = "+15999999999999";  
    
    //Must be verified numbers in trial account
    private static final String OPERATOR_PHONE_NUMBER = "+34888888888";
    private static final String CLIENT_PHONE_NUMBER   = "+34777777777";
    
    
    public static void main(String[] args) throws Exception {   
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
    
        PhoneNumber to = new PhoneNumber(OPERATOR_PHONE_NUMBER);
    
        PhoneNumber from = new PhoneNumber(ASSIGNED_PHONE_NUMBER);
    
        Twiml twiml = new Twiml(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>                    " +  
            "<Response>                                                    " +
            "   <Say voice=\"woman\">This is said by a robotic woman</Say> " +          
            "   <Dial>                                                     " +
            "      <Number> " + CLIENT_PHONE_NUMBER + "</Number>           " +
            "   </Dial>                                                    " +
            "</Response>                                                   " );
    
        CallCreator callCreator = Call.creator(to, from, twiml);
            callCreator.setRecord(true);
    
        Call call = callCreator.create();
        System.out.println(call);
    }
    

    }

In the doc I see something could be done with callCreator.setStatusCallback(URI.create("https://www.myapp.com/events")), and some clasification of events: "initiated", "ringing", "answered", "completed". BUT I havent find the code "on the other side", I mean in https://www.myapp.com/events extreme ¿?

1

1 Answers

1
votes

You need to setup a that rest endpoint and set it as status callback url. The url will recieve events from twilio.

Refer this

edit: if you also need events from the nested verb, define attribute 'action' to it.

ie

 <Dial action="//callbackURL">
 <Number>   CLIENT_PHONE_NUMBER  </Number> 
 </Dial>

that way you'll know your cases 1 ,2 ,3 ,4.

and to "control" the call, you just respond with the desired TwiML to the callback request. Hope this clarifies.

edit2: You need to do something like:

//handles callback url
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
{  //...
   TwiMLResponse twiml = new TwiMLResponse();
   String callSid = request.getParameter("CallSid");
   //handle call specific data
   switch(request.getParameter("CallStatus")){
      case "no-answer": //construct twiML
      case "ringing" ://...
    }
    //...
   response.setContentType("application/xml");
   response.getWriter().print(twiml.toXML());
}

See: IVR - Example