Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


transfer files between VPS and Telegram (Tutorial)
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

transfer files between VPS and Telegram (Tutorial)

farsighterfarsighter Member
edited October 2022 in Tutorials

Here I'll show you how to transfer files between VPS and your Telegram (without any new installation on VPS).

The popular Telegram messenger is supported in all OS, Android/Linux/Windows/Mac/IOS/ etc, and even in browsers through Telegram Web (https://web.telegram.org) so being able to quickly transfer files to/from Telegram will save time and headache, no need http server or port exposed.

This tutorial will results in:
1. A bash function name sft (which stands for 'send file to Telegram').
2. A bash alias name gft (which stands for 'get file from Telegram').

First, we'll quickly create an empty passive Telegram bot:
Go to the following address in the machine where you have Telegram installed:
https://t.me/botfather
or just search for 'botfather' in Telegram.

Inside botfather just type the command /newbot and follow the few instructions until you get a token from botfather.

A token looks something like this:
7657098866:HTfjCUuGgXjPygUFGGviviguHI
We'll refer to it here as YourBOTToken

Save your token somewhere.

Now we'll need to get Telegram account chat ID.
Enter your new telegram bot, start it and just type some text in it. Then go to your browser and visit:
https://api.telegram.org/bot<YourBOTToken>/getUpdates

Search for the object that looks like this: "chat":{"id":437965788 and save this number. This will be ChatID in the following examples.

Now everything is ready.

In order to send files from VPS to Telegram bot edit this function with your chat ID and bot token:
sft () { curl -F chat_id="<ChatID>" -F document=@"$1" -F caption="File from server" https://api.telegram.org/bot<YourBOTToken>/sendDocument ; }

Usage: sft filename

In order to get files from your Telegram bot into VPS edit this alias:

alias gft="curl https://api.telegram.org/file/bot<YourBOTToken>/\$(curl -s https://api.telegram.org/bot<YourBOTToken>/getFile?file_id=\$(curl -s https://api.telegram.org/bot<YourBOTToken>/getUpdates |grep -oP file_id'\"':'\"'.*?'\"' | tail -1 | cut -d \"\\\"\" -f 3) |cut -d \":\" -f 7 | cut -d \"\\\"\" -f 2) --output file-from-telegram"

Usage: upload a file to your Telegram bot then type gft in terminal. It will download last file. Any file you download will be saved under the name file-from-telegram so better rename it immediately.

  • The gft code is sometimes tricky, If it fails to download your file just long click the file in Telegram bot, click 'reply' and add some text then try again. It should (eventually) work. Check file size or file type using command file file-from-telegram to verify file.

  • Max file size for Telegram bot is lower than their general limit. It used to be 50MB idk if they increased it as they promised.

If you like stf/gft you can save them in ~/.bashrc so they'll be permanent.

Comments

  • yoursunnyyoursunny Member, IPv6 Advocate

    What's with those greps?
    If you are processing JSON data, use jq command.

    Thanked by 1Not_Oles
  • farsighterfarsighter Member
    edited October 2022

    @yoursunny said:
    What's with those greps?
    If you are processing JSON data, use jq command.

    You're right I wasn't familiar with terminal jq when I made the gft alias 2 years ago, but it works anyway. Also not everyone have jq installed and I try to stick to zero dependencies.

    Thanked by 1Not_Oles
  • I had the same idea and wrote a script for this which also supports adding the telegram message id to the message. This allows you to also edit telegram messages as well by message id. Feel free to borrow code from my script at https://github.com/centminmod/telegram-send :)

    Sending Centmin Mod LEMP stack cminfo command for JSON formatted cpu and memory statistics

    ./tgsend.sh send "$(cminfo sar-json | jq -r '."cpu-load"[]')"
    {
      "from": "centmin",
      "to": "George",
      "date": "2021-07-08T01:16:15Z",
      "message": "[msgid: 40] {\n  \"cpu\": \"all\",\n  \"user\": 1.21,\n  \"nice\": 0,\n  \"system\": 0.49,\n  \"iowait\": 0.43,\n  \"steal\": 0,\n  \"idle\": 97.87\n}"
    }
    
    ./tgsend.sh send "$(cminfo sar-json | jq -r '."memory"')"
    {
      "from": "centmin",
      "to": "George",
      "date": "2021-07-08T01:17:09Z",
      "message": "[msgid: 41] {\n  \"memfree\": 3111888,\n  \"memused\": 29629116,\n  \"memused-percent\": 90.5,\n  \"buffers\": 1323936,\n  \"cached\": 17754908,\n  \"commit\": 25211564,\n  \"commit-percent\": 72.37,\n  \"active\": 13057488,\n  \"inactive\": 9479208,\n  \"dirty\": 84,\n  \"swpfree\": 2091768,\n  \"swpused\": 3328,\n  \"swpused-percent\": 0.16,\n  \"swpcad\": 204,\n  \"swpcad-percent\": 6.13\n}"
    }
    

    Thanked by 2Not_Oles farsighter
  • Not_OlesNot_Oles Moderator, Patron Provider

    @yoursunny said:
    What's with those greps?
    If you are processing JSON data, use jq command.

    I am too ignorant to take a position, but it seems that some people sometimes might like gron | rg.

    Friendly greetings! :)

    References:
    gron ("Make JSON greppable!"): https://github.com/tomnomnom/gron
    ripgrep: https://github.com/BurntSushi/ripgrep

    Thanked by 2farsighter iamATOM
Sign In or Register to comment.