You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Twilio Function中获取AddOns中的recommendation参数值

How to Extract "recommendation":"PASS" from Twilio AddOns in a Twilio Function

Got it, let's walk through this straightforwardly. When Twilio sends that POST request to your Function, all the call details—including the AddOns payload you shared—come through in the event parameter. Here's exactly how to grab that specific recommendation value, while checking the CallStatus as you requested:

First, here's the complete Function code, with comments explaining each step:

exports.handler = function(context, event, callback) {
  // First verify we're dealing with a ringing call
  if (event.CallStatus === "ringing") {
    // Pull the AddOns object directly from the incoming event payload
    const addOns = event.AddOns;

    // Safely navigate the nested structure to avoid errors if data is missing
    if (addOns?.status === "successful" && addOns.results?.marchex_cleancall) {
      // Dig into the nested result objects (matches your provided AddOns structure)
      const marchexData = addOns.results.marchex_cleancall.result.result;
      const callRecommendation = marchexData.recommendation;

      // Now you can use this value however you need—log it, return it, etc.
      console.log(`Marchex CleanCall Recommendation: ${callRecommendation}`);

      // Example response (customize this based on your workflow)
      callback(null, { 
        status: "success",
        recommendation: callRecommendation
      });
    } else {
      // Handle cases where AddOns failed or Marchex data isn't present
      callback(null, { 
        status: "error",
        message: "Marchex CleanCall result unavailable or unsuccessful"
      });
    }
  } else {
    // Handle calls that aren't in ringing status
    callback(null, { 
      status: "info",
      message: "Call is not in ringing status"
    });
  }
};

Key Notes:

  • Optional Chaining (?.): I used ?. to safely access nested properties. This prevents your Function from crashing if any layer (like results or marchex_cleancall) is missing or undefined.
  • Structure Match: The code follows exactly the AddOns structure you provided—notice the two levels of result (marchex_cleancall.result.result) before getting to recommendation.
  • Flexibility: Once you have callRecommendation, you can integrate it into your workflow—like routing the call differently based on whether it's "PASS" or another value.

Just make sure your Twilio Function is set up to receive webhook requests from Twilio, and test with a real call (or use Twilio's Request Inspector to verify the payload matches your example).

内容的提问来源于stack exchange,提问作者tech amit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:07:43