2017-01-09 116 views
1

我創建使用Twilio公司的Java lib中,像這樣的電話:解析Twilio statuscallback電話/會議活動

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 

回答

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()); 
    } 
} 
+0

謝謝,但我相信這只是打電話(v5)的棄用方式。這正是我上面所做的,也沒有說明如何處理回來的回調事件。希望API攔截statuscallback事件,並保持內部模型的更新。不要以爲是這樣? –