📜 ⬆️ ⬇️

Pampering Writing Telegram bot on Google script

Goals, objectives, excuses for madness.


It all started with the fact that someone from the habravchan in the comments told about using Google Translate as a synonymizer. He proposed to translate the text from one language to another and vice versa, and promised a fairly good percentage of differences and the preservation of the meaning of the text. But it is not exactly.

Questions I have decided to get the answer "yes"



How it all works


First, I created a bot and got the API key. I will not write about how to do this, this information has been published many times on Habré and is easily googled.

It quickly became clear that I could send an http request to the Telegram server. About any web hook I did not think. It is possible that it can be used through google sites, but I did not check this idea at all.
')
Then I created a tabular document with two sheets. The first sheet contains the offset parameter, which is required to receive messages to the bot that have not yet been received. On the same sheet on the second line is recorded the date of the last update. On the second sheet is the log of incoming messages with the result of the translation into Bulgarian and vice versa.

The Bulgarian language was chosen because it is close to Russian, but, unfortunately, this did not save from translating unfamiliar words into translit.

Through the project triggers in the script, I set the execution of the function to get data every minute.

The function receives the latest messages to the bot using the id of the last message +1. Further, all messages are run through the translate and sent back.


Disadvantages and unpleasant features


Naturally, the chosen "tools" of the bot implementation impose a lot of restrictions. First, the bot will respond to messages with a long delay (up to a minute). Secondly, despite the fact that Google Translate is used inside Google Script, there are still limitations on the number of requests per second and their total number per day. For obvious reasons, the possibility of using a web hook is missing.

Talk about the quality of the work of Google Translate as a synonymizer does not have to. This method does not fit. Perhaps it makes sense to break the text into sentences, and sentences into words.

/** *        */ 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); } 

What is the result?


My experiment showed that you can easily connect Google services with the Telegram bot for personal use, automate the collection of information and receive it very quickly. You can easily write any avito parser and send new announcements to the telegrams on the request of interest. In this case, even the speed of the bot's response to messages does not play any significant role.

Synonymizer, of course, turned out so-so.

Here is a log can be observed in the document:



UPD: Pylorama in his comments told about how to make a WebHook.

Source: https://habr.com/ru/post/326220/


All Articles