基于ManyChat、Zapier与JavaScript实现问卷分档消息推送求助
Fixing Zapier JavaScript to Calculate Survey Scores & Deliver Targeted Messages
Hey there! Let's get your Zapier code working properly so it can calculate the survey score and send the right Slovenian message based on the total. Your original code had a couple of key issues that were preventing the message logic from running—let's break this down step by step.
What Was Wrong With the Original Code?
- Early Return Killed Subsequent Logic: The first
returnstatement immediately sent back the calculated score, which meant none of theifconditions below ever got executed. Functions stop running as soon as areturnis hit! - Incorrect Condition Comparisons: You were comparing the numeric score to string values like
'<25'and listing every number in a range as a string—this won't work for numeric evaluations. We need to use proper numeric operators (<,>=,<=,>) instead.
Corrected Code
Here's a cleaned-up, functional version that does exactly what you need:
// Calculate total score by summing all 15 question values (Yes=3, No=1) const totalScore = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] .map(questionNum => Number(inputData[`q${questionNum}`])) .reduce((runningTotal, questionScore) => runningTotal + questionScore, 0); // Determine which message to send based on the total score let responseMessage; if (totalScore < 26) { responseMessage = "Videti je, da so vaše prehranske navade ustrezne. Za izboljšanje priporočamo jemanje multivitaminskih/mineralnih tablet!"; } else if (totalScore >= 26 && totalScore <= 35) { responseMessage = "Multivitaminski/mineralni dodatek k prehrani bo vašemu telesu pomagal ohraniti esencialna hranila, ki jih potrebuje, skupaj z drugimi označenimi dodatki!"; } else if (totalScore > 35) { responseMessage = "Vnos multivitaminov/mineralov bi vam zagotovo koristil. Z bolj uravnoteženo prehrano in dodatkom multivitaminov/mineralov pa bi potrebovali še vnos drugih vitaminov/mineralov!"; } // Return both the score and message for your Zap to use (adjust if you only need one) return { totalScore, responseMessage };
Key Improvements Explained
- Simplified Score Calculation: Instead of writing out 15 individual
Number(inputData.qX)calls, we use an array of question numbers, map over them to get each score, then reduce to sum everything up. This is easier to read and maintain if you ever add/remove questions. - Proper Conditional Logic: We first calculate the score, then use clear numeric ranges to pick the right message. No more string comparisons or listing every number in a range!
- Useful Return Value: The code returns both the total score and the message. You can use the
responseMessagefield directly in your Zap to send the message to the subscriber, andtotalScorecan be logged or stored for later analysis if needed.
Quick Note for Your Zap Setup
Make sure in your Zap's next step (sending the Messenger message) you select the responseMessage output from this code step as the message content. That's how the correct Slovenian text will get pushed to the user!
内容的提问来源于stack exchange,提问作者Suzana Kondić
相关产品推荐
相关产品推荐

