/** * */ function getMessages() { var botId = 'TELEGRAM_BOT_API_KEY'; var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheets = ss.getSheets(); var offset = sheets[0].setActiveSelection('B1').setNumberFormat('@STRING@').getValue(); var updates = UrlFetchApp.fetch("https://api.telegram.org/bot"+botId+"/getUpdates?offset="+ offset + '&limit=40&timeout=1'); var transferLanguage = 'bg'; // var sourceLanguage = 'ru'; // updates = JSON.parse(updates); Logger.log(updates); if (updates.ok) { for (var i in updates.result) { var update = updates.result[i]; offset = update.update_id + 1; sheets[0].setActiveSelection('B1').setValue(offset); sheets[0].setActiveSelection('B2').setValue(new Date(update.message.date * 1000)); // var text = update.message.text; if (text.length >= 4096) { text = ' , '; } else { if (text == '/start') { text = ' , . .'; } else { Utilities.sleep(1000); text = LanguageApp.translate(LanguageApp.translate(text, sourceLanguage, transferLanguage), transferLanguage, sourceLanguage); } writeLog({ date: new Date(update.message.date * 1000), inMessage: update.message.text, outMessage: text, userName: update.message.from.first_name + ' ' + update.message.from.last_name, userId: update.message.from.id, userUsername: update.message.from.username }); } UrlFetchApp.fetch("https://api.telegram.org/bot"+botId+"/sendMessage", { 'method' : 'post', 'payload' : { chat_id: update.message.chat.id, text: text } }); } } } /** * "" */ function writeLog(data) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[1]; var lastRow = sheet.getLastRow() + 1; sheet.setActiveSelection('A' + lastRow).setNumberFormat('@STRING@').setValue(data.date); sheet.setActiveSelection('B' + lastRow).setNumberFormat('@STRING@').setValue(data.userUsername); sheet.setActiveSelection('C' + lastRow).setNumberFormat('@STRING@').setValue(data.userId); sheet.setActiveSelection('D' + lastRow).setNumberFormat('@STRING@').setValue(data.userName); sheet.setActiveSelection('E' + lastRow).setNumberFormat('@STRING@').setValue(data.inMessage); sheet.setActiveSelection('F' + lastRow).setNumberFormat('@STRING@').setValue(data.outMessage); }
Source: https://habr.com/ru/post/326220/
All Articles