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

基于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?

  1. Early Return Killed Subsequent Logic: The first return statement immediately sent back the calculated score, which meant none of the if conditions below ever got executed. Functions stop running as soon as a return is hit!
  2. 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 responseMessage field directly in your Zap to send the message to the subscriber, and totalScore can 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ć

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:21:26