我们可以在终端上使用以下命令,使用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命令?
答案 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')