Twilio将业务代表连接到队列中的呼叫

时间:2018-07-27 20:27:46

标签: javascript c# twilio

我有Twilio的试用版(仅允许使用1个电话号码)。

运行我的应用程序时,我会通过Twilio进行身份验证。

然后,使用我的个人电话,在我的Twilio号码上拨打电话,将其自动添加到我的“支持”队列中。效果很好,可以听到音乐。

现在,在我的应用中,我想连接到队列中的呼叫。我在队列列表框上方创建了一个“连接到呼叫者”按钮。单击后,它将执行以下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));
        });
}

这将在我的站点上调用一个终结点,位于此处:

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
        );
    }
}

我认为拥有重定向URL会告诉Twilio击中该端点,并传递客户端“我”的名称。

这是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);
    }
}

但是,当所有这些执行完后,它永远不会到达将来自Twilio的呼叫连接到我的/ call / index端点(随后再也不会到达javascript中的Twilio.Device.incoming函数。

我已经能够成功拨打和接听电话。现在我只想取消队列中的通话...

感谢您的帮助!

编辑:

好的,我记得我在上面的过程中忘记做的一件事……实际上是对Twilio进行了API调用,以告知它连接到成员。

因此,我已经为此配置了服务,所以这是更新的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
        );
    }

但是,我不知道将返回的MemberResource放在响应中的位置。另外,我不确定为服务进行的MemberResource.UpdateAsync调用的Uri传递什么,在这里:

    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"
        );
    }

1 个答案:

答案 0 :(得分:1)

因此,经过更多研究后,我现在开始进行这项工作。由于我已经开始了赏金计划,因此我将提供对我有用的答案...

我的javascript“将代理连接到呼叫”按钮不应一直在呼叫我自己的端点进行连接。更改为:

Twilio.Device.connect({ To: 'Support', From: 'Agent' });

然后,通过我的“呼叫”控制器将呼叫路由回去,在该控制器中我必须添加以下逻辑:

else if (to == "Support" && from == "Agent")
                {
                    response.Say("Connecting you to the first caller");
                    var queueDial = new Dial().Queue("Support");
                    response.Append(queueDial);
                }

那立即将我连接到了支持队列中的个人手机。

顺便说一句,为了让按钮调用客户端支持,我必须将“ Twiml App”名称保存到支持中。

相关问题