Hey everyone! I’m having trouble with my Telegram bot script. It works fine up to currentstep = '2', but when I try to set it to ‘3’ in the survey function, it doesn’t update properly. I’ve attempted to adjust the code, but the third step never executes as expected. Here is a simplified version of my code:
function checkUserInput(msg) {
if (msg.text === '/begin') {
stage = '1';
} else if (msg.text === '/questionnaire') {
stage = '2';
}
}
function runBot(msg) {
if (stage === '1') {
welcome(msg);
} else if (stage === '2') {
questionnaire(msg);
} else if (stage === '3') {
finalStep(msg);
}
}
function handleRequest(request) {
var msg = JSON.parse(request.postData.contents);
checkUserInput(msg);
runBot(msg);
}
Any insights on why the global variable isn’t updating to ‘3’ would be really appreciated!
I’ve dealt with similar issues in my Telegram bots. The problem is likely that Google Apps Script doesn’t maintain variable states between executions. Each time your bot receives a message, it starts fresh, forgetting previous values.
To fix this, you need to use persistent storage. PropertiesService is perfect for this. Here’s how I’d modify your code:
function checkUserInput(msg) {
var props = PropertiesService.getScriptProperties();
if (msg.text === '/begin') {
props.setProperty('stage', '1');
} else if (msg.text === '/questionnaire') {
props.setProperty('stage', '2');
}
}
function runBot(msg) {
var props = PropertiesService.getScriptProperties();
var stage = props.getProperty('stage');
if (stage === '1') {
welcome(msg);
} else if (stage === '2') {
questionnaire(msg);
} else if (stage === '3') {
finalStep(msg);
}
}
This way, your stage persists between executions. Just make sure you’re setting stage to ‘3’ somewhere, probably at the end of your questionnaire function.
Also, add some logging (Logger.log()) in your functions to track execution flow and variable values. It’ll help you pinpoint where things might go wrong.
sounds like ur script might be losing state between executions. try using PropertiesService to persist the stage variable. something like:
var props = PropertiesService.getScriptProperties();
props.setProperty(‘stage’, ‘3’);
// later
var currentStage = props.getProperty(‘stage’);
this should keep ur stage across different runs. hope it helps!
I’ve encountered similar issues with Google Apps Script and Telegram bots. The problem likely stems from the stateless nature of web apps. Each time your script runs, it starts fresh, losing any previous variable values.
A solution is to use PropertiesService for persistent storage. Here’s how you could modify your code:
function checkUserInput(msg) {
var props = PropertiesService.getScriptProperties();
if (msg.text === '/begin') {
props.setProperty('stage', '1');
} else if (msg.text === '/questionnaire') {
props.setProperty('stage', '2');
}
}
function runBot(msg) {
var props = PropertiesService.getScriptProperties();
var stage = props.getProperty('stage');
if (stage === '1') {
welcome(msg);
} else if (stage === '2') {
questionnaire(msg);
} else if (stage === '3') {
finalStep(msg);
}
}
This approach ensures your stage persists between executions. Also, make sure you’re setting stage to ‘3’ somewhere in your code, perhaps at the end of the questionnaire function.