Wednesday, 1 August 2018

Twilio connecting an agent to a call in a queue

I have a trial version of Twilio (only 1 phone number allowed).

When running my app, I get authenticated with Twilio.

Then, using my personal phone, I call my Twilio number where I have it automatically get added to my "Support" queue. Works great, can hear the hold music.

Now, In my app, I want to connect to the call in the queue. I created a button above my queue listbox that says "Connect To Caller". When clicked, it executes this javascript:

document.getElementById('connect-to-queue-caller').onclick = function () {
    $.getJSON('/queue/connectagenttocall')
        .done(function (response) {
            log('Successfully connected to the first caller in the queue');
            log(response);
            log(JSON.stringify(response));
        })
        .fail(function (error) {
            log('Failed to connect to the first caller in the queue.');
            log(JSON.stringify(error));
        });
}

That calls an endpoint on my site, which is here:

public class QueueController : Controller
{
    public ActionResult ConnectAgentToCall()
    {
        var dial = new Dial();
        dial.Queue("Support");

        var response = new VoiceResponse();
        response.Say("You are being connected to the next caller in line.");
        response.Append(dial);

        var redirect = new Redirect(new Uri("http://localhost:54032/call?to=me"));
        response.Append(redirect);

        return Json(
            response,
            JsonRequestBehavior.AllowGet
        );
    }
}

I thought that having the redirect url would tell Twilio to hit that endpoint, passing in the name of the client "me".

Here's that CallController:

public class CallController : Controller
{
    [HttpPost]
    public ActionResult Index(string to, string from)
    {
        var callerId = from;
        var response = new VoiceResponse();

        if (!string.IsNullOrEmpty(to))
        {
            var dial = new Dial(callerId: callerId);

            // wrap the phone number or client name in the appropriate TwiML verb
            // by checking if the number given has only digits and format symbols
            if (to.Contains("5551231234"))
            {
                //dial.Client("me");
                response.Say("You are being transferred to the support queue");
                response.Enqueue("Support");
            }
            else if (Regex.IsMatch(to, "^[\\d\\+\\-\\(\\) ]+$"))
            {
                dial.Number(to);
            }
            else
            {
                dial.Client(to);
            }

            response.Append(dial);
        }
        else
        {
            response.Say("Thanks for your call.");
        }

        return new TwiMLResult(response);
    }
}

But, when all of this is executed, it never hits the /call/index endpoint that would connect the call from Twilio, to me (and subsequently never hits the Twilio.Device.incoming function in the javascript.

I've been able to successfully do outgoing calls, as well as incoming. Now I just want to pull calls off my queue...

Any help is appreciated!

EDIT:

Ok, one thing I remembered that I forgot to do in the process above...actually make an API call to Twilio to tell it to connect to the member.

So, I already had the service on my side configured for this, so here's the updated Controller action:

    [HttpGet]
    [Route("/queue/{queueSid}/dequeue/front")]
    public async Task<ActionResult> ConnectAgentToCall(string queueSid)
    {
        var frontMember = await _twilioQueueService.DequeueFirstMemberAsync(queueSid, "http://localhost:54032" + dequeueResponseXml);

        var dial = new Dial();
        dial.Queue("Support");

        var response = new VoiceResponse();
        response.Say("You are being connected to the next caller in line.");
        response.Append(dial);

        var redirect = new Redirect(new Uri("http://localhost:54032/call?to=me"));
        response.Append(redirect);

        return Json(
            response,
            JsonRequestBehavior.AllowGet
        );
    }

But, I don't know where to put this MemberResource that is returned, in the response. Also, I'm not sure what to pass for the Uri for the MemberResource.UpdateAsync call that the service makes, which is here:

    public async Task<MemberResource> DequeueFirstMemberAsync(string queueSid, string url)
    {
        return await MemberResource.UpdateAsync(
            url: new Uri(url),
            method: Twilio.Http.HttpMethod.Post,
            pathQueueSid: queueSid,
            pathCallSid: "Front"
        );
    }



from Twilio connecting an agent to a call in a queue

No comments:

Post a Comment