Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Set of utils for work with files and Telegram Bot API
Examples
Example 1
Example 1
import { MediaInput, MediaUpload } from "@gramio/files"; // method for sendMediaGroup context.sendMediaGroup([ MediaInput.document( MediaUpload.url( "https://raw.githubusercontent.com/gramiojs/types/main/README.md" ) ), MediaInput.document(MediaUpload.path("./package.json")), ]);
Classes
Class-helper with static methods that represents the content of a media message to be sent.
- animation(): TelegramInputMediaAnimationmedia: TelegramInputMediaAnimation["media"],options?: Omit<TelegramInputMediaAnimation, "media" | "type">
Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent.
- audio(): TelegramInputMediaAudiomedia: TelegramInputMediaAudio["media"],options?: Omit<TelegramInputMediaAudio, "media" | "type">
Represents an audio file to be treated as music to be sent.
- document(): TelegramInputMediaDocumentmedia: TelegramInputMediaDocument["media"],options?: Omit<TelegramInputMediaDocument, "media" | "type">
Represents a general file to be sent.
- photo(): TelegramInputMediaPhotomedia: TelegramInputMediaPhoto["media"],options?: Omit<TelegramInputMediaPhoto, "media" | "type">
Represents a photo to be sent.
- video(): TelegramInputMediaVideomedia: TelegramInputMediaVideo["media"],options?: Omit<TelegramInputMediaVideo, "media" | "type">
Represents a video to be sent.
Class-helper with static methods for file uploading.
- buffer(): Filebuffer: Exclude<BufferSource | ArrayBuffer, string>,filename?: string
Method for uploading Media File by BinaryLike (Buffer or ArrayBuffer and etc).
- localPath(path: string): string
Reference a file by its local path on a self-hosted Bot API server (
--localmode) using thefile://URI scheme. The server reads the file straight from its own disk, so the bytes are never transferred over HTTP. - path(): Promise<File>path: string,filename?: string
Method for uploading Media File by local path.
- stream(): Promise<File>stream: Readable | ReadableStream,filename?: string
Method for uploading Media File by Readable stream.
- text(): Filetext: string,filename?: string
Method for uploading Media File by text content.
- url(): Promise<File>url: URL | string,filename?: string,options?: RequestInit
Method for uploading Media File by URL (also with fetch options).
Lazy, Response-like handle for downloading a Telegram file.
- arrayBuffer(): Promise<ArrayBuffer>
The whole file as an
ArrayBuffer. - blob(): Promise<Blob>
The whole file as a
Blob. - bytes(): Promise<Uint8Array>
The whole file as a
Uint8Array. - info(): Promise<TelegramFile>
The
getFilemetadata (file_id,file_path,file_size, …). Does not read the body. - json<T = unknown>(): Promise<T>
The file parsed as JSON.
- link(): Promise<string>
A download URL for the file. Does not read the body.
- resolve(): Promise<{ file: TelegramFile; source: FileDownloadSource; }>No documentation available
- response(): Promise<Response>No documentation available
- stream(): Promise<ReadableStream<Uint8Array>>
A web
ReadableStreamof the file — pipe large files without buffering. - text(): Promise<string>
The file decoded as UTF-8 text.
- then<TResult1 = ArrayBuffer, TResult2 = never>(): Promise<TResult1 | TResult2>onfulfilled?:((value: ArrayBuffer) => TResult1 | PromiseLike<TResult1>)
| null,onrejected?:((reason: unknown) => TResult2 | PromiseLike<TResult2>)
| nullPromiseLike:await downloadresolves the file bytes (back-compatible). - toFile(path: string): Promise<string>
Save the file to
pathand return it. Copies on disk when possible (no extra transfer).
Functions
Helper to convert JSON to FormData that can accept Telegram Bot API.
if File is not top-level property it will be “attach://<file_attach_name>”
Helper for convert Readable stream to buffer
Download a Telegram file. Returns a TelegramFileDownload handle
(.bytes(), .text(), .json(), .blob(), .stream(), .toFile(),
.link(), .info(); await it for an ArrayBuffer). Pass path to save
straight to disk and get the path back.
Helper to extract files from params and convert them to FormData. (Similar to convertJsonToFormData)
if File is not top-level property it will be “attach://<file_attach_name>”
Guard to check is method used for File Uploading
Interfaces
The minimal Bot-shaped object downloadFile needs. The GramIO Bot
satisfies it structurally, so you just pass the instance — and a custom
"without gramio" wrapper can pass any object of this shape.
Type Aliases
| { file_id: string; }
| { fileId: string; }
| { bigSize: { fileId: string; }; }
Anything downloadFile can turn into a file_id.
Lazily resolves the file metadata + where to read it from.
| { type: "disk"; path: string; }
| { type: "data"; data: ArrayBuffer | Uint8Array; }
A resolved file location used by TelegramFileDownload.
| "url"
| "disk"
| "rewrite"
| ((file: TelegramFile) => Promise<ArrayBuffer> | ArrayBuffer)
Strategy for how downloadFile obtains file bytes — mainly relevant with a
local Bot API server (--local), where getFile returns an absolute path on
the server's disk instead of a URL.
Variables
A set of methods with the function of checking whether a File has been passed in the parameters