Hey everyone, I’m stuck with a problem in my Telegram bot. I made it using Google Apps Script to get product prices from a Google Sheet. It’s supposed to do these things:
Show a product list when someone types /start
Let them pick a product
Tell them the price
Wait for the next /start command
But it’s gone crazy! The bot won’t stop sending the product list over and over. I have to kill the deployment in Apps Script to make it stop. I think there’s something off with how I’m handling sessions or webhooks.
I’ve asked a few AIs for help, but nothing has worked so far. I’m quite new to coding, and I’m at my wits’ end. Could anyone help me figure out what’s causing this continuous loop?
Here’s a simplified version of my code:
function handleMessage(message) {
const chatId = message.chat.id;
const text = message.text || '';
if (text.startsWith('/start')) {
if (!userSessions[chatId]) {
userSessions[chatId] = true;
sendProductList(chatId);
}
} else {
sendMessage(chatId, "Use /start to see products.");
}
}
function handleCallbackQuery(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const productName = callbackQuery.data;
const price = getProductPrice(productName);
let responseText = price ? `Price: $${price}` : `No price for ${productName}`;
editMessage(chatId, callbackQuery.message.message_id, responseText);
answerCallbackQuery(callbackQuery.id);
delete userSessions[chatId];
}
I’ve dealt with similar issues in Telegram bots. The problem likely stems from improper webhook handling. Google Apps Script can sometimes re-execute the same webhook multiple times, causing the loop you’re experiencing.
A potential solution is to implement a simple state management system. You could use PropertiesService in Apps Script to store the current state for each user. This way, you can ensure each user only receives one product list per session.
Here’s a basic implementation idea:
function handleMessage(message) {
const chatId = message.chat.id;
const text = message.text || '';
const userProps = PropertiesService.getUserProperties();
if (text.startsWith('/start')) {
if (userProps.getProperty(chatId) !== 'list_sent') {
sendProductList(chatId);
userProps.setProperty(chatId, 'list_sent');
} else {
sendMessage(chatId, 'Product list already sent. Choose a product.');
}
} else {
sendMessage(chatId, 'Use /start to see products.');
}
}
This should prevent the infinite loop. Let me know if you need further assistance.
I’ve encountered a similar issue with Telegram bots before. The problem likely stems from how you’re handling webhook updates. Google Apps Script can sometimes re-execute the same webhook multiple times, causing the loop you’re experiencing.
To fix this, I’d suggest implementing a simple caching mechanism. You can use the CacheService in Apps Script to store a unique identifier for each update, ensuring you process each one only once.
Here’s a rough idea of how to modify your code:
function doPost(e) {
const update = JSON.parse(e.postData.contents);
const updateId = update.update_id;
if (CacheService.getScriptCache().get(updateId)) {
return ContentService.createTextOutput('OK');
}
CacheService.getScriptCache().put(updateId, 'processed', 600);
// Your existing logic here
return ContentService.createTextOutput('OK');
}
This approach should help prevent the infinite loop. Let me know if you need more clarification!
hey there HappyDancer99! sounds like ur bot’s got a case of the zoomies lol. have u tried adding a check in ur handleMessage function to see if a product list is already sent? maybe something like:
if (!userSessions[chatId].listSent) {
sendProductList(chatId);
userSessions[chatId].listSent = true;
}