File Handling
Telegram's file upload and download methods are a bit complex. Gogram simplifies this process by providing a few helper methods.
Uploading Files
Limits
Telegram has the following limits for file uploads:
- 2GB for each file.
- 4GB if premium account.
- normal photos: 10MB
Uploading a File
Parellel TCP Connections are openned to increase the upload speed.
UploadFile
file, _ := client.UploadFile("file.jpg")
UploadFileFromBytes
fileBytes = []byte("file content")
file, _ := client.UploadFile(fileBytes)
Upload with Progress
A built in progress bar is available for file uploads.
upload.go
message, _ := client.SendMessage("me", "Uploading file...")
var pm *ProgressManager
_, err = client.SendMedia("me", "Sean Paul - No Lie ft. Dua Lipa.flac",
&telegram.MediaOptions{
ProgressCallback: func(i1, i2 int32) {
if pm == nil {
pm = NewProgressManager(int(i1), 2)
} else {
if pm.shouldEdit() {
client.EditMessage(message, pm.getStats(int(i2)))
}
}
},
}
)
Additional Parameters
- FileName: The name of the file.
- Threads: The number of threads to use for uploading. Default depends on the file size.
- ProgressCallback: A callback function to get the progress of the upload.
- ChunkSize: The size of each chunk to upload. Default is 512KB.
Downloading Files
Downloading a File
Parellel TCP Connections are openned to increase the download speed.
DownloadFile
// m = NewMessage object
file, _ := client.DownloadMedia(m.Media())
DownloadFileToBytes
// m = NewMessage object
var buf = bytes.Buffer{}
client.DownloadMedia(m.Media(), &telegram.DownloadOptions{
Buffer: &buf,
})
fmt.Println(len(buf.Bytes()))
Download with Progress
A built in progress bar is available for file downloads.
download.go
message, _ := client.SendMessage("me", "Downloading file...")
var pm *ProgressManager
_, err = client.DownloadMedia(m.Media(), &telegram.DownloadOptions{
ProgressCallback: func(i1, i2 int32) {
if pm == nil {
pm = NewProgressManager(int(i1), 2)
} else {
if pm.shouldEdit() {
client.EditMessage(message, pm.getStats(int(i2)))
}
}
},
})
Additional Parameters
- Buffer: A buffer to write the file to.
- Threads: The number of threads to use for downloading. Default depends on the file size.
- ProgressCallback: A callback function to get the progress of the download.
- ChunkSize: The size of each chunk to download. Default is 512KB.
- FileName: The name of the file to save as.
- DCId: The data center to download from. (deprecated)
File Methods
GetSendableMedia
resolve any media to its sendable form.
send_media.go
media, _ := client.GetSendableMedia("file.jpg")
// client.GetSendableMedia(m.Media())
client.SendMedia("me", media)