任务预约超时转接来电

Forwarding an incoming call after task reservation timeout

我正在使用 Twilio Flex 来支持呼叫中心。我设置了一个 TaskRouter 工作流,其中任务预留超时设置为 120 秒。在它的过滤器中,我创建了两个路由步骤。第一个在主队列中找到匹配的工作人员,超时时间为 120 秒。 120 秒后,它应该移至呼叫转移队列。在呼叫转移队列中,不存在工作人员(目标工作人员表达式:1==2)。我用“trEventListener”函数捕捉所有这些事件。将任务移入呼叫转移队列后,我调用“callForward”函数,该函数使用 twiml.dial() 将呼叫连接到外部号码。我还根据自定义原因将此任务的状态更改为“已取消”,以便我可以在 Flex Insights 中跟踪它。我正在使用 link 中的指南来形成我的逻辑:https://support.twilio.com/hc/en-us/articles/360021082934-Implementing-Voicemail-with-Twilio-Flex-TaskRouter-and-WFO.

呼叫转移工作正常,但根据 Flex 洞察,有些呼叫在 120 秒后(120 - 300 秒之间)得到处理。理想情况下,这些也应该转发。我也没有记录错误来追踪为什么只有少数几个电话会发生这种情况。

此外,在某些情况下,当我尝试将任务状态更改为以我的自定义原因取消时,它会吐出以下错误:无法取消任务,因为它不是挂起或保留的。在其他情况下,它工作正常。再次很难弄清楚为什么它有选择地工作并且其行为不一致。

这是功能代码。

trEventListener.js:

exports.handler = function(context, event, callback) {
    
    const client = context.getTwilioClient();
    let task = '';
    let workspace = '';
    console.log(`__[trEventStream]__: Event recieved of type: ${event.EventType}`);
    
    // setup an empty success response
    let response = new Twilio.Response();
    response.setStatusCode(204);
    
    // switch on the event type
    switch(event.EventType) {
        case 'task-queue.entered':
            // ignore events that are not entering the 'Call Forward' TaskQueue 
            if (event.TaskQueueName !== 'Call Forward') {
                console.log(`__[trEventStream]__: Entered ${event.TaskQueueName} queue - no forwarding required!`);
                return callback(null, response);
            }
    
            console.log(`__[trEventStream]__: entered ${event.TaskQueueName} queue - forwarding call!`);
            task = event.TaskSid;
            workspace = event.WorkspaceSid;
            const ta = JSON.parse(event.TaskAttributes);
            const callSid = ta.call_sid;
    
            let url = `https://${context.DOMAIN_NAME}/forwardCall`;
    
            // redirect call to forwardCall function
            client.calls(callSid).update({
                method: 'POST',
                url: encodeURI(url),
            }).then(() => { 
                console.log(`__[trEventStream]__: [SUCCESS] ~> Task with id ${task} forwarded to external DID`);
                // change task status to canceled so it doesn't appear in flex or show up as a pending task
                client.taskrouter.workspaces(workspace)
                 .tasks(task)
                 .update({
                    assignmentStatus: 'canceled',
                    reason: 'Call forwarded'
                  })
             .then(task => {
                    console.log(`__[trEventStream]__: [SUCCESS] ~> Task canceled`);
                    return callback(null, response); 
                 }).catch(err => { 
                    console.log(`__[trEventStream]__: [ERROR] ~> Task not marked complete: `, err);
                    // doesn't warrant reponse 500 since call still forwarded :)
                    return callback(null, response);
                 });
            }).catch(err => {
                console.log(`__[trEventStream]__: [ERROR] ~> Task failed to forward to external DID: `, err);
                response.setStatusCode(500);
                return callback(err, response);
            });
    break;
    default:
        return callback(null, response);
    }
};

callForward.js:

exports.handler = function(context, event, callback) {
    console.log(`forwarding call`);
    // set-up the variables that this Function will use to forward a phone call using TwiML
    // REQUIRED - you must set this
    let phoneNumber = event.PhoneNumber || context.NUMBER;
    // OPTIONAL
    let callerId =  event.CallerId || null;
    // OPTIONAL
    let timeout = event.Timeout || null;
    // OPTIONAL
    let allowedCallers = event.allowedCallers || [];

    let allowedThrough = true;
    if (allowedCallers.length > 0) {
      if (allowedCallers.indexOf(event.From) === -1) {
        allowedThrough = false;    
      }
    }
    
    // generate the TwiML to tell Twilio how to forward this call
    let twiml = new Twilio.twiml.VoiceResponse();

    let dialParams = {};
    if (callerId) {
      dialParams.callerId = callerId;
    }
    if (timeout) {
      dialParams.timeout = timeout;
    }
    
    if (allowedThrough) {
      twiml.dial(dialParams, phoneNumber); // making call :)
    }
    else {
      twiml.say('Sorry, you are calling from a restricted number. Good bye.');
    }
    
    // return the TwiML
    callback(null, twiml);
        
};

任何形式的帮助 and/or 指导将不胜感激。

这里是 Twilio 开发人员布道者。

当您重定向来自任务的调用时,它的任务会被取消,原因是“已重定向”,因此您不需要自己取消它。

您的代码偶尔无法更新任务,因为您的代码与任务之间的竞争条件被 Twilio 取消。