Skip to content

GramIO API Reference / @gramio/files/dist

@gramio/files/dist

Classes

ClassDescription
MediaInputClass-helper with static methods that represents the content of a media message to be sent.
MediaUploadClass-helper with static methods for file uploading.
TelegramFileDownloadLazy, Response-like handle for downloading a Telegram file.

Interfaces

InterfaceDescription
FileDownloadBotThe 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

Type AliasDescription
DownloadFileInputAnything downloadFile can turn into a file_id.
FileDownloadResolverLazily resolves the file metadata + where to read it from.
FileDownloadSourceA resolved file location used by TelegramFileDownload.
FileSourceStrategy 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

MEDIA_METHODS

const MEDIA_METHODS: MethodsWithMediaUpload

Defined in: files/index.d.ts:43

Methods which can carry Telegram file uploads.

Codegenerated

Functions

convertJsonToFormData()

convertJsonToFormData<T>(method, params): Promise<FormData>

Defined in: files/index.d.ts:12

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>”

Documentation

Type Parameters

Type Parameter
T extends keyof APIMethods

Parameters

ParameterType
methodT
paramsNonNullable<APIMethodParams<T>>

Returns

Promise<FormData>


convertStreamToBuffer()

convertStreamToBuffer(stream): Promise<Buffer<ArrayBufferLike>>

Defined in: files/index.d.ts:21

Helper for convert Readable stream to buffer

Parameters

ParameterType
streamReadable

Returns

Promise<Buffer<ArrayBufferLike>>


downloadFile()

Call Signature

downloadFile(bot, attachment): TelegramFileDownload

Defined in: files/index.d.ts:282

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.

Powers bot.downloadFile / context.download, and is re-exported from gramio so you can call it with a bot instance from anywhere.

Parameters
ParameterType
botFileDownloadBot
attachmentDownloadFileInput
Returns

TelegramFileDownload

Example
ts
import { downloadFile } from "gramio";

const dl = downloadFile(bot, fileId);
await dl.toFile("./photo.jpg");
const text = await downloadFile(bot, fileId).text();

Call Signature

downloadFile(bot, attachment, path): Promise<string>

Defined in: files/index.d.ts:283

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.

Powers bot.downloadFile / context.download, and is re-exported from gramio so you can call it with a bot instance from anywhere.

Parameters
ParameterType
botFileDownloadBot
attachmentDownloadFileInput
pathstring
Returns

Promise<string>

Example
ts
import { downloadFile } from "gramio";

const dl = downloadFile(bot, fileId);
await dl.toFile("./photo.jpg");
const text = await downloadFile(bot, fileId).text();

extractFilesToFormData()

extractFilesToFormData<T>(method, params): Promise<[FormData, NonNullable<APIMethodParams<T>>]>

Defined in: files/index.d.ts:19

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>”

Documentation

Type Parameters

Type Parameter
T extends keyof APIMethods

Parameters

ParameterType
methodT
paramsNonNullable<APIMethodParams<T>>

Returns

Promise<[FormData, NonNullable<APIMethodParams<T>>]>


isBlob()

isBlob(blob?): boolean

Defined in: files/index.d.ts:41

Guard to check is it Blob or Promise.

Parameters

ParameterType
blob?string | object | Blob

Returns

boolean


isMediaUpload()

isMediaUpload<T>(method, params): boolean

Defined in: files/index.d.ts:5

Guard to check is method used for File Uploading

Type Parameters

Type Parameter
T extends keyof APIMethods

Parameters

ParameterType
methodT
paramsNonNullable<APIMethodParams<T>>

Returns

boolean