twilio - 如何在 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);
});
};
解决方案
我知道这有点晚了,但这是我在 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);
});
};
推荐阅读
- android - 如何修复 Flutter 中的“程序类型已存在:android.support.v4.os.ResultReceiver$MyResultReceiver”错误
- java - 如何将文本视图数据从第一个活动发送到第三个活动
- pdo - 如何使用 PDO 将我的 MySQL Case When 语句插入 PHP,以便我可以将输出添加到我的柱形图中
- javascript - 如何解决 ImageBackground 在 React-Native 中不采用全宽问题?
- python - How to fix an error with np.where function?
- java - 前端和后端分开部署的 Cookie 的使用(域)
- angular - (本机脚本):共享热点时布局上出现空白白线/空格(ios 蓝条)?
- string - 如何在 Dart 中将 ibm 字符类型字节转换为字符串?
- mit-scratch - I have 4 identical sprites that don't do the same thing, Why?
- excel - Application.Evaluate Does not work with SUMIF with ">" or "<" condition