简体   繁体   English

解析Twilio statuscallback呼叫/会议事件

[英]Parsing Twilio statuscallback Call/Conference events

I'm creating a call using Twilio's Java lib like so: 我正在使用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);

I get call events as application/x-www-form-urlencoded . 我将调用事件作为application/x-www-form-urlencoded Is there a best practise way to parse the sent form data into some sort of Twilio call event object? 是否有最佳实践方法将发送的表单数据解析为某种Twilio调用事件对象?

Example of the status sent by 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

Update : Maintaining state for the events returned by statusCallbacks for example, is something you must build into your app. 更新 :例如,维护statusCallbacks返回的事件的状态是您必须在应用程序中构建的。

Check out the following example given in the docs for 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());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM