首页 > 解决方案 > 如何在 twilio 函数调用节点库中获取可用的工作人员数量?

问题描述

我希望调用一个 twilio 函数并获得可用工人的数量。

我觉得这可能与 TaskQueues 和可用的 Matching Workers 有关?

我想出了以下内容。但是,用户在与任务交互时仍被列为可用,这意味着这不一定有效。

exports.handler = function (context, event, callback) {
const client = require('twilio')(context.ACCOUNT_SID, context.AUTH_TOKEN);
client.taskrouter
.workspaces('eee')
.workers.list()
.then(workers => {
    data = {
        availWorkersCount: Object.keys(workers.filter(x=> x.available === true && x.attributes.includes("sales"))).length
    };

    const response = new Twilio.Response();
    response.appendHeader('Access-Control-Allow-Origin', '*');
    response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST GET');
    response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
    response.appendHeader('Content-Type', 'application/json');
    response.setBody(data);

    callback(null, response);
});

};

标签: twiliotwilio-functions

解决方案


我知道这有点晚了,但这是我在 twilio 函数中用于执行此操作的代码块。我们从工作室流程中调用该函数并使用结果来决定我是将呼叫路由到语音邮件还是播放一些 IVR 选项来选择他们应该放入哪个队列。要使用它,您可以传递一个可选技能到进一步过滤代理。我们抓住所有可用的工人,然后过滤到只有具有所需技能的工人。

然后,您可以使用它并检查是否有任何可用的代理也有分配给他们的任务,然后将它们从计数中删除。

const fetch = require("node-fetch");

exports.handler = function(context, event, callback) {
    let response = new Twilio.Response();

    // Set the status code to 200 OK
    response.setStatusCode(200);

    // Set the Content-Type Header
    response.appendHeader('Access-Control-Allow-Origin', '*');
    response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
    response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
    response.appendHeader('Content-Type', 'application/json');

    let body = {
        TotalAvailable: 0
    };
    
    let client = context.getTwilioClient();
    client.taskrouter.workspaces(context.WorkspaceSid)
        .workers
        .list({
            available: 'true',
            limit: 50
        })
        .then((workers) => {
            let agents = [];
            let i = 0;
            if(workers){
                for(i = 0; i < workers.length; i++){
                    let worker = workers[i];
                    let item = {};
                    let attributes = JSON.parse(worker.attributes);

                    if(attributes && attributes.routing && attributes.routing.skills && attributes.routing.skills.length > 0){
                        item.skills = attributes.routing.skills;
                        item.nid = attributes.nid;
                        item.first_name = attributes.first_name;
                        item.last_name = attributes.last_name;
                        
                        if(event.skill){
                            if(item.skills.includes(event.skill)){

                                // TODO: filter here 

                                agents.push(item);
                            }    
                        }else{
                            agents.push(item);
                        }
                    }
                }
            }
            body.TotalAvailable = agents.length;
            body.Agents = agents;

            response.setBody(body);
            callback(null, response);
        })
        .catch((ex) => {
            body.error = true;
            body.message = ex;
            response.setBody(body);
            callback(null, response);
        });
};

推荐阅读