i have similar issue with telegram connecting to google GAS. I get infinite loop of good reply from GAS into my telegram group. Debugging looks good with no issues. GAS deployed as webapp. Passing single parameter from telegram:
https://api.telegram.org/bot1234567890......./setWebhook?url=https://script.google.com/macros/s/1234567890................/exec?community=PARM12345
https://api.telegram.org/bot123456...../getWebhookInfo shows:
{
"ok": true,
"result": {
"url": "https://script.google.com/macros/s/123456......./exec?community=GINDI",
"has_custom_certificate": false,
"pending_update_count": 1,
"last_error_date": 1764588546,
"last_error_message": "Wrong response from the webhook: 302 Moved Temporarily",
"max_connections": 40,
"ip_address": "100.200.700.500"
}
}
Did not configure any google APIs, ot the configuration.
function doPost(e) {
const finalResponse = ContentService.createTextOutput('{}').setMimeType(ContentService.MimeType.JSON);
if (!e || !e.postData || !e.postData.contents) {
// ❗ prevents Telegram from retrying
return finalResponse;
}
// Initialize services
let update = null;
try {
update = JSON.parse(e.postData.contents);
} catch (err) {
//Logger.log("Invalid JSON update, ignored.");
return finalResponse;
}
if (update.message?.from?.is_bot) {
//Logger.log("Ignored bot-originated message to prevent loop.");
return finalResponse;
}
const messageText = update.message?.text;
if (!messageText || typeof messageText !== "string") {
//Logger.log("Ignored non-text message.");
return finalResponse;
}
const trimmed = messageText.trim();
if (!trimmed.startsWith("#")) {
//Logger.log(`Skipping: Not a command or text missing. Exiting.`);
return finalResponse;
}
const cache = CacheService.getScriptCache();
const updateId = update.update_id?.toString();
if (updateId) {
if (cache.get(updateId)) {
Logger.log("Duplicate update detected → ignored.");
return finalResponse;
}
cache.put(updateId, "1", 30);
}
const lock = LockService.getScriptLock();
if (!lock.tryLock(500)) {
Logger.log("Lock busy → ignored safely.");
return finalResponse;
}
try {
let config = getScriptPropsFallback(); // Load service default config
// --- CORE BUSINESS LOGIC STARTS HERE ---
config.communityName = e.parameter?.community || "";
config.chatId = update.message?.chat?.id;
config.userId = update.message?.from?.id;
config.message_id = update.message?.message_id;
config.messageText =trimmed;
// MAIN COMMAND DISPATCH
config = Fun1(config);
Func2(config);
} catch (err) {
Logger.log(`doPost (Webhook Entry) CRASHED on update ${updateId}:`, err);
try {
sendReply(update.message.chat.id, update.message.message_id, "❌ System error. Admin notified.", getScriptPropsFallback());
} catch (e2) {}
} finally {
try { lock.releaseLock(); } catch (e) {}
}
return finalResponse;
}
any help is welcomed. please reply to [email protected]