diff --git a/packages/client/src/methods/messages/send-media.ts b/packages/client/src/methods/messages/send-media.ts index 959618b0..474d091f 100644 --- a/packages/client/src/methods/messages/send-media.ts +++ b/packages/client/src/methods/messages/send-media.ts @@ -91,7 +91,9 @@ export async function sendMedia( let mime = 'application/octet-stream' const input = media.file - if (typeof input === 'string' && input.match(/^https?:\/\//)) { + if (typeof input === 'object' && tl.isAnyInputMedia(input)) { + inputMedia = input + } else if (typeof input === 'string' && input.match(/^https?:\/\//)) { inputMedia = { _: 'inputMediaDocumentExternal', url: input, @@ -116,7 +118,9 @@ export async function sendMedia( if ('thumb' in media && media.thumb) { const t = media.thumb - if (typeof t === 'string' && t.match(/^https?:\/\//)) { + if (typeof t === 'object' && tl.isAnyInputMedia(t)) { + throw new MtCuteArgumentError("Thumbnail can't be InputMedia") + } else if (typeof t === 'string' && t.match(/^https?:\/\//)) { throw new MtCuteArgumentError("Thumbnail can't be external") } else if (isUploadedFile(t)) { thumb = t.inputFile diff --git a/packages/client/src/methods/messages/send-photo.ts b/packages/client/src/methods/messages/send-photo.ts index fb5011d1..60b4efc0 100644 --- a/packages/client/src/methods/messages/send-photo.ts +++ b/packages/client/src/methods/messages/send-photo.ts @@ -94,7 +94,9 @@ export async function sendPhoto( if (!params) params = {} let media: tl.TypeInputMedia - if (typeof photo === 'string' && photo.match(/^https?:\/\//)) { + if (typeof photo === 'object' && tl.isAnyInputMedia(photo)) { + media = photo + } else if (typeof photo === 'string' && photo.match(/^https?:\/\//)) { media = { _: 'inputMediaPhotoExternal', url: photo, diff --git a/packages/client/src/types/files/utils.ts b/packages/client/src/types/files/utils.ts index f2f38c19..1f9029a4 100644 --- a/packages/client/src/types/files/utils.ts +++ b/packages/client/src/types/files/utils.ts @@ -30,7 +30,7 @@ export type UploadFileLike = * can also pass {@link UploadedFile} returned from {@link TelegramClient.uploadFile}, * raw `tl.TypeInputFile` and URLs to remote files */ -export type InputFileLike = UploadFileLike | UploadedFile | tl.TypeInputFile +export type InputFileLike = UploadFileLike | UploadedFile | tl.TypeInputFile | tl.TypeInputMedia export interface FileDownloadParameters { /** diff --git a/packages/client/src/types/media/document.ts b/packages/client/src/types/media/document.ts index e54409ee..90610106 100644 --- a/packages/client/src/types/media/document.ts +++ b/packages/client/src/types/media/document.ts @@ -3,6 +3,7 @@ import { tl } from '@mtcute/tl' import { Thumbnail } from './thumbnail' import { TelegramClient } from '../../client' import { makeInspectable } from '../utils' +import { InputMediaLike } from './input-media' /** * A file that is represented as a document in MTProto. @@ -93,6 +94,36 @@ export class RawDocument extends FileLocation { getThumbnail(type: string): Thumbnail | null { return this.thumbnails.find((it) => it.raw.type === type) ?? null } + + /** + * Input media TL object generated from this object, + * to be used inside {@link InputMediaLike} or {@link TelegramClient.sendPhoto} + */ + get inputMediaTl(): tl.TypeInputMedia { + return { + _: 'inputMediaDocument', + id: { + _: 'inputDocument', + id: this.doc.id, + accessHash: this.doc.accessHash, + fileReference: this.doc.fileReference + } + } + } + + /** + * Input media object generated from this object, + * to be used with {@link TelegramClient.sendMedia} + */ + get inputMedia(): InputMediaLike { + return { + // type is only really used for creating tl.InputMedia, + // but since we are providing it directly, we can use `auto` + type: 'auto', + file: this.inputMediaTl + // other fields are not needed since it's a forwarded media + } + } } /** diff --git a/packages/client/src/types/media/photo.ts b/packages/client/src/types/media/photo.ts index 491b1379..2cfe8766 100644 --- a/packages/client/src/types/media/photo.ts +++ b/packages/client/src/types/media/photo.ts @@ -4,6 +4,7 @@ import { TelegramClient } from '../../client' import { MtCuteArgumentError } from '../errors' import { Thumbnail } from './thumbnail' import { makeInspectable } from '../utils' +import { InputMediaLike } from './input-media' /** * A photo @@ -105,6 +106,37 @@ export class Photo extends FileLocation { getThumbnail(type: string): Thumbnail | null { return this.thumbnails.find((it) => it.raw.type === type) ?? null } + + /** + * Input media generated from this object, + * to be used in {@link TelegramClient.sendPhoto} + * and {@link InputMediaLike} + */ + get inputMediaTl(): tl.TypeInputMedia { + return { + _: 'inputMediaPhoto', + id: { + _: 'inputPhoto', + id: this.raw.id, + accessHash: this.raw.accessHash, + fileReference: this.raw.fileReference + } + } + } + + /** + * Input media object generated from this object, + * to be used with {@link TelegramClient.sendMedia} + */ + get inputMedia(): InputMediaLike { + return { + // type is only really used for creating tl.InputMedia, + // but since we are providing it directly, we can use `auto` + type: 'auto', + file: this.inputMediaTl + // other fields are not needed since it's a forwarded media + } + } } makeInspectable(Photo, ['fileSize', 'dcId', 'width', 'height'])