Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163// === Needed importsimport { type InputFileProxy } from "@grammyjs/types";import { Agent as HttpAgent } from "http";import { Agent as HttpsAgent } from "https";import fetch from "node-fetch";import { basename } from "path";import { Readable } from "stream";import { type ReadStream } from "fs";import { URL } from "url";import { createReadStream } from "fs";
// === Export all API typesexport * from "@grammyjs/types";
// === Export debugimport { debug as d } from "debug";export { d as debug };const debug = d("grammy:warn");
// === Export system-specific operations// Turn an AsyncIterable<Uint8Array> into a streamexport const itrToStream = (itr: AsyncIterable<Uint8Array>) => Readable.from(itr, { objectMode: false });
// === Base configuration for `fetch` callsexport function baseFetchConfig(apiRoot: string) { if (apiRoot.startsWith("https:")) { return { compress: true, agent: new HttpsAgent({ keepAlive: true }) }; } else if (apiRoot.startsWith("http:")) { return { agent: new HttpAgent({ keepAlive: true }) }; } else return {};}
/** 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// Accessor for file data in `InputFile` instancesexport const toRaw = Symbol("InputFile data");
/** * An `InputFile` wraps a number of different sources for [sending * files](https://grammy.dev/guide/files.html#uploading-your-own-file). * * It corresponds to the `InputFile` type in the [Telegram Bot API * Reference](https://core.telegram.org/bots/api#inputfile). */export class InputFile { private consumed = false; private readonly fileData: ConstructorParameters<typeof InputFile>[0]; /** * Optional name of the constructed `InputFile` instance. * * Check out the * [documenation](https://grammy.dev/guide/files.html#uploading-your-own-file) * 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 path to a local file or a `Buffer` or a `fs.ReadStream` that specifies the file data * @param filename Optional name of the file */ constructor( file: | string | URL | URLLike | Uint8Array | ReadStream | Iterable<Uint8Array> | AsyncIterable<Uint8Array>, filename?: string, ) { this.fileData = file; filename ??= this.guessFilename(file); this.filename = filename; if ( typeof file === "string" && (file.startsWith("http:") || file.startsWith("https:")) ) { debug( `InputFile received the local file path '${file}' that looks like a URL. Is this a mistake?`, ); } } 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 (typeof data === "string") return createReadStream(data); // Handle URLs and URLLike objects if (data instanceof URL) { return data.protocol === "file" // node-fetch does not support file URLs ? createReadStream(data.pathname) : 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); for await (const chunk of body) { if (typeof chunk === "string") { throw new Error( `Could not transfer file, received string data instead of bytes from '${url}'`, ); } yield chunk; }}
// === Export InputFile typestype GrammyTypes = InputFileProxy<InputFile>;
/** Wrapper type to bundle all methods of the Telegram API */export type Telegram = 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"];