/** * Google Apps Script Proxy for Telegram Bot API - FIXED VERSION * This handles redirects and provides better error handling */ function doPost(e) { try { Logger.log('=== INCOMING REQUEST ==='); Logger.log('Headers: ' + JSON.stringify(e)); Logger.log('Post data: ' + e.postData.contents); // Parse the incoming request const requestData = JSON.parse(e.postData.contents); Logger.log('Parsed request: ' + JSON.stringify(requestData)); // Extract parameters const method = requestData.method; const botToken = requestData.bot_token; if (!method || !botToken) { Logger.log('ERROR: Missing method or bot_token'); return ContentService .createTextOutput(JSON.stringify({ ok: false, error: 'Missing method or bot_token', received: requestData })) .setMimeType(ContentService.MimeType.JSON); } // Build Telegram API URL const telegramUrl = `https://api.telegram.org/bot${botToken}/${method}`; Logger.log('Telegram URL: ' + telegramUrl); // Prepare payload (remove proxy-specific fields) const payload = { ...requestData }; delete payload.method; delete payload.bot_token; Logger.log('Final payload: ' + JSON.stringify(payload)); // Make request to Telegram API with better options const options = { method: 'POST', headers: { 'Content-Type': 'application/json', 'User-Agent': 'GoogleAppsScript-TelegramProxy/1.0' }, payload: JSON.stringify(payload), followRedirects: true, validateHttpsCertificates: true }; Logger.log('Making request to Telegram...'); const response = UrlFetchApp.fetch(telegramUrl, options); const responseCode = response.getResponseCode(); const responseText = response.getContentText(); Logger.log('Response code: ' + responseCode); Logger.log('Response text: ' + responseText); if (responseCode !== 200) { Logger.log('ERROR: Bad response code from Telegram'); return ContentService .createTextOutput(JSON.stringify({ ok: false, error: `Telegram API returned ${responseCode}`, response: responseText })) .setMimeType(ContentService.MimeType.JSON); } // Return the response return ContentService .createTextOutput(responseText) .setMimeType(ContentService.MimeType.JSON); } catch (error) { Logger.log('FATAL ERROR: ' + error.toString()); Logger.log('Stack trace: ' + error.stack); return ContentService .createTextOutput(JSON.stringify({ ok: false, error: 'Proxy error: ' + error.toString(), timestamp: new Date().toISOString() })) .setMimeType(ContentService.MimeType.JSON); } } function doGet(e) { Logger.log('GET request received'); Logger.log('Parameters: ' + JSON.stringify(e.parameters)); // Health check endpoint return ContentService .createTextOutput(JSON.stringify({ status: 'Google Apps Script Proxy is running', timestamp: new Date().toISOString(), version: '1.1.0', deployment: 'active' })) .setMimeType(ContentService.MimeType.JSON); } /** * Test function - run this in the editor to verify setup */ function testDeployment() { Logger.log('=== TESTING DEPLOYMENT ==='); // Test GET endpoint const getResult = doGet({parameters: {}}); Logger.log('GET test result: ' + getResult.getContent()); // You can test POST here with your actual bot token Logger.log('=== TEST COMPLETE ==='); Logger.log('If you see this, the script is working. Deploy as web app now.'); }