我有一个twilio Javascript函数,它在有人调用关联的studio flow phone #后立即在我的studio flow中执行。此函数用于检查当前是否有活动的会议呼叫正在进行,并返回"True“或"False”,这样我就可以在if/else小部件中使用该字符串来连接主叫方或发起新的会议。
// This is your new function. To start, set the name and path on the left.
exports.handler = function(context, event, callback) {
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("GET", "https://api.twilio.com/2010-04-01/Accounts/myAccountSid/Conferences.json?FriendlyName=mySidDocumentName");
xhr.setRequestHeader("Authorization", "Basic myAuthString");
xhr.send();
xhr.addEventListener("readystatechange", function() {
if(this.readyState == 4) {
console.log(JSON.stringify(xhr.responseText));
var jsonResponse = JSON.parse(xhr.responseText);
var arrayLength = Object.keys(jsonResponse.Conferences[jsonResponse]).length;
if (arrayLength > 0) {
var isConferenceOngoing = "True"
} else {
var isConferenceOngoing = "False"
}
}
return callback(null, isConferenceOngoing);
});
};在响应中,我感兴趣的“会议”键是一个数组,这会导致问题,因为Twilio不能解析工作室流程中的数组,因此它必须在函数调用中完成:https://www.twilio.com/docs/studio/widget-library/http-request“请注意,尽管数组是有效的https://www.twilio.com/docs/studio/widget-library/http-request,但如果您的请求返回一个对象数组,它将不会被解析。”
因此,我只需要检查会议数组是否为空,如果是,则返回"False“到我的演播室流程,或者如果有活动的会议(即数组长度> 0),则返回"True”。返回"True“或"False”将允许我在演播室流程中执行if/else小部件,以便将呼叫者连接到现有会议或开始新的会议呼叫。
以下是没有活动会议呼叫时Postman中的响应(请注意,会议数组为空):

我对Javascript的了解几乎为零,但我认为我已经很接近了。
发布于 2021-04-02 03:43:41
更新:添加了基于注释的异步/等待功能。
exports.handler = function(context, event, callback) {
const twilioClient = context.getTwilioClient();
let responseObject = twilioClient.conferences
.list({status: 'in-progress'})
var isConferenceOngoing = null
async function setConferenceVariable() {
if (responseObject.Length > 0)
{
isConferenceOngoing = "True"
} else {
isConferenceOngoing = "False"
}
}
async function getConferenceDetails() {
await setConferenceVariable();
return callback(null, isConferenceOngoing);
}
getConferenceDetails()
};最新更新:下面是更简单的方法。
exports.handler = function(context, event, callback) {
const twilioClient = context.getTwilioClient();
twilioClient.conferences
.list({status: 'in-progress'})
.then(conferences => { callback(null, !conferences.length)})
};发布于 2021-04-02 20:36:11
@dylan的最新更新看起来好多了。当返回值为false时,我没有看到返回值,因此出现了以下情况:
exports.handler = function(context, event, callback) {
const twilioClient = context.getTwilioClient();
twilioClient.conferences
.list({status: 'in-progress'})
.then(conferences => callback(null, {result: !conferences.length}))
.catch(err => callback(err));
};https://stackoverflow.com/questions/66908807
复制相似问题