Jack King
Jack King

Reputation: 41

Upload file using telegram-upload in flask?

We can upload file using telegram-upload library by using the following command on terminal

telegram-upload file1.mp4 /path/to/file2.mkv

But if I want to call this inside python function, How should I do it. I mean in a python function if users passes the file path as an argument, then that function should be able to upload the file to telegram server.It is not mentioned in the documentation.
In other words I want to ask how to execute or run shell commands from inside python function?

Upvotes: 1

Views: 384

Answers (2)

wowkin2
wowkin2

Reputation: 6355

For telegram-upload you can use upload method in telegram_upload.management and
for telegram-download use download method in the same file.

Or you can see how they are implemented there.

from telegram_upload.client import Client
from telegram_upload.config import default_config, CONFIG_FILE
from telegram_upload.exceptions import catch
from telegram_upload.files import NoDirectoriesFiles, RecursiveFiles

DIRECTORY_MODES = {
    'fail': NoDirectoriesFiles,
    'recursive': RecursiveFiles,
}

def upload(files, to, config, delete_on_success, print_file_id, force_file, forward, caption, directories,
           no_thumbnail):
    """Upload one or more files to Telegram using your personal account.
    The maximum file size is 1.5 GiB and by default they will be saved in
    your saved messages.
    """
    client = Client(config or default_config())
    client.start()
    files = DIRECTORY_MODES[directories](files)
    if directories == 'fail':
        # Validate now
        files = list(files)
    client.send_files(to, files, delete_on_success, print_file_id, force_file, forward, caption, no_thumbnail)

Upvotes: 1

Jack King
Jack King

Reputation: 41

I found the solution.Using os module we can run command line strings inside python function i.e. os.system('telegram-upload file1.mp4 /path/to/file2.mkv')

Upvotes: 0

Related Questions