Dmitry Beresnev
commited on
Commit
·
93393a6
1
Parent(s):
3c6f046
add proxy
Browse files- proxy/google_apps_script.js +120 -0
proxy/google_apps_script.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Google Apps Script Proxy for Telegram Bot API - FIXED VERSION
|
| 3 |
+
* This handles redirects and provides better error handling
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
function doPost(e) {
|
| 7 |
+
try {
|
| 8 |
+
Logger.log('=== INCOMING REQUEST ===');
|
| 9 |
+
Logger.log('Headers: ' + JSON.stringify(e));
|
| 10 |
+
Logger.log('Post data: ' + e.postData.contents);
|
| 11 |
+
|
| 12 |
+
// Parse the incoming request
|
| 13 |
+
const requestData = JSON.parse(e.postData.contents);
|
| 14 |
+
Logger.log('Parsed request: ' + JSON.stringify(requestData));
|
| 15 |
+
|
| 16 |
+
// Extract parameters
|
| 17 |
+
const method = requestData.method;
|
| 18 |
+
const botToken = requestData.bot_token;
|
| 19 |
+
|
| 20 |
+
if (!method || !botToken) {
|
| 21 |
+
Logger.log('ERROR: Missing method or bot_token');
|
| 22 |
+
return ContentService
|
| 23 |
+
.createTextOutput(JSON.stringify({
|
| 24 |
+
ok: false,
|
| 25 |
+
error: 'Missing method or bot_token',
|
| 26 |
+
received: requestData
|
| 27 |
+
}))
|
| 28 |
+
.setMimeType(ContentService.MimeType.JSON);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Build Telegram API URL
|
| 32 |
+
const telegramUrl = `https://api.telegram.org/bot${botToken}/${method}`;
|
| 33 |
+
Logger.log('Telegram URL: ' + telegramUrl);
|
| 34 |
+
|
| 35 |
+
// Prepare payload (remove proxy-specific fields)
|
| 36 |
+
const payload = { ...requestData };
|
| 37 |
+
delete payload.method;
|
| 38 |
+
delete payload.bot_token;
|
| 39 |
+
|
| 40 |
+
Logger.log('Final payload: ' + JSON.stringify(payload));
|
| 41 |
+
|
| 42 |
+
// Make request to Telegram API with better options
|
| 43 |
+
const options = {
|
| 44 |
+
method: 'POST',
|
| 45 |
+
headers: {
|
| 46 |
+
'Content-Type': 'application/json',
|
| 47 |
+
'User-Agent': 'GoogleAppsScript-TelegramProxy/1.0'
|
| 48 |
+
},
|
| 49 |
+
payload: JSON.stringify(payload),
|
| 50 |
+
followRedirects: true,
|
| 51 |
+
validateHttpsCertificates: true
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
Logger.log('Making request to Telegram...');
|
| 55 |
+
const response = UrlFetchApp.fetch(telegramUrl, options);
|
| 56 |
+
const responseCode = response.getResponseCode();
|
| 57 |
+
const responseText = response.getContentText();
|
| 58 |
+
|
| 59 |
+
Logger.log('Response code: ' + responseCode);
|
| 60 |
+
Logger.log('Response text: ' + responseText);
|
| 61 |
+
|
| 62 |
+
if (responseCode !== 200) {
|
| 63 |
+
Logger.log('ERROR: Bad response code from Telegram');
|
| 64 |
+
return ContentService
|
| 65 |
+
.createTextOutput(JSON.stringify({
|
| 66 |
+
ok: false,
|
| 67 |
+
error: `Telegram API returned ${responseCode}`,
|
| 68 |
+
response: responseText
|
| 69 |
+
}))
|
| 70 |
+
.setMimeType(ContentService.MimeType.JSON);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
// Return the response
|
| 74 |
+
return ContentService
|
| 75 |
+
.createTextOutput(responseText)
|
| 76 |
+
.setMimeType(ContentService.MimeType.JSON);
|
| 77 |
+
|
| 78 |
+
} catch (error) {
|
| 79 |
+
Logger.log('FATAL ERROR: ' + error.toString());
|
| 80 |
+
Logger.log('Stack trace: ' + error.stack);
|
| 81 |
+
|
| 82 |
+
return ContentService
|
| 83 |
+
.createTextOutput(JSON.stringify({
|
| 84 |
+
ok: false,
|
| 85 |
+
error: 'Proxy error: ' + error.toString(),
|
| 86 |
+
timestamp: new Date().toISOString()
|
| 87 |
+
}))
|
| 88 |
+
.setMimeType(ContentService.MimeType.JSON);
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
function doGet(e) {
|
| 93 |
+
Logger.log('GET request received');
|
| 94 |
+
Logger.log('Parameters: ' + JSON.stringify(e.parameters));
|
| 95 |
+
|
| 96 |
+
// Health check endpoint
|
| 97 |
+
return ContentService
|
| 98 |
+
.createTextOutput(JSON.stringify({
|
| 99 |
+
status: 'Google Apps Script Proxy is running',
|
| 100 |
+
timestamp: new Date().toISOString(),
|
| 101 |
+
version: '1.1.0',
|
| 102 |
+
deployment: 'active'
|
| 103 |
+
}))
|
| 104 |
+
.setMimeType(ContentService.MimeType.JSON);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
/**
|
| 108 |
+
* Test function - run this in the editor to verify setup
|
| 109 |
+
*/
|
| 110 |
+
function testDeployment() {
|
| 111 |
+
Logger.log('=== TESTING DEPLOYMENT ===');
|
| 112 |
+
|
| 113 |
+
// Test GET endpoint
|
| 114 |
+
const getResult = doGet({parameters: {}});
|
| 115 |
+
Logger.log('GET test result: ' + getResult.getContent());
|
| 116 |
+
|
| 117 |
+
// You can test POST here with your actual bot token
|
| 118 |
+
Logger.log('=== TEST COMPLETE ===');
|
| 119 |
+
Logger.log('If you see this, the script is working. Deploy as web app now.');
|
| 120 |
+
}
|