浏览器调用 - Struts2中的Twilio.js(Java)

时间:2017-09-10 19:11:15

标签: java twilio twilio-api

Struts2和Twilio.js(api sdk)的新功能需要指导。

我已经仔细检查了我的twilio凭据,创建了访问令牌并在我的浏览器上接受了它," [设备]:流已准备好"在控制台日志上。当我尝试使用Twilio.Device.connect(〜)拨打号码时,控制台日志显示{code:31002,message:" Connection Declined",connection:a}。我不知道在日志上要查找什么。我在twilio帐户上设置了TwiML应用程序的语音URL,并使用ngrok https测试连接。

//ACTIVATE CALL START
            var connection = null;
            $("#activate-calls").click(function(){

                $("#answer").hide();
                $("#hangup").hide();
                $("#reject").hide();

                $.getJSON('activateCalls').done(

                        function (data) {
                             // log('Got a token.');
                              console.log('Token: ' + data.data.token);

                              // Setup Twilio.Device
                              Twilio.Device.setup(data.data.token, {debug: true});
                              Twilio.Device.ready(function (device) {
                                    $("#log").text("Ready");
                                });
                              Twilio.Device.error(function (error) {
                                    $("#log").text("Error: " + error.message);
                                });
                              Twilio.Device.connect(function (conn) {

                                    $("#log").text("Successfully established call");
                                    $("#call").hide();
                                    $("#reject").hide();
                                    $("#hangup").show();

                                });
                              Twilio.Device.disconnect(function (conn) {
                                    $("#log").text("Call ended. Ready for new incoming/ outgoing calls.");
                                    $("#hangup").hide();
                                    $("#call").show();
                                });


                                Twilio.Device.incoming(function(conn) {
                                    // Set the reference to the current
                                    // connection when we receive an
                                    // incoming call
                                    alert("ringring");
                                    connection = conn;
                                    $("#log").text("Incoming call from " + conn.parameters.From);
                                    $("#answer").show();
                                    $("#call").hide();
                                    $("#reject").show();

                                    // Clear the reference to the
                                    // connection when disconnected.
                                    connection.disconnect(function() {
                                        connection = null;
                                    })
                                });

                                Twilio.Device.cancel(function(conn) {
                                    $("#log").text("Ready");
                                    $("#answer").hide();
                                    $("#reject").hide();
                                    $("#call").show();
                                });
                                $("#reject").click(function() {
                                    $("#log").text("Incoming call is rejected. Ready for new incoming/ outgoing calls.");
                                    connection.reject();
                                    $("#answer").hide();
                                    $("#reject").hide();
                                    $("#call").show();


                                });

                                $("#answer").click(function() {
                                    // If there's no pending connection,
                                    // there's nothing to do.
                                    if (!connection) { return; }

                                    // Update the interface
                                    $("#answer").hide();
                                    $("#call").show();
                                    $("#log").text("Call accepted");

                                    // Accept the current connection.
                                    connection.accept();
                                });

                                $("#call").click(function() {
                                    // get the phone number to connect the call to
                                    params = {"PhoneNumber": $("#number").val()};
                                    Twilio.Device.connect(params);
                                });

                                $("#hangup").click(function() {
                                    Twilio.Device.disconnectAll();
                                    $("#log").text("Ready");
                                });





                        }



                );


            });


            //ACTIVATE CALL END

jsp / html看起来像这样..

<div align="center">
                <div class="form-inline">
                    <div class="input-group">
                        <div class="input-group-addon"><img src="https://www.twilio.com/bundles/favicons/img/Twilio_16.png" /></div>
                        <input type="text" class="form-control" id="number" name="number">
                    </div>

                    <button class="btn btn-success" id="answer">Accept</button>

                    <button class="btn btn-success" id="call">Call</button>

                    <button class="btn btn-danger" id="hangup">Hangup</button>

                    <button class="btn btn-danger" id="reject">Reject</button>


                </div>
                <br>
                <div id="log" class="alert-info" style="width: 347px; font-size: large;"></div>
            </div>

我在activeCalls上创建令牌的动作类是:

public String execute(){

    logger.debug("MCI> ActivateCallsAction.execute");

    String identity = "johnny";

    List<Scope> scopes = new ArrayList<>();
    scopes.add(new IncomingClientScope(identity));
    scopes.add(new OutgoingClientScope.Builder(APP_SID).build());


    Jwt jwt = new ClientCapability.Builder(ACCOUNT_SID, AUTH_TOKEN).scopes(scopes).build();

    String token = jwt.toJwt();
    logger.debug("token: " + token);

    HashMap<String, String> json = new HashMap<>();
    json.put("identity", identity);
    json.put("token", token);
    data = json;

    logger.debug(json.get("token") + "-" + json.get("identity"));



    return "success";
}

如果需要,我可以从控制台日志发布完整错误。 TIA!

0 个答案:

没有答案