Google 应用脚本 - Telegram 机器人 - 自定义键盘
Google App Script - Telegram Bot - Custom Keyboard
我想创建自定义键盘。
我有这个 GAS 代码:
function sendText(chatId,text){
var payload = { "method": "sendMessage", "chat_id": String(chatId),"text": text, "parse_mode": "HTML" }
var data = {
"method": "post",
"payload": payload,
"reply_markup": JSON.stringify({
"keyboard": [
[
"A",
"B"
],
[
"C",
"D"
]
],
"resize_keyboard":true
})
}
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
它作为 echo bot 效果很好,但我无法创建自定义键盘。它只是不起作用,我不知道为什么。我在网上搜索了解决方案,但一无所获。请帮助我:)
您对 keyboard
字段使用了错误的格式,请参见以下示例:
在@Sean 和@Kos 的帮助下,我已经解决了我的问题,这是工作代码。我还添加了 inline_keyboard 类型。
function sendText(chatId,text,keyBoard){
keyBoard = keyBoard || 0;
if(keyBoard.inline_keyboard || keyBoard.keyboard){
var data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(chatId),
text: text,
parse_mode: "HTML",
reply_markup: JSON.stringify(keyBoard)
}
}
}else{
var data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(chatId),
text: text,
parse_mode: "HTML"
}
}
}
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
键盘格式必须如下:
{
keyboard: [
[
"A",
"B"
],
[
"C",
"D"
]
],
resize_keyboard:true,
one_time_keyboard:true
}
{
inline_keyboard: [
[
{text:'Sandwich',callback_data:'sandwich'},
{text:'A juicy steak',callback_data:'steak'}
],
[
{text:'Sandwich2',callback_data:'sandwich2'},
{text:'A juicy steak2',callback_data:'steak2'}
]
]
}
请显示,您如何在应用程序脚本中使用 callback_data?
我想创建自定义键盘。
我有这个 GAS 代码:
function sendText(chatId,text){
var payload = { "method": "sendMessage", "chat_id": String(chatId),"text": text, "parse_mode": "HTML" }
var data = {
"method": "post",
"payload": payload,
"reply_markup": JSON.stringify({
"keyboard": [
[
"A",
"B"
],
[
"C",
"D"
]
],
"resize_keyboard":true
})
}
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
它作为 echo bot 效果很好,但我无法创建自定义键盘。它只是不起作用,我不知道为什么。我在网上搜索了解决方案,但一无所获。请帮助我:)
您对 keyboard
字段使用了错误的格式,请参见以下示例:
在@Sean 和@Kos 的帮助下,我已经解决了我的问题,这是工作代码。我还添加了 inline_keyboard 类型。
function sendText(chatId,text,keyBoard){
keyBoard = keyBoard || 0;
if(keyBoard.inline_keyboard || keyBoard.keyboard){
var data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(chatId),
text: text,
parse_mode: "HTML",
reply_markup: JSON.stringify(keyBoard)
}
}
}else{
var data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(chatId),
text: text,
parse_mode: "HTML"
}
}
}
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
键盘格式必须如下:
{
keyboard: [
[
"A",
"B"
],
[
"C",
"D"
]
],
resize_keyboard:true,
one_time_keyboard:true
}
{
inline_keyboard: [
[
{text:'Sandwich',callback_data:'sandwich'},
{text:'A juicy steak',callback_data:'steak'}
],
[
{text:'Sandwich2',callback_data:'sandwich2'},
{text:'A juicy steak2',callback_data:'steak2'}
]
]
}
请显示,您如何在应用程序脚本中使用 callback_data?