Подскажите пожалуйста, как заставить бот получать результаты опроса и записывать их в таблицу? И как выдавать сообщение пользователю (answerCallback) о том что ответ принят?
let token = "5954818159:AAHQeP.........v8gkHf5XsLw";
let wUrl = "https://script.google.com/macros/s/AKfycbwRRXkfrEasps.........ALoXtn2tU9R4K9VRvrvxQExH4rFmPi7BsTO/exec";
function getMe() {
let response = UrlFetchApp.fetch("https://api.telegram.org/bot" + token + "/getMe");
console.log(response.getContentText());
}
function setWebhook() {
let webAppUrl = wUrl;
let response = UrlFetchApp.fetch("https://api.telegram.org/bot" + token + "/setWebhook?url=" + webAppUrl);
console.log(response.getContentText());
}
function getWebhookInfo() {
let response = UrlFetchApp.fetch("https://api.telegram.org/bot" + token + "/getWebhookInfo");
console.log(response.getContentText());
}
function sendText(chat_id, text, keyBoard) {
let data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(chat_id),
text: text,
parse_mode: "HTML",
reply_markup: JSON.stringify(keyBoard)
}
};
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
function acq(chat_id, text) {
let data = {
method: "post",
payload: {
method: "answerCallbackQuery",
chat_id: String(chat_id),
text: text,
show_alert: true,
parse_mode: "HTML"
}
};
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
function sendPoll(chat_id, question, answers) {
let data = {
method: "post",
muteHttpExceptions: true,
payload: {
method: "sendPoll",
chat_id: String(chat_id),
question: String(question),
parse_mode: "HTML",
options: JSON.stringify(answers),
is_closed: false,
is_anonymous: false,
type: "regular",
allows_multiply_answers: false,
open_period: 60,
protect_content: true}
}
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
Logger.log(data);
};
function doPost(e) {
const now = new Date();
let ssId = "1xJWCR2iSwB3Sn4........KB8wKIOwatmE75l0f4";
let ss = SpreadsheetApp.openById(ssId).getSheetByName("qa");
let q1 = ss.getRange("A1").getValue();
let q1a1 = ss.getRange("B1").getValue();
let q1a2 = ss.getRange("C1").getValue();
let q1a3 = ss.getRange("D1").getValue();
let q1a4 = ss.getRange("E1").getValue();
let answers = [''+q1a1+'',''+q1a2+'',''+q1a3+'',''+q1a4+''];
let data = JSON.parse(e.postData.contents);
let chat_id = data.message.chat.id;
let username = data.message.chat.username;
let text = data.message.text;
let poll_answer = data.poll_answer.option_ids;
if (text == "/start")
sendPoll(chat_id, q1, answers)
acq(chat_id,"Ответ принят");
SpreadsheetApp.openById(ssId).getSheetByName("Messages").appendRow([now, chat_id, username, poll_answer]);
SpreadsheetApp.openById(ssId).getSheetByName("debug").getRange(1, 1).setValue(JSON.stringify(data, null, 10));
}
Откройте по ссылке или QR бот @iMakeBot, нажмите кнопку Старт/Start.
Следуйте инструкциям бота.