2021-05-01 00:30:53 +03:00
|
|
|
import { TelegramClient } from '../../client'
|
|
|
|
import {
|
|
|
|
BotKeyboard,
|
|
|
|
InputMediaLike,
|
|
|
|
InputPeerLike,
|
|
|
|
Message,
|
|
|
|
ReplyMarkup,
|
|
|
|
} from '../../types'
|
|
|
|
import { normalizeDate, randomUlong } from '../../utils/misc-utils'
|
|
|
|
import { tl } from '@mtcute/tl'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a group of media.
|
|
|
|
*
|
|
|
|
* @param chatId ID of the chat, its username, phone or `"me"` or `"self"`
|
|
|
|
* @param medias Medias contained in the message.
|
|
|
|
* @param params Additional sending parameters
|
2021-05-04 13:08:20 +03:00
|
|
|
* @link InputMedia
|
2021-05-01 00:30:53 +03:00
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export async function sendMediaGroup(
|
|
|
|
this: TelegramClient,
|
|
|
|
chatId: InputPeerLike,
|
|
|
|
medias: InputMediaLike[],
|
|
|
|
params?: {
|
|
|
|
/**
|
|
|
|
* Message to reply to. Either a message object or message ID.
|
|
|
|
*/
|
|
|
|
replyTo?: number | Message
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse mode to use to parse entities before sending
|
|
|
|
* the message. Defaults to current default parse mode (if any).
|
|
|
|
*
|
|
|
|
* Passing `null` will explicitly disable formatting.
|
|
|
|
*/
|
|
|
|
parseMode?: string | null
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to send this message silently.
|
|
|
|
*/
|
|
|
|
silent?: boolean
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If set, the message will be scheduled to this date.
|
|
|
|
* When passing a number, a UNIX time in ms is expected.
|
|
|
|
*/
|
|
|
|
schedule?: Date | number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For bots: inline or reply markup or an instruction
|
|
|
|
* to hide a reply keyboard or to force a reply.
|
|
|
|
*/
|
|
|
|
replyMarkup?: ReplyMarkup
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that will be called after some part has been uploaded.
|
|
|
|
* Only used when a file that requires uploading is passed,
|
|
|
|
* and not used when uploading a thumbnail.
|
|
|
|
*
|
|
|
|
* @param index Index of the media in the original array
|
|
|
|
* @param uploaded Number of bytes already uploaded
|
|
|
|
* @param total Total file size
|
|
|
|
*/
|
2021-05-07 14:26:29 +03:00
|
|
|
progressCallback?: (
|
|
|
|
index: number,
|
|
|
|
uploaded: number,
|
|
|
|
total: number
|
|
|
|
) => void
|
2021-05-01 00:30:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to clear draft after sending this message.
|
|
|
|
*
|
|
|
|
* Defaults to `false`
|
|
|
|
*/
|
|
|
|
clearDraft?: boolean
|
|
|
|
}
|
|
|
|
): Promise<Message> {
|
|
|
|
if (!params) params = {}
|
|
|
|
|
2021-05-15 20:25:59 +03:00
|
|
|
const peer = await this.resolvePeer(chatId)
|
2021-05-01 00:30:53 +03:00
|
|
|
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
|
|
|
|
|
|
|
|
const multiMedia: tl.RawInputSingleMedia[] = []
|
|
|
|
|
|
|
|
for (let i = 0; i < medias.length; i++) {
|
|
|
|
const media = medias[i]
|
|
|
|
const inputMedia = await this._normalizeInputMedia(media, {
|
2021-05-07 14:26:29 +03:00
|
|
|
progressCallback: params.progressCallback?.bind(null, i),
|
2021-05-01 00:30:53 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
const [message, entities] = await this._parseEntities(
|
2021-05-07 14:26:29 +03:00
|
|
|
// some types dont have `caption` field, and ts warns us,
|
|
|
|
// but since it's JS, they'll just be `undefined` and properly
|
|
|
|
// handled by _parseEntities method
|
|
|
|
(media as any).caption,
|
2021-05-01 00:30:53 +03:00
|
|
|
params.parseMode,
|
2021-05-07 14:26:29 +03:00
|
|
|
(media as any).entities
|
2021-05-01 00:30:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
multiMedia.push({
|
|
|
|
_: 'inputSingleMedia',
|
|
|
|
randomId: randomUlong(),
|
|
|
|
media: inputMedia,
|
|
|
|
message,
|
2021-05-07 14:26:29 +03:00
|
|
|
entities,
|
2021-05-01 00:30:53 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await this.call({
|
|
|
|
_: 'messages.sendMultiMedia',
|
|
|
|
peer,
|
|
|
|
multiMedia,
|
|
|
|
silent: params.silent,
|
|
|
|
replyToMsgId: params.replyTo
|
|
|
|
? typeof params.replyTo === 'number'
|
|
|
|
? params.replyTo
|
|
|
|
: params.replyTo.id
|
|
|
|
: undefined,
|
|
|
|
randomId: randomUlong(),
|
|
|
|
scheduleDate: normalizeDate(params.schedule),
|
|
|
|
replyMarkup,
|
|
|
|
clearDraft: params.clearDraft,
|
|
|
|
})
|
|
|
|
|
|
|
|
return this._findMessageInUpdate(res)
|
|
|
|
}
|