【问题标题】:Twilio Javascript Function to Parse Array用于解析数组的 Twilio Javascript 函数
【发布时间】:2021-04-01 17:06:07
【问题描述】:

我有一个 twilio Javascript 函数,在有人拨打相关的工作室流程电话后立即在我的工作室流程中执行 #。这个函数应该检查当前是否有正在进行的电话会议并返回“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 无法在工作室流程中解析数组,因此必须在函数调用中完成:@ 987654321@ "注意,虽然数组是合法的JSON,但是如果你的请求返回一个对象数组,它是不会被解析的。"

所以我只需要检查会议数组是否为空,如果是,则将“False”返回到我的工作室流程,或者是否有活动会议(即数组长度> 0)然后返回“真的”。返回“True”或“False”将允许我在我的工作室流程中执行 if/else 小部件,以将呼叫者连接到现有会议或开始新的电话会议。

当没有活动的电话会议时,Postman 中的响应如下所示(注意 Conferences 数组为空):

我对 Javascript 的了解几乎为零,但我认为我已经接近了。

【问题讨论】:

    标签: javascript twilio


    【解决方案1】:

    更新:添加了基于 cmets 的 async/await 函数。

    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)})
    };
    

    【讨论】:

    • 不确定这是否会始终有效,您的回调不在正确的位置@dylan,twilio.com/docs/runtime/functions/…(因为它返回一个承诺,您需要等待)
    • 我编辑了代码以包含 async/await 函数。如果这看起来不错,请告诉我。
    【解决方案2】:

    最新更新看起来好多了@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));
    };
    

    【讨论】:

    • 您能详细说明一下吗?如果数组为空,!array 应该返回 True,如果不为空,则返回 False。不是说你错了,但是当我需要 3 个时,我很难只用一个电话号码进行测试。
    • 我认为这与在回调中返回布尔值 false 有某种时髦的效果有关,因为没有返回任何内容。
    猜你喜欢
    • 2021-07-08
    • 2022-01-26
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多