Troubleshooting: Telegram Bot Stuck in Product List Loop

Hey everyone, I’m new to coding and I’m having trouble with my Telegram bot built with Google Apps Script to fetch product prices from a Google Sheet. It should:

  1. Show the product list when someone types /start only once.
  2. Allow product selection.
  3. Display the price for the selected product just one time.
  4. Wait until the process is restarted.

Instead, the bot repeatedly sends the product list in a loop until I disable the deployment. I suspect the issue is with session handling or webhook triggers, but I’m not sure.

I’ve tried multiple AIs for help with my code, but nothing has worked so far. Here’s a simplified version of the logic I’m using:

function handleMessage(message) {
  const chatId = message.chat.id;
  if (message.text === '/start') {
    sendProductList(chatId);
  }
}

function sendProductList(chatId) {
  const products = ['Apple', 'Banana', 'Orange'];
  const keyboard = products.map(p => [{ text: p, callback_data: p }]);
  sendMessageWithKeyboard(chatId, 'Pick a product:', keyboard);
}

function handleCallbackQuery(query) {
  const chatId = query.message.chat.id;
  const product = query.data;
  const price = getPrice(product);
  sendMessage(chatId, `Price of ${product}: $${price}`);
}

Any ideas on why this looping issue might be happening? Thanks in advance!

hey tom, sounds like a pain! have u tried using a simple flag variable to track if the product list was sent? somethin like:

let listSent = false;

function handleMessage(message) {
  if (message.text === '/start' && !listSent) {
    sendProductList(chatId);
    listSent = true;
  }
}

this might stop the loop. good luck mate!

I’ve been there, Tom. The looping issue can be frustrating, but there’s a simple fix. Try implementing a user state management system. Here’s what I did:

  1. Create a global object to store user states.
  2. In handleMessage, check the user’s state before sending the product list.
  3. Update the state after sending the list or price.

Something like this:

const userStates = {};

function handleMessage(message) {
  const chatId = message.chat.id;
  if (!userStates[chatId]) userStates[chatId] = 'initial';

  if (message.text === '/start' && userStates[chatId] === 'initial') {
    sendProductList(chatId);
    userStates[chatId] = 'awaiting_selection';
  }
}

function handleCallbackQuery(query) {
  const chatId = query.message.chat.id;
  if (userStates[chatId] === 'awaiting_selection') {
    // Handle product selection
    userStates[chatId] = 'initial';
  }
}

This approach solved my looping problem. Give it a shot and let me know how it goes!

I’ve encountered a similar issue with Telegram bots before. The looping problem you’re experiencing is likely due to the webhook continuously receiving and processing the same message multiple times. To resolve this, you need to implement a way to acknowledge and ‘consume’ each message once it’s been processed.

Try adding a unique identifier check for each message. You can use the message_id or update_id provided by Telegram. Store these IDs in a temporary cache or database. Before processing any message, check if its ID has been seen before. If it has, skip processing. This should prevent the loop.

Also, ensure your webhook is properly configured to only receive new updates. You might want to review your setWebhook settings in the Telegram Bot API.

Lastly, consider implementing a simple state machine to manage the conversation flow. This will help you keep track of where each user is in the interaction process, preventing unwanted repetitions.