javascript - 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 到达该端点,并传入客户端“我”的名称。
这是呼叫控制器:
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);
}
}
但是,当所有这些都被执行时,它永远不会到达 /call/index 端点,该端点会将来自 Twilio 的调用连接到我(并且随后永远不会到达 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"
);
}
解决方案
所以,经过更多的研究,我现在有了这个工作。由于我已经开始赏金,我将更新对我有用的答案......
我的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 应用程序”名称保存到支持。