Google Apps Script telegram bot - waiting for user response before executing conditional logic

I’m working on a telegram bot using Google Apps Script and I’ve encountered an issue. I want the bot to stop and wait for user input before it carries on with the next part of my code.

Here’s what I’m trying to achieve: when a user sends the /poll command, the bot should ask “what’s your mood today?”. It then needs to wait until the user responds before continuing with my conditional checks.

Currently, my code sends out the question, but it doesn’t pause correctly:

function poll(messageData){
  var question1 = {
    'chat_id': messageData.message.chat.id,
    'text': 'what\'s your mood today?'
  }
  var apiMethod = 'sendMessage';
  var requestOptions = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(question1)
  }
  var apiResponse = UrlFetchApp.fetch('https://api.telegram.org/bot' + botToken + '/' + apiMethod, requestOptions);

var userInput = messageData.message.text;
if(userInput == "good"){
   nextPhase = '2'; //this needs to be fixed
}

How can I make the script actually wait for the user’s reply before moving on to check the if statement?

The problem is you’re mixing synchronous and asynchronous patterns. Google Apps Script doesn’t work that way - each Telegram webhook hits your script as a completely separate execution. Variables don’t stick around between calls. You need state management. I store conversation states in PropertiesService or a Google Sheet. When someone sends /poll, I save something like waiting_for_mood along with their chat ID. Then in my main webhook handler, I check for pending states before processing any message. Create a function that checks user states at the start of every webhook execution. If someone has waiting_for_mood set, handle their response and clear the state. Treat every incoming message like it might be answering a previous question.

yeah, ur mixing sync and async approaches. each telegram msg triggers a new script execution, so your userInput still refers to the original /poll msg instead of the reply.

use PropertiesService.setProperty() to track chat states: PropertiesService.setProperty(chatId + '_state', 'awaiting_mood'). check for pending states in your webhook handler b4 processing new messages. works perfectly for multi-step convos.

You’ve got a fundamental misunderstanding of how webhooks work. Each Telegram message triggers a completely fresh script execution - you can’t pause and wait within the same run. Your code’s trying to read the user’s response from the same message that triggered /poll, which obviously won’t work. You need to restructure this as a state machine. When someone sends /poll, store their chat ID and conversation step somewhere persistent like PropertiesService. Then modify your main webhook handler to check if incoming messages are part of an ongoing conversation. If the user has a pending state, route their message to the right handler instead of treating it as a new command. This lets you build complex multi-step interactions that feel seamless even though each message gets processed independently.