I am currently using app script to make a telegram bot that replies and reads the messages. I created a menu with buttons, but the following buttons only work to read the boxes of the google sheet. Instead, I would like the buttons to work only to change the sheet, and after I changed the sheet, the possibility to do the search by text returns.
Thank you
function setWebhook() {
var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
var response = UrlFetchApp.fetch(url);
}
function sendMessage(chat_id, text) {
var url = telegramUrl + "/sendMessage?chat_id=" + chat_id + "&text=" + text;
var response = UrlFetchApp.fetch(url);
}
function sendKeyboard(chatId, text, keyboard) {
var data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(chatId),
text: text,
parse_mode: "HTML",
reply_markup: JSON.stringify(keyboard)
}
};
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var keyboard = {
"inline_keyboard": [
[{
"text": "Button1",
"callback_data": "Button1"
}],
[{
"text": "Button2",
"callback_data": "Button2"
}],
[{
"text": "Button3",
"callback_data": "Button3"
}],
[{
"text": "Button4",
"callback_data": "Button4"
}],
[{
"text": "Button5",
"callback_data": "Button5"
}],
[{
"text": "Button6",
"callback_data": "Button6"
}]
]
}
if (contents.callback_query) {
var chat_id = contents.callback_query.from.id;
var user = contents.callback_query.message.chat.first_name + " " + contents.callback_query.message.chat.last_name;
var cb_data = contents.callback_query.data;
var ssId = "SPREADSHEET_ID";
var sh2 = SpreadsheetApp.openById(ssId).getSheetByName("Foglio2");
var sh2range = sh2.getRange(1, 1, sh2.getLastRow(), sh2.getLastColumn());
var sh2values = sh2range.getValues();
var sh2values_len = sh2values.length;
var cocktail_nome;
for (i = 0; i < sh2values_len; i++) {
cocktail_nome = sh2values[i][0] + '';
if (cb_data == cocktail_nome.toLowerCase()) {
answer = "Ingredienti: " + sh2values[i][1] + '.' + '%0A' + "Preparazione: " + sh2values[i][2];
break;
}
}
sendMessage(chat_id, answer);
SpreadsheetApp.openById(ssId).getSheetByName("Foglio1").appendRow([new Date(), chat_id, user, cb_data]);
} else if (contents.message) {
var chat_id = contents.message.from.id;
var user = contents.message.chat.first_name + " " + contents.message.chat.last_name;
var answer = "Ciao " + user + ". Seleziona il nome di un cocktail e ti dirĂ² come prepararlo!";
sendKeyboard(chat_id, answer, keyboard);
}
}