Google 网络应用 JavaScript _ Telegram 机器人状态机
Google web app JavaScript _ Telegram bot state machine
我正在开发一个 Google 网络应用 JavaScript 应用程序来与 Telegram 机器人通信。
Telegram 用户通过使用 InlineKeyboardMarkup (InlineKeyboardButton) 输入某些词或点击出现在 Telegram 组中的按钮;
我已经实现了一个 Telegram Bot 状态机来检测输入了哪些命令,以确定要执行的相应操作;
问题:有没有更高效的方法满足上面的要求?
我正在使用的 Google 网络应用 JavaScript 源代码示例:
function menu( chat_id ) {
let url = vUrlTelegram + "/sendMessage";
var keyboard = {
'inline_keyboard' :
[
[{'text' : 'admin', 'callback_data' : 'admin'}], // Row 1
[{'text' : 'squad', 'callback_data' : 'squad'}], // Row 2
[{'text' : 'carioca', 'callback_data' : 'carioca'}], // Row 3
[{'text' : 'brasileiro', 'callback_data' : 'brasileiro'}], // Row 4
[{'text' : 'sponsors', 'callback_data' : 'sponsors'}], // Row 5
[{'text' : 'test', 'callback_data' : 'test'}] // Row 6
]
};
var data = {
'chat_id': chat_id,
'text': "main menu",
'reply_markup': keyboard
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
function telegramBotMachine( chat_id, text, name, data ) {
var sUpperData = data.toUpperCase();
var sResponse = " ";
// var sContent = "chat_id: " + chat_id + " \ntext: " + text + " \nname: " + name + " \ndata: " + data + " \nsUpperData: " + sUpperData;
// sendFormattedMessage( chat_id, sContent, "HTML", "false", "false" );
if ( sUpperData.substr( 0, 4 ) === "MENU" ) {
menu( chat_id );
} else if ( sUpperData.substr( 0, 7 ) === "CARIOCA" && sUpperData.length === 7 ) {
cariocaSubMenu( chat_id );
} else if ( sUpperData.substr( 0, 13 ) === "CARIOCA JOGOS" && sUpperData.length > 7 ) {
cariocaLeagueGameReport( chat_id );
} else if ( sUpperData.substr( 0, 14 ) === "CARIOCA TABELA" && sUpperData.length > 7 ) {
cariocaLeagueTableReport( chat_id );
} else if ( sUpperData.substr( 0, 10 ) === "BRASILEIRO" && sUpperData.length === 10 ) {
brasileiroSubMenu( chat_id );
} else if ( sUpperData.substr( 0, 16 ) === "BRASILEIRO JOGOS" && sUpperData.length > 10 ) {
brasileiroLeagueGameReport( chat_id );
} else if ( sUpperData.substr( 0, 17 ) === "BRASILEIRO TABELA" && sUpperData.length > 10 ) {
brasileiroLeagueTableReport( chat_id );
} else if ( sUpperData.substr( 0, 5 ) === "ADMIN" ) {
adminReport( chat_id )
} else if ( sUpperData.substr( 0, 5 ) === "SQUAD" ) {
squadReport( chat_id );
} else if ( sUpperData.substr( 0, 8 ) === "SPONSORS" ) {
sponsorsReport( chat_id );
} else if ( sUpperData.substr( 0, 4 ) === "TEST" ) {
sendFormattedMessage( chat_id, "Test", "HTML", "false", "false" );
}
}
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var chat_id = "";
var text = "";
var name = "";
var data = "";
var sResponse = "";
// GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Telegram Bot Update", JSON.stringify(contents, null, 4));
if ( typeof( contents.message ) != "undefined" ) {
// block of code to be executed if condition1 is true
chat_id = contents.message.chat.id;
text = contents.message.text;
name = contents.message.from.first_name + " " + contents.message.from.last_name;
data = text;
sResponse = telegramBotMachine( chat_id, text, name, data );
} else if ( typeof( contents.callback_query ) != "undefined" ) {
// block of code to be executed if the condition1 is false and condition2 is true
chat_id = contents.callback_query.message.chat.id;
text = contents.callback_query.message.text;
name = contents.callback_query.message.from.first_name;
data = contents.callback_query.data;
sResponse = telegramBotMachine( chat_id, text, name, data );
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
}
不确定我是否得到了您想要实现的目标,但看起来您可以使用来自 inline_keyboard 的回调数据来跟踪用户单击按钮后的操作。
your_bot.on('callback_query', (callbackData) => {
if (callbackData.data === 'value1') {
// some action
}
else if (callbackData.data === 'value2') {
// some action
}
}
用户点击按钮后,您将获得 'callback_query' 并使用方法 'on' 处理事件 'callback_query'。然后,您从 inline_keyboard 的 callback_data 中获取数据,并考虑根据您收到的数据采取的行动。这里是 info about callbackQuery object.
question: is there a more efficient approach to meet above?
有! Telegram Inlinekeyboardbutton 提供了 callback_data
这样您就可以轻松区分回调!
不使用回调文本,而是降低字符并检查按钮值
var text = contents.message.text;
var name = contents.message.from.first_name + " " + contents.message.from.last_name;
var sResponse = telegramBotMachine( chat_id, text, name );
if ( sUpperText.substr( 0, 4 ) === "MENU" ) {
menu( chat_id );
} else if
....
您应该使用已经在 keyboard
函数中提供的 callback_data
,但只是没有使用!像这样;
function telegramBotMachine( chat_id, callbackData, name ) {
switch (callbackData) {
case 'menu':
menu( chat_id );
break;
case 'admin':
adminReport( chat_id );
break;
case 'squad':
squadReport( chat_id );
break;
// ...
default:
sendFormattedMessage( chat_id, "Test", "HTML", "false", "false" );
break;
}
}
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var chat_id = contents.message.chat.id;
// Check for callback_query
if (typeof contents.callback_query !== 'undefined') {
var callbackData = contents.callback_query.data; // admin, squad, ...
var name = contents.message.from.first_name + " " + contents.message.from.last_name;
var sResponse = telegramBotMachine( chat_id, callbackData, name );
}
}
使用 switch-case 提高可读性!
我正在开发一个 Google 网络应用 JavaScript 应用程序来与 Telegram 机器人通信。
Telegram 用户通过使用 InlineKeyboardMarkup (InlineKeyboardButton) 输入某些词或点击出现在 Telegram 组中的按钮;
我已经实现了一个 Telegram Bot 状态机来检测输入了哪些命令,以确定要执行的相应操作;
问题:有没有更高效的方法满足上面的要求?
我正在使用的 Google 网络应用 JavaScript 源代码示例:
function menu( chat_id ) {
let url = vUrlTelegram + "/sendMessage";
var keyboard = {
'inline_keyboard' :
[
[{'text' : 'admin', 'callback_data' : 'admin'}], // Row 1
[{'text' : 'squad', 'callback_data' : 'squad'}], // Row 2
[{'text' : 'carioca', 'callback_data' : 'carioca'}], // Row 3
[{'text' : 'brasileiro', 'callback_data' : 'brasileiro'}], // Row 4
[{'text' : 'sponsors', 'callback_data' : 'sponsors'}], // Row 5
[{'text' : 'test', 'callback_data' : 'test'}] // Row 6
]
};
var data = {
'chat_id': chat_id,
'text': "main menu",
'reply_markup': keyboard
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
function telegramBotMachine( chat_id, text, name, data ) {
var sUpperData = data.toUpperCase();
var sResponse = " ";
// var sContent = "chat_id: " + chat_id + " \ntext: " + text + " \nname: " + name + " \ndata: " + data + " \nsUpperData: " + sUpperData;
// sendFormattedMessage( chat_id, sContent, "HTML", "false", "false" );
if ( sUpperData.substr( 0, 4 ) === "MENU" ) {
menu( chat_id );
} else if ( sUpperData.substr( 0, 7 ) === "CARIOCA" && sUpperData.length === 7 ) {
cariocaSubMenu( chat_id );
} else if ( sUpperData.substr( 0, 13 ) === "CARIOCA JOGOS" && sUpperData.length > 7 ) {
cariocaLeagueGameReport( chat_id );
} else if ( sUpperData.substr( 0, 14 ) === "CARIOCA TABELA" && sUpperData.length > 7 ) {
cariocaLeagueTableReport( chat_id );
} else if ( sUpperData.substr( 0, 10 ) === "BRASILEIRO" && sUpperData.length === 10 ) {
brasileiroSubMenu( chat_id );
} else if ( sUpperData.substr( 0, 16 ) === "BRASILEIRO JOGOS" && sUpperData.length > 10 ) {
brasileiroLeagueGameReport( chat_id );
} else if ( sUpperData.substr( 0, 17 ) === "BRASILEIRO TABELA" && sUpperData.length > 10 ) {
brasileiroLeagueTableReport( chat_id );
} else if ( sUpperData.substr( 0, 5 ) === "ADMIN" ) {
adminReport( chat_id )
} else if ( sUpperData.substr( 0, 5 ) === "SQUAD" ) {
squadReport( chat_id );
} else if ( sUpperData.substr( 0, 8 ) === "SPONSORS" ) {
sponsorsReport( chat_id );
} else if ( sUpperData.substr( 0, 4 ) === "TEST" ) {
sendFormattedMessage( chat_id, "Test", "HTML", "false", "false" );
}
}
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var chat_id = "";
var text = "";
var name = "";
var data = "";
var sResponse = "";
// GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Telegram Bot Update", JSON.stringify(contents, null, 4));
if ( typeof( contents.message ) != "undefined" ) {
// block of code to be executed if condition1 is true
chat_id = contents.message.chat.id;
text = contents.message.text;
name = contents.message.from.first_name + " " + contents.message.from.last_name;
data = text;
sResponse = telegramBotMachine( chat_id, text, name, data );
} else if ( typeof( contents.callback_query ) != "undefined" ) {
// block of code to be executed if the condition1 is false and condition2 is true
chat_id = contents.callback_query.message.chat.id;
text = contents.callback_query.message.text;
name = contents.callback_query.message.from.first_name;
data = contents.callback_query.data;
sResponse = telegramBotMachine( chat_id, text, name, data );
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
}
不确定我是否得到了您想要实现的目标,但看起来您可以使用来自 inline_keyboard 的回调数据来跟踪用户单击按钮后的操作。
your_bot.on('callback_query', (callbackData) => {
if (callbackData.data === 'value1') {
// some action
}
else if (callbackData.data === 'value2') {
// some action
}
}
用户点击按钮后,您将获得 'callback_query' 并使用方法 'on' 处理事件 'callback_query'。然后,您从 inline_keyboard 的 callback_data 中获取数据,并考虑根据您收到的数据采取的行动。这里是 info about callbackQuery object.
question: is there a more efficient approach to meet above?
有! Telegram Inlinekeyboardbutton 提供了 callback_data
这样您就可以轻松区分回调!
不使用回调文本,而是降低字符并检查按钮值
var text = contents.message.text;
var name = contents.message.from.first_name + " " + contents.message.from.last_name;
var sResponse = telegramBotMachine( chat_id, text, name );
if ( sUpperText.substr( 0, 4 ) === "MENU" ) {
menu( chat_id );
} else if
....
您应该使用已经在 keyboard
函数中提供的 callback_data
,但只是没有使用!像这样;
function telegramBotMachine( chat_id, callbackData, name ) {
switch (callbackData) {
case 'menu':
menu( chat_id );
break;
case 'admin':
adminReport( chat_id );
break;
case 'squad':
squadReport( chat_id );
break;
// ...
default:
sendFormattedMessage( chat_id, "Test", "HTML", "false", "false" );
break;
}
}
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var chat_id = contents.message.chat.id;
// Check for callback_query
if (typeof contents.callback_query !== 'undefined') {
var callbackData = contents.callback_query.data; // admin, squad, ...
var name = contents.message.from.first_name + " " + contents.message.from.last_name;
var sResponse = telegramBotMachine( chat_id, callbackData, name );
}
}
使用 switch-case 提高可读性!