Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Files

Telegram supports specifying files in many different formats. In order to accommodate them all, there are multiple structs and type aliases required.

All of these types implement the RequestFileData interface.

TypeDescription
FilePathA local path to a file
FileIDExisting file ID on Telegram’s servers
FileURLURL to file, must be served with expected MIME type
FileReaderUse an io.Reader to provide a file. Lazily read to save memory.
FileBytes[]byte containing file data. Prefer to use FileReader to save memory.

FilePath

A path to a local file.

file := tgbotapi.FilePath("tests/image.jpg")

FileID

An ID previously uploaded to Telegram. IDs may only be reused by the same bot that received them. Additionally, thumbnail IDs cannot be reused.

file := tgbotapi.FileID("AgACAgIAAxkDAALesF8dCjAAAa_…")

FileURL

A URL to an existing resource. It must be served with a correct MIME type to work as expected.

file := tgbotapi.FileURL("https://i.imgur.com/unQLJIb.jpg")

FileReader

Use an io.Reader to provide file contents as needed. Requires a filename for the virtual file.

When BotAPI.MaxRetries is enabled, uploads are automatically retried on 429 Too Many Requests responses only if every uploaded file is a FileBytes or a FilePath (FileID and FileURL are not uploaded and don’t prevent retries). A FileReader stream is consumed during upload, so requests uploading a FileReader, or any custom RequestFileData implementation, are never retried. Use FileBytes or FilePath if you need automatic retries.

var reader io.Reader

file := tgbotapi.FileReader{
    Name: "image.jpg",
    Reader: reader,
}

FileBytes

Use a []byte to provide file contents. Generally try to avoid this as it results in high memory usage. Also requires a filename for the virtual file.

var data []byte

file := tgbotapi.FileBytes{
    Name: "image.jpg",
    Bytes: data,
}