fix(client): allow caption in every media type

This commit is contained in:
teidesu 2022-07-08 15:01:19 +03:00
parent 51280d6494
commit 61ca4130c8
2 changed files with 41 additions and 54 deletions

View file

@ -5,12 +5,7 @@ import { InputFileLike } from '../files'
import { Venue } from './venue' import { Venue } from './venue'
import { FormattedString } from '../parser' import { FormattedString } from '../parser'
interface BaseInputMedia { interface CaptionMixin {
/**
* File to be sent
*/
file: InputFileLike
/** /**
* Caption of the media * Caption of the media
*/ */
@ -21,6 +16,13 @@ interface BaseInputMedia {
* If passed, parse mode is ignored * If passed, parse mode is ignored
*/ */
entities?: tl.TypeMessageEntity[] entities?: tl.TypeMessageEntity[]
}
interface FileMixin {
/**
* File to be sent
*/
file: InputFileLike
/** /**
* Override file name for the file. * Override file name for the file.
@ -58,14 +60,14 @@ interface BaseInputMedia {
* newly uploaded photos with `auto` will be * newly uploaded photos with `auto` will be
* uploaded as a document * uploaded as a document
*/ */
export interface InputMediaAuto extends BaseInputMedia { export interface InputMediaAuto extends FileMixin, CaptionMixin {
type: 'auto' type: 'auto'
} }
/** /**
* An audio file or voice message to be sent * An audio file or voice message to be sent
*/ */
export interface InputMediaAudio extends BaseInputMedia { export interface InputMediaAudio extends FileMixin, CaptionMixin {
type: 'audio' type: 'audio'
/** /**
@ -104,7 +106,7 @@ export interface InputMediaAudio extends BaseInputMedia {
/** /**
* Voice message to be sent * Voice message to be sent
*/ */
export interface InputMediaVoice extends BaseInputMedia { export interface InputMediaVoice extends FileMixin, CaptionMixin {
type: 'voice' type: 'voice'
/** /**
@ -129,7 +131,7 @@ export interface InputMediaVoice extends BaseInputMedia {
/** /**
* A generic file to be sent * A generic file to be sent
*/ */
export interface InputMediaDocument extends BaseInputMedia { export interface InputMediaDocument extends FileMixin, CaptionMixin {
type: 'document' type: 'document'
/** /**
@ -147,19 +149,16 @@ export interface InputMediaDocument extends BaseInputMedia {
/** /**
* A photo to be sent * A photo to be sent
*/ */
export interface InputMediaPhoto extends BaseInputMedia { export interface InputMediaPhoto extends FileMixin, CaptionMixin {
type: 'photo' type: 'photo'
} }
/** /**
* A sticker to be sent * A sticker to be sent
*/ */
export interface InputMediaSticker extends BaseInputMedia { export interface InputMediaSticker extends FileMixin, CaptionMixin {
type: 'sticker' type: 'sticker'
caption?: never
entities?: never
/** /**
* Whether this sticker is animated? * Whether this sticker is animated?
* *
@ -184,7 +183,7 @@ export interface InputMediaSticker extends BaseInputMedia {
/** /**
* A video to be sent * A video to be sent
*/ */
export interface InputMediaVideo extends BaseInputMedia { export interface InputMediaVideo extends FileMixin, CaptionMixin {
type: 'video' type: 'video'
/** /**
@ -244,7 +243,7 @@ export interface InputMediaVideo extends BaseInputMedia {
/** /**
* A geolocation to be sent * A geolocation to be sent
*/ */
export interface InputMediaGeo { export interface InputMediaGeo extends CaptionMixin {
type: 'geo' type: 'geo'
/** /**
@ -301,7 +300,7 @@ export interface InputMediaGeoLive extends Omit<InputMediaGeo, 'type'> {
* Note that dice result value is generated randomly on the server, * Note that dice result value is generated randomly on the server,
* you can't influence it in any way! * you can't influence it in any way!
*/ */
export interface InputMediaDice { export interface InputMediaDice extends CaptionMixin {
type: 'dice' type: 'dice'
/** /**
@ -313,7 +312,7 @@ export interface InputMediaDice {
/** /**
* A venue to be sent * A venue to be sent
*/ */
export interface InputMediaVenue { export interface InputMediaVenue extends CaptionMixin {
type: 'venue' type: 'venue'
/** /**
@ -345,7 +344,7 @@ export interface InputMediaVenue {
/** /**
* A contact to be sent * A contact to be sent
*/ */
export interface InputMediaContact { export interface InputMediaContact extends CaptionMixin {
type: 'contact' type: 'contact'
/** /**
@ -373,7 +372,7 @@ export interface InputMediaContact {
/** /**
* A game to be sent * A game to be sent
*/ */
export interface InputMediaGame { export interface InputMediaGame extends CaptionMixin {
type: 'game' type: 'game'
/** /**
@ -385,7 +384,7 @@ export interface InputMediaGame {
/** /**
* An invoice to be sent (see https://core.telegram.org/bots/payments) * An invoice to be sent (see https://core.telegram.org/bots/payments)
*/ */
export interface InputMediaInvoice { export interface InputMediaInvoice extends CaptionMixin {
type: 'invoice' type: 'invoice'
/** /**
@ -440,8 +439,12 @@ export interface InputMediaInvoice {
/** /**
* A simple poll to be sent * A simple poll to be sent
*
* > **Note**: when using user account,
* > the poll can't be sent to PMs, only to channels/groups,
* > otherwise there will be `MEDIA_INVALID` error.
*/ */
export interface InputMediaPoll { export interface InputMediaPoll extends CaptionMixin {
type: 'poll' type: 'poll'
/** /**
@ -532,23 +535,6 @@ export interface InputMediaQuiz extends Omit<InputMediaPoll, 'type'> {
solutionEntities?: tl.TypeMessageEntity[] solutionEntities?: tl.TypeMessageEntity[]
} }
/**
* Input media that can have a caption.
*
* Note that meta-fields (like `duration`) are only
* applicable if `file` is {@link UploadFileLike},
* otherwise they are ignored.
*
* A subset of {@link InputMediaLike}
*/
export type InputMediaWithCaption =
| InputMediaAudio
| InputMediaVoice
| InputMediaDocument
| InputMediaPhoto
| InputMediaVideo
| InputMediaAuto
/** /**
* Input media that can be sent somewhere. * Input media that can be sent somewhere.
* *
@ -559,7 +545,12 @@ export type InputMediaWithCaption =
* @link InputMedia * @link InputMedia
*/ */
export type InputMediaLike = export type InputMediaLike =
| InputMediaWithCaption | InputMediaAudio
| InputMediaVoice
| InputMediaDocument
| InputMediaPhoto
| InputMediaVideo
| InputMediaAuto
| InputMediaSticker | InputMediaSticker
| InputMediaVenue | InputMediaVenue
| InputMediaGeo | InputMediaGeo
@ -752,12 +743,12 @@ export namespace InputMedia {
* as static members of {@link Dice}. * as static members of {@link Dice}.
* *
* @param emoji Emoji representing the dice * @param emoji Emoji representing the dice
* @param params Additional parameters
*/ */
export function dice(emoji: string): InputMediaDice { export function dice(emoji: string, params: CaptionMixin): InputMediaDice {
return { const ret = params as tl.Mutable<InputMediaDice>
type: 'dice', ret.type = 'dice'
emoji, return ret
}
} }
/** /**

View file

@ -5,8 +5,7 @@ import { MessageEntity } from './message-entity'
import { Message } from './message' import { Message } from './message'
import { InputPeerLike } from '../peers' import { InputPeerLike } from '../peers'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { InputMediaWithCaption } from '../media' import { InputMediaLike } from '../media'
/** /**
* A draft message * A draft message
@ -94,17 +93,14 @@ export class DraftMessage {
* @link TelegramClient.sendMedia * @link TelegramClient.sendMedia
*/ */
sendWithMedia( sendWithMedia(
media: InputMediaWithCaption, media: InputMediaLike,
params?: Parameters<TelegramClient['sendMedia']>[2] params?: Parameters<TelegramClient['sendMedia']>[2]
): Promise<Message> { ): Promise<Message> {
if (!media.caption) {
media.caption = this.raw.message
media.entities = this.raw.entities
}
return this.client.sendMedia(this._chatId, media, { return this.client.sendMedia(this._chatId, media, {
clearDraft: true, clearDraft: true,
replyTo: this.raw.replyToMsgId, replyTo: this.raw.replyToMsgId,
caption: this.raw.message,
entities: this.raw.entities,
...(params || {}), ...(params || {}),
}) })
} }