【发布时间】:2019-07-16 21:27:04
【问题描述】:
所以我要做的是当机器人收到 /uploadPhoto 消息时,它会等待下一条消息并检查它是否包含照片。我尝试了许多不同的循环,包括 do..while、if、while 和其他解决方案。现在我认为我需要从 doPost 函数调用 doPost 函数。有什么办法吗?
试过do..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;
}
- 文本 /uploadPhoto 到机器人
- Bot 回复“上传照片...”
- 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 请求中找不到照片。我在单元格中得到“未定义”,机器人不响应照片,而是使用未定义的 /(command) 响应
【问题讨论】:
-
你查看
e的内容了吗?使用doPost(e),然后使用if检查信息应该可以工作。 -
在任何正在执行的
doPost实例中,只有一条消息。任何后续的 POST 都由一个完全独立的doPost.... 实例处理。
标签: javascript post google-apps-script http-post telegram