3

Since Telegram is an infinite cloud service, I use it as a secondary backup location. I would like to automate this so that I don't have to remember to do it every month (in the last two years I missed about every second monthly backup).
But I can't find a way to automate any Telegram client without click macros (I would like to have it run silently in the background):

  • ./Telegram --help does nothing except opening the program.
  • I wasn't successful in determining where the argv variable goes from the main method, so I don't know if there even are console arguments.
  • Bots can't access my file system (and that's a good thing).

I can easily automate packing the files I want into 1.5GB archives using 7z, but I have no idea about how to actually send them. Is there a way to do it without click macros?

I'm on Debian 9.5 and using the official Linux client from telegram.org.

5
  • Can't you directly use the libmtproto to write a storage backend? Oct 18, 2018 at 12:01
  • I don't know, since I don't know that program. Also, I would have to know details about how to send packages to the Telegram servers. At some point I just have a custom client. If you know how to make a minimalistic program for that or whatever your idea was, that would be awesome. Oct 18, 2018 at 12:06
  • I don't know the details, but it seems that TDlib is what you want. Oct 18, 2018 at 12:14
  • It seems like there is a command line client for Telegram and it can send files, even though it's not in the documentation: github.com/vysheng/tg/issues/458 I'll try that this evening and if it works, write an answer. Nov 8, 2018 at 10:23
  • Update: The command line arguments are now officially documented and indeed, there is no way to do it with the official client. May 10, 2020 at 14:27

3 Answers 3

2

I found a solution! The official client probably doesn't allow it (all known command line arguments are unofficially documented here), but for Linux and Mac (and probably also Linux subsystem on Windows), there is an unofficial command line interface for Telegram called "tg": https://github.com/vysheng/tg
It doesn't mention in its description that it can also send files, but in a Github issue someone wrote that there is the send_file function. In general it's not too well documented, but calling it with --help and entering help inside the program gives a lot of info, the rest can be figured out by fiddling with the parameters and trying different things.
This is the solution I came up with that way:

Preparation

  • Install tg like the readme file says. It didn't work rightaway for me on Debian due to a bug, but as said in this bug report comment, installing libssl1.0-dev is a workaround for that bug: Execute sudo apt install libssl1.0-dev between ./configure and make.
  • Log in with your Telegram account: bin/telegram-cli to start the interactive program, enter phone number (with country code, so in my case starting with +49), enter login code.
  • Now use another Telegram client to send a message in the target chat (or alternatively receive a message there). That adds the chat's name to the program's internal chat list. If multiple chats with the same name exist, it seems to pick the most recently added one. I have not found a way to message a chat by ID.
    That's it for the setup, quit exits the program.

Uploading files programmatically

Now files can be uploaded by giving a send_file command to the program with --execute, which makes it execute that command and then quit automatically:

/path/to/tg/bin/telegram-cli --exec 'send_file <chat> <filename>'

<chat> is the chat's name.
<filename> is the path to the file.
/path/to/tg should be obvious.

Example usage

I'll use something like this to upload a backup of my Google Drive folder to Telegram after I've created it with p7zip:

for filename in ~/drive_backup/drive.zip.*; do ~/tg/bin/telegram-cli --exec 'send_file Backup_chat '"$filename"; done

This uploads all files whose names start with "drive.zip." (.001, .002 etc.) in the folder ~/drive_backup to the Telegram chat "Backup_chat".

3
  • 2
    The message in telegram is "Saved Messages" to my own chat. When i run "bin/telegram-cli --exec 'send_file "Saved Messages" config.log' nothing happens. Is there a way we can name this as Backup_chat.
    – Suresh
    Jul 11, 2020 at 14:20
  • @Suresh You need your own username, without quotes. So in my case it would be msg @Fabian42 "test" or msg @Fabian42 test. Sadly it gives you no feedback if you have the wrong username or one for which the program hasn't seen a message come in or out yet. Jul 11, 2020 at 17:59
  • That is already the preparation step #3 in my answer. Sep 7, 2021 at 19:57
0

You can upload encrypted backups on Telegram cloud with PGPgram. It uses gpg for encryption and lets you upload files of any size without manually splitting them; from version 0.2 it even lets you backup YouTube videos and channels. Of course it has database file import/export feature.

9
  • I tested a bunch of stuff and it seems to work well. Of course the downside is that I would need to install it everywhere for it to work, but I would usually only use it on my laptop anyway. May 11, 2020 at 8:10
  • 1
    Actually, I just now tried restoring files on another system than it has been uploaded to . That doesn't work at all. That makes it useless as a backup system, because if my system is broken and I re-install it, I can't access my files anymore. May 11, 2020 at 8:27
  • What do you mean with "Of course it has database file import/export feature."? There is no such command in --help or in the Github description. May 11, 2020 at 13:04
  • You can export backed up files list and credentials either directly copying the directory at ~/.config/pgpgram to another user's home (be sure to have correct ownership for those file) or you can merge files backed up from another computer into current installation copying ~/.config/pgpgram/files.pkl over and then giving pgpgram --import files.pkl (if you installed from master that won't probably work atm), it's in --help since 23 march commit. Master is not on PyPI because it saw introduced great performance features (when over 5.000 files) which broke search and import though. May 12, 2020 at 11:58
  • Usually I do backups copying the directory on a flash drive and import files backed up from other devices to the main machine using --import. May 12, 2020 at 12:17
-2

Try

telegram-desktop -sendpath <fullpath>

5
  • How would that select a chat? Jan 15, 2019 at 7:24
  • There seems to be such a command actually. But there is no documentation for it. If you know more, please comment here: github.com/telegramdesktop/tdesktop/issues/2058 But I'm pretty sure that just a path is not enough, how would it know what chat to send it to? I could actually try just putting a chat ID before or after the path, but that's just a random guess. Jan 15, 2019 at 7:26
  • I have it running. You ask for help, get the proper and right answer and vote -1? O_o Don't count on my help anymore
    – Juan
    Jan 15, 2019 at 12:31
  • 1
    Well, how do you send to a specific chat? It can't just send it to a random chat. Do I need an ID, a name or something else? Where do I put that argument? Jan 15, 2019 at 14:25
  • 3
    I tried it now. It's interactive, I'm supposed to click on a chat. That's not automatic and can't happen in the background. So this post does indeed not answer my queston. Jan 15, 2019 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .