Skip to main content


The Telegram Bot Framework.
Extremely Popular
Go to Latest
// === Needed importsimport { basename } from "";import { type InputFileProxy } from "";import { toRaw } from "./platform.deno.ts";
// === Export all API typesexport * from "";
/** Something that looks like a URL. */interface URLLike { /** * Identifier of the resource. Must be in a format that can be parsed by the * URL constructor. */ url: string;}
// === InputFile handling and File augmenting/** * An `InputFile` wraps a number of different sources for [sending * files]( * * It corresponds to the `InputFile` type in the [Telegram Bot API * Reference]( */export class InputFile { private consumed = false; private readonly fileData: ConstructorParameters<typeof InputFile>[0]; /** * Optional name of the constructed `InputFile` instance. * * Check out the * [documentation]( * on sending files with `InputFile`. */ public readonly filename?: string; /** * Constructs an `InputFile` that can be used in the API to send files. * * @param file A URL to a file or a `Blob` or other forms of file data * @param filename Optional name of the file */ constructor( file: | Blob | URL | URLLike | Uint8Array | ReadableStream<Uint8Array> | Iterable<Uint8Array> | AsyncIterable<Uint8Array>, filename?: string, ) { this.fileData = file; filename ??= this.guessFilename(file); this.filename = filename; } private guessFilename( file: ConstructorParameters<typeof InputFile>[0], ): string | undefined { if (typeof file === "string") return basename(file); if (typeof file !== "object") return undefined; if ("url" in file) return basename(file.url); if (!(file instanceof URL)) return undefined; return basename(file.pathname) || basename(file.hostname); } [toRaw](): Uint8Array | Iterable<Uint8Array> | AsyncIterable<Uint8Array> { if (this.consumed) { throw new Error("Cannot reuse InputFile data source!"); } const data = this.fileData; // Handle local files if (data instanceof Blob) return; // Handle URL and URLLike objects if (data instanceof URL) return fetchFile(data); if ("url" in data) return fetchFile(data.url); // Mark streams and iterators as consumed if (!(data instanceof Uint8Array)) this.consumed = true; // Return buffers and byte streams as-is return data; }}
async function* fetchFile(url: string | URL): AsyncIterable<Uint8Array> { const { body } = await fetch(url); if (body === null) { throw new Error(`Download failed, no response body from '${url}'`); } yield* body;}
// === Export InputFile typestype GrammyTypes = InputFileProxy<InputFile>;
/** Wrapper type to bundle all methods of the Telegram API */export type ApiMethods = GrammyTypes["Telegram"];
/** Utility type providing the argument type for the given method name or `{}` if the method does not take any parameters */export type Opts<M extends keyof GrammyTypes["Telegram"]> = GrammyTypes["Opts"][M];
/** This object represents the content of a media message to be sent. It should be one of- InputMediaAnimation- InputMediaDocument- InputMediaAudio- InputMediaPhoto- InputMediaVideo */export type InputMedia = GrammyTypes["InputMedia"];/** Represents a photo to be sent. */export type InputMediaPhoto = GrammyTypes["InputMediaPhoto"];/** Represents a video to be sent. */export type InputMediaVideo = GrammyTypes["InputMediaVideo"];/** Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent. */export type InputMediaAnimation = GrammyTypes["InputMediaAnimation"];/** Represents an audio file to be treated as music to be sent. */export type InputMediaAudio = GrammyTypes["InputMediaAudio"];/** Represents a general file to be sent. */export type InputMediaDocument = GrammyTypes["InputMediaDocument"];