Reputation: 86
how to answer in Twilio Api using answer button, i m unable to pick the call on browser, i want to know how to answer or reject the call while i am connecting device on Answer button click, plz help me
my snippet is
Twilio.Device.setup('<?=$token?>');
Twilio.Device.ready(function(device) {
});
Twilio.Device.incoming(function(conn) {
$("#number").val(conn.parameters.From);
var ss= "Incomging Call:\n"+conn.parameters.From;
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
Twilio.Device.error(function(conn) {
alert("Error:\n"+data);
});
function call() {
// get the phone number or client to connect the call to
// params = {"PhoneNumber": $("#number").val()};
// Twilio.Device.connect(params);
Twilio.Device.connect();
}
function hangup() {
Twilio.Device.disconnectAll();
}
<input type="text" id="number" name="number" />
<button class="call" onclick="call();">
Answer
</button>
<button class="hangup" onclick="hangup();">
Reject
</button>
<div id="log">Loading app</div>
have any idea in twilio incoming call???
Upvotes: 2
Views: 2753
Reputation: 10366
Twilio evangelist here.
The code you've included above is automatically answering incoming calls because you are calling the accept()
function in the incoming event callback.
If you want to change your app so that calls are only answered after a button is clicked, you will first need to add a second button to the UI. In that buttons click
event you call a function that calls the accept
function.
You can continue to use the incoming callback to do something like enable your accept button only when there is an incoming call, but notice that you'll need to create a global variable that holds the incoming connection so you can access it in the accept
function.
var connection;
Twilio.Device.incoming(function(conn) {
connection = conn;
$("#number").val(conn.parameters.From);
var ss= "Incomging Call:\n"+conn.parameters.From;
$("#log").text("Incoming connection from " + conn.parameters.From);
//enable the accept button
$(".accept").prop('disabled', false);
});
function accept() {
connection.accept();
}
<button class="accept" onclick="accept();" disabled>Accept</button>
Of course you can get a lot more complex in the incoming function than whats shown above. Check out this blog post for a more complex example that shows how to use the jQuery dialog widget to show a screen pop. Hope that helps.
Upvotes: 5