在 Google Apps 脚本的请求中找不到 postData

No postData found in the request in Google Apps Script

提问人:Deka Halane 提问时间:11/17/2023 最后编辑:TheMasterDeka Halane 更新时间:11/18/2023 访问量:36

问:

我正在尝试将消息从 Telegram 获取到 Google 表格。如何解决这个问题,每次我在 Google Apps Script 上运行时,我都没有得到任何响应。但是当我使用我的机器人在我的频道上发布一些内容并运行 getUpdates 时,它工作正常:doPost

{"ok":true,"result":[{"update_id":621505689,
"message":{"message_id":11,"from":{"id":1671067397,"is_bot":false,"first_name":"aisha","last_name":"halane","username":"aishamhalane","language_code":"en"},"chat":{"id":-1001994285933,"title":"aisha & Trading_Binance_Aisha_bot","is_forum":true,"type":"supergroup"},"date":1700229032,"text":"Hi for the last time"}}]}

我尝试删除 Webhook 并创建一个新的 webhook,也尝试多次部署它。

这是我运行doPost时的输出:

Execution log
2:46:47 PM  Notice  Execution started
2:46:47 PM  Info    Received doPost request
2:46:47 PM  Info    null
2:46:47 PM  Info    No postData found in the request
2:46:48 PM  Notice  Execution completed

代码:

var token = "XXXXXXX"; // FILL IN YOUR OWN TOKEN
var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = "https://script.google.com/macros/s/AKfycbxaZsr0S9zaKObluErpCvCwQmWcC3ejV0wgVjEdLRsT7CwLqowPgMzT4UiRBtfHPE3G/exec"; // FILL IN YOUR GOOGLE WEB APP ADDRESS
var ssId = "1PMrEftiZOdZkSH0oUIq8qUNHLHhTj7Kl-GfUkculluU"; // FILL IN THE ID OF YOUR SPREADSHEET

function getMe() {
  var url = telegramUrl + "/getMe";
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function setWebhook() {
  var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function sendText(id,text) {
  var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + encodeURIComponent(text);
  Logger.log("Sending message to URL: " + url); // Additional log for debugging
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}


function doGet(e) {
  return HtmlService.createHtmlOutput("Hi there");
}

function doPost(e) {

  Logger.log("Received doPost request");
  Logger.log(JSON.stringify(e));

  if (!e || !e.postData) {
    Logger.log("No postData found in the request");
    return HtmlService.createHtmlOutput("Request data is missing");
  }

  // this is where telegram works
  var data = JSON.parse(e.postData.contents);
  var text = data.message.text;
  var id = data.message.chat.id;
  var name = data.message.chat.first_name + " " + data.message.chat.last_name;
  var answer = "Hi " + name + ", thank you for your comment " + text;
  var specificChatId = '6387192318'; // Replace with the specific chat ID
  if(id === -1001994285933) {
    // This message is from the specific group, so handle accordingly
    var responseText = "Response to the group message.";
    sendText(id, responseText);
  sendText(specificChatId, "Hello, this is a message.");
  sendText(id,answer);
  SpreadsheetApp.openById(ssId).getSheets()[0].appendRow([new Date(),id,name,text,answer]);
  
  if(/^@/.test(text)) {
    var sheetName = text.slice(1).split(" ")[0];
    var sheet = SpreadsheetApp.openById(ssId).getSheetByName(sheetName) ? SpreadsheetApp.openById(ssId).getSheetByName(sheetName) : SpreadsheetApp.openById(ssId).insertSheet(sheetName);
    var comment = text.split(" ").slice(1).join(" ");
    sheet.appendRow([new Date(),id,name,comment,answer]);
  }
}

}

function deleteWebhook() {
  var telegramBotToken = 'XXXXXX';
  var baseUrl = 'https://api.telegram.org/bot' + telegramBotToken;
  var deleteWebhookUrl = baseUrl + '/deleteWebhook';

  var options = {
    'method': 'get',
    'muteHttpExceptions': true
  };

  var response = UrlFetchApp.fetch(deleteWebhookUrl, options);

  if (response.getResponseCode() !== 200) {
    Logger.log("Unable to connect");
    Logger.log("Telegram error " + response.getResponseCode() + ", " + response.getContentText());
  } else {
    Logger.log("Telegram webhook deleted");
    var jsonResponse = JSON.parse(response.getContentText());
    Logger.log(jsonResponse);
  }
}

function getWebhookInfo() {
  var url = telegramUrl + "/getWebhookInfo";
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}
google-apps-script google-sheets 电报机器人

评论

0赞 TheMaster 11/18/2023
如果没有 event 对象,则无法直接从脚本编辑器运行脚本。e
0赞 Deka Halane 11/20/2023
@TheMaster我明白了,但是您的意思是在哪个函数参数上缺少事件对象?
1赞 TheMaster 11/20/2023
我不认为你这样做。查看 stackoverflow.com/a/63851123
0赞 Deka Halane 11/21/2023
谢谢,但我没有发现它有帮助。你能帮我解决这个问题吗?
0赞 TheMaster 11/21/2023
everytime I run doPost on Google Apps Script, I get no response.你的意思是你正在点击脚本编辑器中的“运行”按钮。右?如果没有来自外部的事件,你如何期望一个“事件对象”?

答: 暂无答案