@gramio/files
Version:
Set of utils to work with files uploading to Telegram Bot API
110 lines (104 loc) • 4.82 kB
TypeScript
import { Readable } from 'node:stream';
import { APIMethods, APIMethodParams, TelegramInputFile, TelegramInputMediaAnimation, TelegramInputMediaDocument, TelegramInputMediaAudio, TelegramInputMediaPhoto, TelegramInputMediaVideo } from '@gramio/types';
/** Guard to check is method used for File Uploading */
declare function isMediaUpload<T extends keyof APIMethods>(method: T, params: NonNullable<APIMethodParams<T>>): boolean;
/**
* 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](https://core.telegram.org/bots/api#inputfile)
*/
declare function convertJsonToFormData<T extends keyof APIMethods>(method: T, params: NonNullable<APIMethodParams<T>>): Promise<FormData>;
/**
* Helper to extract files from params and convert them to FormData. (Similar to {@link convertJsonToFormData})
* if File is not top-level property it will be `“attach://<file_attach_name>”`
*
* [Documentation](https://core.telegram.org/bots/api#inputfile)
*/
declare function extractFilesToFormData<T extends keyof APIMethods>(method: T, params: NonNullable<APIMethodParams<T>>): Promise<[FormData | undefined, NonNullable<APIMethodParams<T>>]>;
/** Helper for convert Readable stream to buffer */
declare function convertStreamToBuffer(stream: Readable): Promise<Buffer>;
type Extractor = {
name: string;
type: "array" | "union";
property: string;
};
type MethodsWithMediaUpload = {
[]?: [
(params: NonNullable<APIMethodParams<Method>>) => boolean,
Extractor[] | null
];
};
/** Guard to check is it {@link Blob} or {@link Promise} */
declare function isBlob(blob?: TelegramInputFile | object | string): boolean;
/**
* A set of methods with the function of checking whether a {@link File} has been passed in the parameters
*
* @codegenerated
* */
declare const MEDIA_METHODS: MethodsWithMediaUpload;
/**
* Class-helper with static methods that represents the content of a media message to be sent.
*
* [Documentation](https://gramio.dev/files/media-input.html)
*/
declare class MediaInput {
/**
* Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent.
*
* [Documentation](https://core.telegram.org/bots/api/#inputmediaanimation)
*/
static animation(media: TelegramInputMediaAnimation["media"], options?: Omit<TelegramInputMediaAnimation, "media" | "type">): TelegramInputMediaAnimation;
/**
* Represents a general file to be sent.
*
* [Documentation](https://core.telegram.org/bots/api/#inputmediadocument)
*/
static document(media: TelegramInputMediaDocument["media"], options?: Omit<TelegramInputMediaDocument, "media" | "type">): TelegramInputMediaDocument;
/**
* Represents an audio file to be treated as music to be sent.
*
* [Documentation](https://core.telegram.org/bots/api/#inputmediaaudio)
*/
static audio(media: TelegramInputMediaAudio["media"], options?: Omit<TelegramInputMediaAudio, "media" | "type">): TelegramInputMediaAudio;
/**
* Represents a photo to be sent.
*
* [Documentation](https://core.telegram.org/bots/api/#inputmediaphoto)
*/
static photo(media: TelegramInputMediaPhoto["media"], options?: Omit<TelegramInputMediaPhoto, "media" | "type">): TelegramInputMediaPhoto;
/**
* Represents a video to be sent.
*
* [Documentation](https://core.telegram.org/bots/api/#inputmediavideo)
*/
static video(media: TelegramInputMediaVideo["media"], options?: Omit<TelegramInputMediaVideo, "media" | "type">): TelegramInputMediaVideo;
}
/**
* Class-helper with static methods for file uploading.
*
* [Documentation](https://gramio.dev/files/media-upload.html)
*/
declare class MediaUpload {
/**
* Method for uploading Media File by local path.
*/
static path(path: string, filename?: string): Promise<File>;
/**
* Method for uploading Media File by Readable stream.
*/
static stream(stream: Readable | ReadableStream, filename?: string): Promise<File>;
/**
* Method for uploading Media File by BinaryLike (Buffer or ArrayBuffer and etc).
*/
static buffer(buffer: Exclude<BufferSource | ArrayBuffer, string>, filename?: string): File;
/**
* Method for uploading Media File by URL (also with fetch options).
*/
static url(url: URL | string, filename?: string, options?: RequestInit): Promise<File>;
/**
* Method for uploading Media File by text content.
*/
static text(text: string, filename?: string): File;
}
export { MEDIA_METHODS, MediaInput, MediaUpload, convertJsonToFormData, convertStreamToBuffer, extractFilesToFormData, isBlob, isMediaUpload };