Skip to content

MTGo Media & File Transfer API ​

MTGo provides a comprehensive media and file transfer API for uploading and downloading files through Telegram's distributed storage system. Files are automatically split into parts, encrypted, and distributed across Telegram's data centers.

Types ​

FileChunk ​

go
type FileChunk struct {
    Offset int64 // Byte offset of the current chunk
    Bytes  int64 // Bytes transferred so far
    Total  int64 // Total file size in bytes
}

Progress callback receives FileChunk on each part transfer.

UploadResult ​

go
type UploadResult struct {
    FileID int64 // Unique file identifier
    Parts  int   // Number of uploaded parts
    // Additional fields provided by Telegram
}

Returned by all upload methods.


Upload ​

UploadFile ​

go
func (c *Client) UploadFile(ctx context.Context, path string) (*UploadResult, error)

Uploads a file from disk by path.

ParameterTypeDescription
ctxcontext.ContextRequest context
pathstringAbsolute or relative file path
ReturnDescription
*UploadResultUploaded file metadata
errorFile access or upload error
go
result, err := client.UploadFile(ctx, "/path/to/photo.jpg")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Uploaded file ID: %d, parts: %d\n", result.FileID, result.Parts)

UploadFromReader ​

go
func (c *Client) UploadFromReader(ctx context.Context, reader io.Reader, size int64, fileName string) (*UploadResult, error)

Uploads file content from an io.Reader.

ParameterTypeDescription
ctxcontext.ContextRequest context
readerio.ReaderData source
sizeint64Total size in bytes (required)
fileNamestringFile name for the upload
go
buf := bytes.NewReader(data)
result, err := client.UploadFromReader(ctx, buf, int64(len(data)), "document.pdf")
if err != nil {
    log.Fatal(err)
}

UploadFromFile ​

go
func (c *Client) UploadFromFile(ctx context.Context, file *os.File) (*UploadResult, error)

Uploads from an already-opened *os.File. Useful when you need to control file opening yourself.

ParameterTypeDescription
ctxcontext.ContextRequest context
file*os.FileOpen file handle
go
f, err := os.Open("video.mp4")
if err != nil {
    log.Fatal(err)
}
defer f.Close()

result, err := client.UploadFromFile(ctx, f)
if err != nil {
    log.Fatal(err)
}

Download ​

DownloadFile ​

go
func (c *Client) DownloadFile(
    ctx context.Context,
    location tg.InputFileLocationClass,
    dcID int32,
    progress func(FileChunk),
) ([]byte, error)

Downloads a file to memory. Returns the full file content as a byte slice.

ParameterTypeDescription
ctxcontext.ContextRequest context
locationtg.InputFileLocationClassFile location from Telegram
dcIDint32Data center ID
progressfunc(FileChunk)Optional progress callback
ReturnDescription
[]byteFile content
errorDownload or network error
go
data, err := client.DownloadFile(ctx, fileLoc, dcID, func(chunk media.FileChunk) {
    pct := float64(chunk.Bytes) / float64(chunk.Total) * 100
    fmt.Printf("\rDownloading: %.1f%%", pct)
})
if err != nil {
    log.Fatal(err)
}
fmt.Printf("\nDownloaded %d bytes\n", len(data))

DownloadToFile ​

go
func (c *Client) DownloadToFile(
    ctx context.Context,
    location tg.InputFileLocationClass,
    dcID int32,
    filePath string,
    fileSize int64,
    progress func(FileChunk),
) error

Downloads a file directly to disk, avoiding loading the entire file into memory.

ParameterTypeDescription
ctxcontext.ContextRequest context
locationtg.InputFileLocationClassFile location
dcIDint32Data center ID
filePathstringDestination file path
fileSizeint64Expected file size for progress
progressfunc(FileChunk)Optional progress callback
go
err := client.DownloadToFile(ctx, fileLoc, dcID, "/tmp/download.mp4", fileSize, nil)
if err != nil {
    log.Fatal(err)
}

DownloadMedia ​

go
func (c *Client) DownloadMedia(
    ctx context.Context,
    media types.Media,
    thumbSize string,
    progress func(FileChunk),
) ([]byte, error)

Downloads media from a types.Media object. MTGo resolves the file location automatically.

ParameterTypeDescription
ctxcontext.ContextRequest context
mediatypes.MediaMedia object from a message
thumbSizestringThumbnail size (e.g. "m", "x", "y", "w")
progressfunc(FileChunk)Optional progress callback
go
data, err := client.DownloadMedia(ctx, msg.Photo, "", nil)
if err != nil {
    log.Fatal(err)
}
os.WriteFile("photo.jpg", data, 0644)

DownloadMediaToFile ​

go
func (c *Client) DownloadMediaToFile(
    ctx context.Context,
    media types.Media,
    thumbSize string,
    filePath string,
    fileSize int64,
    progress func(FileChunk),
) error

Downloads media from a types.Media object directly to a file on disk.

ParameterTypeDescription
ctxcontext.ContextRequest context
mediatypes.MediaMedia object
thumbSizestringThumbnail size or empty for full
filePathstringDestination path
fileSizeint64Expected size
progressfunc(FileChunk)Optional progress callback
go
err := client.DownloadMediaToFile(ctx, msg.Document, "", "report.pdf", msg.Document.Size, nil)
if err != nil {
    log.Fatal(err)
}

Streaming ​

StreamFile ​

go
func (c *Client) StreamFile(
    ctx context.Context,
    location tg.InputFileLocationClass,
    dcID int32,
    chunkSize int,
    handler func(chunk []byte) error,
) error

Streams a file in chunks through a handler callback. Each chunk is passed to handler as it arrives, enabling real-time processing without buffering the entire file.

ParameterTypeDescription
ctxcontext.ContextRequest context
locationtg.InputFileLocationClassFile location
dcIDint32Data center ID
chunkSizeintDesired chunk size in bytes
handlerfunc(chunk []byte) errorChunk handler; return error to abort
go
var buf bytes.Buffer
err := client.StreamFile(ctx, fileLoc, dcID, 64*1024, func(chunk []byte) error {
    buf.Write(chunk)
    return nil
})
if err != nil {
    log.Fatal(err)
}

Resolve File Location ​

GetFileLocation ​

go
func GetFileLocation(media types.Media, thumbSize string) (tg.InputFileLocationClass, int32, error)

Resolves a types.Media object into a Telegram file location and data center ID. Used internally by DownloadMedia and DownloadMediaToFile, but exposed for advanced use cases.

ParameterTypeDescription
mediatypes.MediaMedia object
thumbSizestringThumbnail size or empty for full file
ReturnDescription
tg.InputFileLocationClassResolved file location
int32Data center ID
errorUnsupported media type or missing fields
go
loc, dcID, err := media.GetFileLocation(msg.Photo, "m")
if err != nil {
    log.Fatal(err)
}
data, err := client.DownloadFile(ctx, loc, dcID, nil)

Progress Tracking ​

All download and upload methods accept an optional progress func(FileChunk) callback. Use it to build progress bars, log transfer status, or cancel long transfers.

go
progress := func(chunk media.FileChunk) {
    if chunk.Total > 0 {
        pct := float64(chunk.Bytes) / float64(chunk.Total) * 100
        fmt.Printf("Progress: %.1f%% (%d / %d bytes)\n", pct, chunk.Bytes, chunk.Total)
    }
}

data, err := client.DownloadFile(ctx, loc, dcID, progress)

For cancellation, use the context:

go
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

data, err := client.DownloadFile(ctx, loc, dcID, progress)
if errors.Is(err, context.DeadlineExceeded) {
    fmt.Println("Download timed out")
}

Released under the Apache-2.0 License.