Implementing a Telegram Bot Survey Using Generators

How can I efficiently manage sequential prompts in my Telegram bot survey without relying on if…else chains? I tried using a generator function to yield messages and capture responses, but I’m unclear on the proper way to invoke it step by step. Here is an alternate code snippet demonstrating my approach:

function* handleSurveyFlow() {
  let firstResponse = yield bot.askMessage(userToken, "Please enter your first name:");
  bot.on('update', updateData => {
    let firstName = updateData.text;
  });

  let secondResponse = yield bot.askMessage(userToken, "Now, provide your last name:");
  bot.on('update', updateData => {
    let lastName = updateData.text;
  });
}

I would appreciate any guidance on how to call the generator consistently to process the user inputs in a more structured way.

hey, try sticking with chained promises or a state machine. i ended up wrapping each survey step in an async function that calls the next on response. feels more natural than juggling generator yields.

I have experimented with a similar approach while implementing sequential survey flows. My solution involved creating a control function that listens for updates and then calls next() on the generator with the received input. Essentially, you start the generator and then, once an update is detected, manually resume execution by passing the new value. This allows you to maintain a clear, step-by-step progression without nesting if…else constructs. In my experience, this method leads to more structured and maintainable code compared to chaining promises or using a full-fledged state machine.