使用Google App脚本和Webhooks开发电报机器人。
我正在尝试编辑通过ReplyKeyboardMarkup发送的editMessageReplyMarkup消息的键盘按钮。
在此问题底部的代码中,您可以看到我如何使用功能sendKeyboardMessage()
(正在工作)以及如何尝试使用editKeyboardMessage()
( 不起作用)。
如果我尝试运行editKeyboardMessage()
,则会得到类似以下内容:
Request error 400. Server response {"ok":false,"error_code":400,"description":"Bad Request: can't parse inline keyboard button: Text buttons are unallowed in the inline keyboard"} (use muteHttpExceptions to examine complete response)
但是,如果我尝试这样记录请求URL:Logger.log(url + "/" + JSON.stringify(data))
我得到一个URL,如果我访问它,则会出现此错误:
{
ok: false,
error_code: 404,
description: "Not Found: method not found"
}
这是记录的URL,我可以访问以获取该错误:
https://api.telegram.org/bot + TOKEN + /{"method":"post","payload":{"method":"editMessageReplyMarkup","chat_id":"CHATID","message_id":"MESSAGEID","parse_mode":"HTML","reply_markup":"{\"inline_keyboard\":[[{\"text\":\"Yes\"}],[{\"text\":\"No\"}],[{\"text\":\"Maybe\"}]]}"}}
这是以下代码中使用的buttonArray
的格式:
已发送消息:[[{"text":"Yes"}],[{"text":"No"}]]
尝试编辑为:
[[{"text":"Yes"}],[{"text":"No"}],[{"text":"Maybe"}]]
以下是功能:
var url = "https://api.telegram.org/bot" + token;
function sendKeyboardMessage(id, buttonArray, text) {
var keyboardMarkup = {"keyboard": buttonArray ,"resize_keyboard": true};
var data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(id),
text: text,
parse_mode: "HTML",
reply_markup: JSON.stringify(keyboardMarkup)
}
};
var response = UrlFetchApp.fetch(url + "/", data);
}
function editKeyboardMessage(id, message_id, buttonArray) {
var keyboardMarkup = {"inline_keyboard": buttonArray};
var data = {
method: "post",
payload: {
method: "editMessageReplyMarkup",
chat_id: String(id),
message_id: String(message_id),
parse_mode: "HTML",
reply_markup: JSON.stringify(keyboardMarkup)
}
};
var response = UrlFetchApp.fetch(url + "/", data)
}
问题:
谢谢!