- 該電子表格包含專案 1,部署為具有以下權限的 Web 應用程式:
Execute as: Me, Who has access: Anyone.
網頁應用
function doPost(e) {
myLog('Received from Addon: ' JSON.stringify(e));
// console.log('parameters from caller ' JSON.stringify(e));
return ContentService.createTextOutput(JSON.stringify(e));
}
一個 webhook aTelegram-bot 和這個 webapp 設定。
- 我正在使用此電子表格來測驗(作為附件)另一個專案 2。
添加在
function sendPost() {
var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();
// var webAppUrl = "https://script.google.com/macros/s/#####/exec"; // 7: Part_1 - WebApp: My
var webAppUrl = "https://script.google.com/macros/s/###/exec"; // 7: Part_1 - WebApp: Tester
// var auth = ScriptApp.getOAuthToken();
// var header = { 'Authorization': 'Bearer ' auth };
var payload = { scriptName: 'updateData', sheetURL: 'sheetURL' };
var options = {
method: 'post',
// headers: header,
muteHttpExceptions: true,
payload: payload
};
var resp = UrlFetchApp.fetch(webAppUrl, options);
var respCode = resp.getResponseCode();
console.log('resp: ' respCode);
myLog(respCode);
var respTxt = resp.getContentText();
myLog('Response from webApp: ' respTxt);
console.log('resp: ' respTxt);
}
這是該程序的簡短視頻(英文字幕)。
- 我運行 sendPost() 并且一切正常。專案 2 將資料發送到 webapp,后者回傳它。由于這是一個容器系結腳本而不是獨立腳本,因此我無法查看 GCC 記錄器中的日志。因此,我在自定義記錄器中查看它們并正常添加條目。
還https://api.telegram.org/bot{API_token}/getWebhookInfo顯示沒有錯誤:
{"ok":true,"result": {"url":"https://script.google.com/macros/s/###/exec", "has_custom_certificate":false, "pending_update_count":0, "max_connections":40,"ip_address":"142.250.***.***"}}
- Now I am sending a message from the chat with the bot. The doPost(e) function in the webapp accepts it and writes it to the spreadsheet. However, everything is not limited to one message. Requests from the bot come and come, and the logger creates more and more new rows in the spreadsheet. This happens until I redeploy the webapp with the doPost () function commented out. I tried to figure out if this is a limited loop or not. My patience was only enough for 20 such iterations, because as a result, the messages start repeating at intervals of about 1 minute. Then I have to reinstall the webhook.
In any case, it interferes with testing the addon.
- GetWebhookInfo is now showing that there is a "Wrong response from the webhook: 302 Moved Temporarily" error:
{"ok":true,"result": {"url":"https://script.google.com/macros/s/###/exec", "has_custom_certificate":false, "pending_update_count":1, "last_error_date":1635501472, "last_error_message":"Wrong response from the webhook: 302 Moved Temporarily", "max_connections":40,"ip_address":"142.250.1***.***"}}
- Googling revealed several possible reasons. From url to the script has changed to MITM in your network. I do not really believe in MITM and I suppose that this is due to the fact that the spreadsheet is open in testing mode as add-on and the URL of the webapp has changed in this mode. If so, then I'm not sure if this is the correct behavior of the testing system. In theory, such a situation should have been provided for and the webap url should remain unchanged. But maybe I'm wrong and the reason is different, so
QUESTION: Has anyone come across such a situation and will suggest a workaround on how to test a script as an addon in such conditions?
uj5u.com熱心網友回復:
http-status-code-302指重定向。如果ContentService使用,Google 會臨時將資源重定向到另一個域以提供內容。使用HtmlService. 因此,如果問題與重定向有關,請HtmlService改用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/344403.html
標籤:google-apps-script web-applications telegram-bot add-on http-status-code-302
