telega/file

File handling utilities for Telegram Bot API Provides types and functions for working with files in media uploads

Types

Represents different ways to specify media in Telegram Bot API

pub type MediaInput {
  Url(url: String)
  FileId(file_id: String)
  LocalFile(path: String, attach_name: String)
  Bytes(data: BitArray, filename: String, attach_name: String)
}

Constructors

  • Url(url: String)

    HTTP or HTTPS URL to a file on the internet

  • FileId(file_id: String)

    File ID of a previously uploaded file (for reuse)

  • LocalFile(path: String, attach_name: String)

    Local file path for multipart upload The attach_name is used to reference the file in the multipart form

  • Bytes(data: BitArray, filename: String, attach_name: String)

    Raw bytes for direct upload

Information needed for multipart file upload

pub type MultipartFile {
  MultipartFile(
    field_name: String,
    filename: String,
    content: BitArray,
    mime_type: option.Option(String),
  )
}

Constructors

  • MultipartFile(
      field_name: String,
      filename: String,
      content: BitArray,
      mime_type: option.Option(String),
    )

    Arguments

    field_name

    Field name in the multipart form

    filename

    File name to send

    content

    File content

    mime_type

    MIME type (optional)

Values

pub fn download_by_path(
  client: client.TelegramClient,
  file_path: String,
) -> Result(BitArray, String)

Downloads a file using its file_path from the File object

pub fn download_file(
  client: client.TelegramClient,
  file_id: String,
) -> Result(BitArray, String)

Downloads a file from Telegram servers First gets the file path using getFile API, then downloads the actual file

pub fn download_to_file(
  client: client.TelegramClient,
  file_id: String,
  save_path: String,
) -> Result(Nil, String)

Downloads a file and saves it to disk

pub fn from_bytes(data: BitArray, filename: String) -> MediaInput

Creates a MediaInput from bytes

pub fn from_file(path: String) -> MediaInput

Creates a MediaInput from a local file path

pub fn from_file_with_name(
  path: String,
  attach_name: String,
) -> MediaInput

Creates a MediaInput from a local file path with custom attach name

pub fn from_string(value: String) -> MediaInput

Creates a MediaInput from a string that could be URL or file ID

pub fn get_attach_name(
  input: MediaInput,
) -> option.Option(String)

Gets the attach name if this is a local file or bytes

pub fn get_file_info(
  client: client.TelegramClient,
  file_id: String,
) -> Result(types.File, String)

Gets file information without downloading

pub fn read_file(
  path: String,
) -> Result(MediaInput, simplifile.FileError)

Reads a file and creates a MediaInput with its contents

pub fn requires_multipart(input: MediaInput) -> Bool

Checks if this MediaInput requires multipart upload

pub fn to_json_value(input: MediaInput) -> String

Gets the string representation for JSON encoding For local files and bytes, returns the attach:// reference

pub fn to_multipart_file(
  input: MediaInput,
) -> Result(option.Option(MultipartFile), simplifile.FileError)

Converts MediaInput to MultipartFile for upload

Search Document