1

I am trying to implement 'hold call' functionality into my system. I have looked at this question and some twilio docs, and recommended way to do this is to dequeue the call, which will automatically play the hold music, and then retrieve from queue when hold is complete(un-hold).

This works perfectly fine, when I have an incoming call from a mobile, to my twilio client, and I can dequeue the call, and all works fine.

However when I do it with the outgoing call from the twilio client to the mobile, if I update the call to issue dequeue instruction, I get to hear the hold music on the client side itself, and the phone gets disconnected. I suppose this is because I have set the client leg of the call on hold. So the question is how do I get the call sid for the mobile leg of the call.

I have tried querying CallResource by ParentCallId, but that does not return anything in the case of outgoing calls. Any ideas?

Call is initiated on the client with:

var params = {
        To: num
    };

    console.log('Calling ' + num + '...');
    Twilio.Device.connect(params);

The connect API callback uses a simple Dial verb.

Client code for saving callid on connect:

Twilio.Device.connect(function (conn) {
                if (conn._direction === 'OUTGOING') {
                    $scope.outgoing_call_sid = conn.parameters.CallSid;
                    $scope.number = conn.message.To;
                } else {
                    $scope.incoming_call_sid = conn.parameters.CallSid;
                    $scope.number = conn.parameters.From;
                }

                $scope.message = 'In call with ' + $scope.number;
                $scope.status = 'InCall';
            });

Client code on hold button click:

$scope.hold = function () {
    $scope.status = 'Hold';
    $scope.message = 'Putting on hold...';
    if ($scope.outgoing_call_sid) {
        return $http.get(serviceBase + 'api/twilio/hold?callid=' + $scope.outgoing_call_sid);
    }
};

Server side Hold API call:

public IHttpActionResult Hold(string callid) { /
    //callid = GetLegCallId(callid); //Try to replace with child call 
    CallResource.Update(new UpdateCallOptions(callid) { Url = ConfigurationManager.AppSettings["ngrokUrl"] + "/api/twilio/enqueue", Method = HttpMethod.Get }); 
    return Ok(); 
}

Code for getting any child calls:

 public string GetLegCallId(string callId)
{
    var calls = CallResource.Read(new ReadCallOptions() { ParentCallSid = callId });
    if (calls.GetEnumerator().Current != null)
        return calls.GetEnumerator().Current.Sid;
}
Shailesh
  • 71
  • 6
  • When you say you tried to query the call resource by parentCallId, what code did you use there? Can you share that with us? – philnash Oct 16 '17 at 11:31
  • added code to original post. I am using C# .net, but think should read ok, even if you are used to PHP/node/etc. – Shailesh Oct 16 '17 at 11:56
  • Hmm, that looks fine. When do you run that code though? Is it right at the start of the call? – philnash Oct 16 '17 at 12:00
  • have added more code, to give it some context – Shailesh Oct 16 '17 at 12:10
  • Thanks for all the extra code. Can I confirm what the actual issue is? When you make the call to the API searching for calls with the `ParentCallSid` from your outgoing call, you get an empty result back? – philnash Oct 16 '17 at 12:41
  • Correct, the enumerator is empty – Shailesh Oct 16 '17 at 12:45
  • Do you mind emailing me at philnash@twilio.com with your account SID and an example call SID from a call that is affected by this issue? I'd like to look into it a bit deeper. – philnash Oct 16 '17 at 12:46
  • Have mailed you. – Shailesh Oct 16 '17 at 13:03

1 Answers1

1

My bad. Twilio wasnt the issue. Issue was with usage of calls.GetEnumerator().Current != null.

Should do MoveNext on the Enumerator, before Current will have a value. Resolved by doing that. Stupid:(

Shailesh
  • 71
  • 6