I’m working on a Telegram bot using Google Apps Script and having trouble with a global variable that tracks workflow steps. My bot processes commands and updates a step counter, but it seems stuck and won’t progress beyond a certain point.
The issue is that my step variable updates to ‘2’ when users trigger the survey command, but when I try to set it to ‘3’ inside the survey function, the bot doesn’t recognize this change and won’t execute the next function in the workflow.
function handleUserInput(botData){
var userMessage = botData.message.text;
if(userMessage == "/begin" || userMessage == "/start"){
workflowStep = '1';
return;
}
if (userMessage == "/poll" || userMessage == "/questionnaire"){
workflowStep = '2';
return;
}
return;
}
function executeWorkflow(botData){
if(workflowStep == "1"){
initializeBot(botData);
return;
}
if(workflowStep == "2"){
runQuestionnaire(botData);
return;
}
if(workflowStep == "3"){
finalizeProcess(botData);
return;
}
}
function doPost(e){
var botData = JSON.parse(e.postData.contents);
handleUserInput(botData);
executeWorkflow(botData);
}
Is there some restriction on how many times I can modify global variables in Google Apps Script? The workflow stops progressing even though I’m updating the step variable inside my questionnaire function.
Google Apps Script doesn’t retain global variables between HTTP requests. Each time your Telegram bot receives a message, GAS initializes a new execution context, reverting your workflowStep variable to its default value.
I faced a similar issue while developing a multi-step form bot. For persistent storage, PropertiesService is suitable for simpler tasks, but if you anticipate multiple users, Google Sheets is a better choice. Create a sheet with user_id and workflow_step columns to manage individual workflows.
Furthermore, your current implementation has all users sharing the same workflow state, which can lead to conflicts during simultaneous usage. It’s crucial to track states based on user IDs.
Classic newbie mistake! Your variable gets wiped when the function ends - that’s just how serverless works. Nothing stays in memory between calls. Use PropertiesService.getScriptProperties() to save data, or you’ll keep hitting this same wall.
Your global variable keeps resetting because each doPost call runs independently. When Telegram sends a message, GAS executes doPost completely, then terminates. The next message starts fresh - your workflowStep goes back to its initial state. You’re losing the variable between HTTP requests.
I hit this same issue with my first GAS bot. You need to store state externally using PropertiesService or Google Sheets. Instead of your global variable, use PropertiesService.getScriptProperties().setProperty(‘workflowStep’, ‘2’) to save the step and getProperty(‘workflowStep’) to retrieve it.
Or store user states in a spreadsheet with user IDs as keys. This also lets you handle multiple users at once since each user needs their own workflow state tracked separately.