在烧瓶中使用电报上传上传文件?

时间:2020-10-01 06:28:03

标签: python telegram telegram-upload

我们可以在终端上使用以下命令,使用telegram-upload库上传文件

struct MainView: View {
    @State private var currentTab = 1 // try change this to 2/3/4 and notice the first tab onAppear always fires
    
    var body: some View {
        return TabView(selection: self.$currentTab) {
            Colors.brown
                .tabItem {
                    Text("0")
                }
                .tag(0)
                .onAppear(perform: {
                    print(self.currentTab) // why does this always print regardless of initial currentTab = 1/2/3/4?
                })
            
            
            Colors.orange
                .tabItem {
                    Text("1")
                }
                .tag(1)
                .onAppear(perform: {
                    print(self.currentTab) // this prints
                })
            
            Colors.red
                .tabItem {
                    Text("2")
                }
                .tag(2)
                .onAppear(perform: {
                    print(self.currentTab)
                })
            
            
            Color.yellow
                .tabItem {
                    Text("3")
                }
                .tag(3)
                .onAppear(perform: {
                    print(self.currentTab)
                })
            
            
            Color.purple
                .tabItem {
                    Text("4")
                }
                .tag(4)
                .onAppear(perform: {
                    print(self.currentTab)
                })
        }
    }
}

但是,如果我想在python函数中调用此函数,应该怎么做。我的意思是在python函数中,如果用户将文件路径作为参数传递,则该函数应该能够将文件上传到电报服务器。文档中未提及。
换句话说,我想问一下如何从python函数内部执行或运行shell命令?

2 个答案:

答案 0 :(得分:1)

对于telegram-upload,您可以在telegram_upload.management
中使用upload方法 对于telegram-download,请在同一文件中使用download方法。

或者您可以在那里看到它们的实现方式。

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)

答案 1 :(得分:0)

我找到了解决方案。使用os模块,我们可以在python函数(即os.system('telegram-upload file1.mp4 /path/to/file2.mkv')

中运行命令行字符串)