2
  1. The spreadsheet contains project 1, deployed as a webapp with permissions: Execute as: Me, Who has access: Anyone.

Webapp

function doPost(e) {
  myLog('Received from Addon: ' + JSON.stringify(e));
  // console.log('parameters from caller ' + JSON.stringify(e));
  return ContentService.createTextOutput(JSON.stringify(e));
}

A webhook aTelegram-bot and this webapp is set.

  1. I am using this spreadsheet for testing (as add-on) of another project 2.

Add-on

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);
}

Here is a short video of the process (EN-subtitles).

  1. I run sendPost() and everything works fine. Project 2 sends data to the webapp, which returns it. Since this is a Container-bound script and not a standalone one, I cannot watch the logs in the GCC logger. Therefore, I look at them in the custom logger and the entries are added normally.

Also https://api.telegram.org/bot{API_token}/getWebhookInfo shows that there are no errors:

{"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.***.***"}}
  1. 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.

  1. 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***.***"}}

  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?

Boris Baublys
  • 952
  • 10
  • 22
  • 1
    What is `getWebhookInfo`? – TheMaster Oct 29 '21 at 13:36
  • `https://api.telegram.org/bot{API_token}/getWebhookInfo` – Boris Baublys Oct 29 '21 at 13:41
  • Link please. Also `However, everything is not limited to one message. Messages from the bot come and go, 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` is unclear. Could you reword it? What is `everything`. Is spreadsheet adding new rows a unexpected thing? – TheMaster Oct 29 '21 at 13:46
  • 1
    302 is just a way of redirection. If you use [`ContentService`, the redirection is expected](https://developers.google.com/apps-script/guides/content#redirects). – TheMaster Oct 29 '21 at 13:46
  • Endless chain of same message? `doPost` is called repeatedly from the same message? – TheMaster Oct 29 '21 at 13:59
  • Yes, Telegram sends an endless series of requests to one my message. – Boris Baublys Oct 29 '21 at 14:36
  • I read somewhere in Telegram FAQ that Telegram does not process redirects. – Boris Baublys Oct 29 '21 at 14:40
  • @TheMaster "Is spreadsheet adding new rows a unexpected thing?" I expect one message in return, but I get an endless series of them. – Boris Baublys Oct 29 '21 at 15:07
  • I'm not too familiar with telegram bots. I don't know why it's pinging the webhook many times for 1 message. May be ask a new question to only focus on that part? – TheMaster Oct 29 '21 at 15:09
  • Rephrased: When I send a message to a bot in Telegram, an endless chain of requestes appears. They are absent in Tedegram, but doPost (e) registers them – Boris Baublys Oct 29 '21 at 17:28
  • 1
    Just guessing, maybe it's because of the redirect? Because the redirect failed, the bot repeatedly pings the same webhook? If the bot can't handle redirect, maybe use `HtmlService` instead of `ContentService`? – TheMaster Oct 29 '21 at 17:32
  • Great idea, I'll try. But I would like to understand the difference. Do ContentService and HTMLService handle redirects differently? – Boris Baublys Oct 29 '21 at 18:08
  • 2
    Htmlservice doesn't redirect but it can only serve html – TheMaster Oct 29 '21 at 18:09
  • 1
    It looks like it helped. Anyway, now that I replaced the last line with `return htmlService.createHtmlOutput ('

    Hi

    ');`, there are no errors. Maybe you can make an answer so I can accept it?
    – Boris Baublys Oct 30 '21 at 14:01

1 Answers1

5

refers to redirection. If ContentService is used, Google temporarily redirects the resource to a another domain to serve the content. This redirection is not performed when using HtmlService. So, if the issue is related to redirection, use HtmlService instead.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Thanks! How now to transfer a Telegram message from a webapp to a spreadsheet in to the an installed add-on? scripts.rune doesn't seem to work here, because to use it, it seems that the addon and webapp need to refer to 1 project. In addition, webapp is a Container-bound script. If it was possible to add an eventListener to the addon, but it seems to be impossible too. And what event to listen to is also incomprehensible. Maybe you have any ideas ?? – Boris Baublys Oct 31 '21 at 15:01
  • @BorisBaublys Kindly ask a new question with preferably a diagram of the flow you're expecting. Why can't you have both addon and webapp under 1 project? Is the addon not in your control? Ask a new question with specific details. – TheMaster Oct 31 '21 at 15:41
  • Yes, I will ask a separate question. Sorry, what is "diagram of the flow"? Can't you link to a post with an example of such a diagram? "Is the addon not in your control?" The addon is under my control, but there is no webapp. Webapp is built into a spreadsheet that the user creates from my template. – Boris Baublys Oct 31 '21 at 16:54
  • 1
    @BorisBaublys Something like [this picture](https://stackoverflow.com/a/25313008/). If you have the addon, you can publish a webapp from that addon itself – TheMaster Oct 31 '21 at 18:56
  • I asked a new question [How to transfer data from webapp to addon](https://stackoverflow.com/questions/69789961/how-to-transfer-data-from-webapp-to-addon) – Boris Baublys Oct 31 '21 at 19:36