Javascript 如何等待下一个POST请求

Javascript 如何等待下一个POST请求,javascript,post,google-apps-script,http-post,telegram,Javascript,Post,Google Apps Script,Http Post,Telegram,所以我要做的是,当bot接收/上传photo消息时,它会等待下一条消息并检查它是否包含photo。我尝试过许多不同的循环,包括do..while、if、while和其他解决方案。现在我想我需要从doPost函数调用doPost函数。有办法吗 尝试执行..while、if、while和其他循环 function doPost(e){ var contents = JSON.parse(e.postData.contents); var id = contents.messag

所以我要做的是,当bot接收/上传photo消息时,它会等待下一条消息并检查它是否包含photo。我尝试过许多不同的循环,包括do..while、if、while和其他解决方案。现在我想我需要从doPost函数调用doPost函数。有办法吗

尝试执行..while、if、while和其他循环

function doPost(e){          

var contents = JSON.parse(e.postData.contents);
var id = contents.message.id;
var text = contents.message.text;

case "uploadPhoto":
sendText(id, "Upload a photo...");

        //endText(id, "Upload a photo to be displayed as your product%0AUsage: upload a photo with a caption /uploadPhoto");

        /**const zinutes_id = contents.message.message_id; 
        const zinutes_id_opa = zinutes_id + 1;
        while (true) {
          if (zinutes_id == zinutes_id_opa){
            if ("photo" in contents.message) {
              var photo = contents.message.photo[3].file_id;
              sendText(id, "okay " + photo);
              sendPhoto(id, photo);
              break;
            }
          }
        } */


       /**  if ("photo" in contents.message){
          var photo = contents.message.photo[3].file_id;
          sendText(id, "okay " + photo);
          sendPhoto(id, photo);
        } else {
          sendText(id, "Upload a photo!");
        }*/
        break;
}

  • 文本/上传照片到机器人
  • 机器人回复“上传照片…”
  • Bot等待下一条消息并解析其内容
  • 编辑:

    这就是我设法做到的:

    doPost(e){
      if(contents.message.includes("photo")){
        var fotke = contents.message.photo[3].file_id;
      }
    
    
      if (checkLastCommand(id) == "uploadPhoto") {
        if (fotke !== "undefined") {
         // @ts-ignore
         sheet.getRange(searchUser(id),6).setValue(fotke);
         sendText(id, fotke);
         // @ts-ignore
         sheet.getRange(searchUser(id),7).clear();
        } else {
         sendText(id, "this is not a photo");
         // @ts-ignore
         sheet.getRange(searchUser(id),7).clear();
      } else if (firstChar === '/') {
         case "uploadPhoto":
             sendText(id, "Upload a photo...");
             sheet.getRange(searchUser(id), 7).setValue("uploadPhoto");
             break;
      }
    
    
    }
    function checkLastCommand(x) {
      // @ts-ignore
      var z = sheet.getRange(searchUser(x), 7).getValue(); 
      return z;
    } 
    

    现在它等待照片,但由于某种原因,它在POST请求中找不到照片。我在单元格中得到“undefined”,机器人不响应photo,而是使用undefined响应/(命令),因此显然我想出了一个解决方案

    if ("photo" in contents.message) {
       var fotke = contents.message.photo[3].file_id;
    } else {
       var text = contents.message.text;
       var firstChar = text.substr(0,1);
    }
    

    程序不想读取已发送照片的文件id,因为它一直在等待文本消息。

    您是否检查了
    e
    的内容?使用
    doPost(e)
    ,然后使用
    检查信息,如果
    有效。在执行的
    doPost
    的任何给定实例中,只有一条消息。任何后续帖子都由
    doPost
    的一个完全独立的实例处理。。。。