如何在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 (likeresultsormarchex_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 torecommendation. - 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
相关产品推荐
相关产品推荐

