解析Twilio statuscallback呼叫/会议事件

时间:2017-01-09 16:18:52

标签: rest api twilio twilio-api

我正在使用Twilio的Java lib创建一个调用,如下所示:

 PhoneNumber phoneNumberTo = new PhoneNumber(to);
 PhoneNumber phoneNumberFrom = new PhoneNumber(from);
 com.twilio.rest.api.v2010.account.Call call = new CallCreator(phoneNumberTo, phoneNumberFrom, URI.create(twimlUrl))
     .setStatusCallback(URI.create(twStatusCallbackUrl))
     .setStatusCallbackEvent(Arrays.asList(
         com.twilio.rest.api.v2010.account.Call.Event.ANSWERED.toString(),
         com.twilio.rest.api.v2010.account.Call.Event.COMPLETED.toString(),
         com.twilio.rest.api.v2010.account.Call.Event.INITIATED.toString(),
         com.twilio.rest.api.v2010.account.Call.Event.RINGING.toString()))
     .create(this.client);

我将呼叫事件设为application/x-www-form-urlencoded。是否有最佳实践方法将发送的表单数据解析为某种Twilio调用事件对象?

Twilio发送的状态示例:

START TIME        =09-Jan-2017 16:00:16
    requestURI=/api/calls/12345/
      authType=null
characterEncoding=UTF-8
 contentLength=591
   contentType=application/x-www-form-urlencoded; charset=utf-8
   contextPath=
        header=accept=*/*
        header=cache-control=max-age=259200
        header=content-length=591
        header=content-type=application/x-www-form-urlencoded; charset=utf-8
        header=host=api.example.com
        header=x-twilio-signature=BLAH/BLAH
        header=user-agent=TwilioProxy/1.1
        header=connection=close
        locale=en_US
        method=POST
     parameter=Called=+44123456789
     parameter=ToState=London
     parameter=CallerCountry=GB
     parameter=Direction=outbound-api
     parameter=Timestamp=Mon, 09 Jan 2017 16:00:16 +0000
     parameter=CallbackSource=call-progress-events
     parameter=CallerState=London
     parameter=ToZip=
     parameter=SequenceNumber=3
     parameter=CallSid=BLAHBLAH
     parameter=To=+44123456789
     parameter=CallerZip=
     parameter=ToCountry=GB
     parameter=ApiVersion=2010-04-01
     parameter=CalledZip=
     parameter=CalledCity=
     parameter=CallStatus=completed
     parameter=Duration=1
     parameter=From=+44123456789
     parameter=CallDuration=5
     parameter=AccountSid=BLAHBLAH
     parameter=CalledCountry=GB
     parameter=CallerCity=
     parameter=Caller=+44123456789
     parameter=FromCountry=GB
     parameter=ToCity=
     parameter=FromCity=
     parameter=CalledState=London
     parameter=FromZip=
     parameter=FromState=London
      pathInfo=null
      protocol=HTTP/1.1
   queryString=null
    remoteAddr=X.X.X.X
    remoteHost=X.X.X.X
    remoteUser=null
requestedSessionId=null
        scheme=http
    serverName=api.example.com
    serverPort=8082
   servletPath=/api/calls/12345/
      isSecure=false

1 个答案:

答案 0 :(得分:0)

更新:例如,维护statusCallbacks返回的事件的状态是您必须在应用中构建的。

查看making a call and monitoring progress events文档中给出的以下示例。

// Install the Java helper library from twilio.com/docs/java/install
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.CallFactory;
import com.twilio.sdk.resource.instance.Call;
import com.twilio.sdk.resource.list.CallList;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class Example {

  // Find your Account Sid and Token at twilio.com/user/account
  public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String AUTH_TOKEN = "your_auth_token";

  public static void main(String[] args) throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

    // Build a filter for the CallList
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("Url", "http://demo.twilio.com/docs/voice.xml"));
    params.add(new BasicNameValuePair("To", "+14155551212"));
    params.add(new BasicNameValuePair("From", "+18668675309"));
    params.add(new BasicNameValuePair("Method", "GET"));
    params.add(new BasicNameValuePair("StatusCallback", "https://www.myapp.com/events"));
    params.add(new BasicNameValuePair("StatusCallbackMethod", "POST"));
    params.add(new BasicNameValuePair("StatusCallbackEvent", "initiated"));
    params.add(new BasicNameValuePair("StatusCallbackEvent", "ringing"));
    params.add(new BasicNameValuePair("StatusCallbackEvent", "answered"));
    params.add(new BasicNameValuePair("StatusCallbackEvent", "completed"));


    CallFactory callFactory = client.getAccount().getCallFactory();
    Call call = callFactory.create(params);
    System.out.println(call.getSid());
  }
}