How to store bot data directly in Telegram


Formulation of the problem

As you know, a bot is a program on a computer that interacts with https://api.telegram.org and pretends to be human. Of course she has data in her own database or something. If this computer suddenly disappears, then I would like true friends in another place and with another computer to “shove a USB flash drive” and launch the same bot from another place. And, of course, for this you need to transfer actual data from the old place to the new one. Of course, there are some time-tested expensive solutions for this, but all this can be organized directly through Telegram. And do not ask why you personally need it.

Files

Everybody knowsthat files sent to Telegram have a file_id, by which they can be uploaded back to the computer using getFile. So, we make regular copies of the data to a file, send these files anywhere, and at the decisive moment, our faithful friends will launch a new instance of the bot, it will make a getFile and continue working with the old data. The problem is that this same file_id is different for different files, and it must be somehow transferred to a new instance of the bot.

First way. Through messages

Command sendMessage not only sends a message. It also returns the number of this message. And by a couple of numbers (chat Id, message number), this very message can be changed regularly (for example, by writing the file_id of the data backup copy there) by means of editMessageTextand then read from the new bot instance with forwardMessage (yes, this command also returns the text of the transmitted message). So, this pair (Chat Id, message number) does not change, which means that you can first start the bot, it will make messages (in some private channel) to store the backup file_id and show a couple of numbers (Chat Id, number messages). Then you should enter this pair of numbers into the “bot on a flash drive” and distribute it to your true friends. This is a completely working method, but this one: “first run then change the bot code” is somewhat inconvenient. But there is also

The second way. Through the menu

As you know, the bot has a menu. Once upon a time it could only be done through @BotFather, but then it appeared setMyCommandsto set the menu directly from the bot, and getMyCommandsto read what was installed in this very menu. So: the bot makes a backup copy and writes the file_id of this copy to some private channel. Then he finds out a couple of numbers (chat ID, message number) and writes them in the menu as an explanation for the command.

When starting the bot from another computer, the bot will first do getMyCommands, find out the same pair of numbers, do forvardMessage, find out the file_id, download the data, and as if nothing had happened.

It is clear that this pair of numbers will hang in front of the eyes of all users and cause unnecessary questions, but in June 2021, setMyCommand got the language_code field and the ability to make different menus for different users with different languages ​​in the system settings. It has been experimentally established that non-existent languages ​​of the “xy” type can be specified as a language code, which means that users will not see anything extra and will not ask questions.

Furthermore. You can organize autorun of spare bots and do without true friends. So: The main bot is running in working mode and, among other things, regularly writes something changing in the menu or in a message with a known number. For example, current time. Spare bots (launched in other places) are in sleep mode, they do nothing but regularly (but after random time intervals) look at this “current time”. If they notice that it has not been updated for a long time, then the first bot to find it will start in production mode. And as if nothing had happened…

And don’t ask why you might need it personally.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *