2021-05-01 00:30:53 +03:00
|
|
|
import { TelegramClient } from '../../client'
|
|
|
|
import {
|
2021-06-10 02:31:48 +03:00
|
|
|
BotKeyboard, InputFileLike,
|
2021-05-01 00:30:53 +03:00
|
|
|
InputMediaLike,
|
|
|
|
InputPeerLike,
|
|
|
|
Message,
|
|
|
|
ReplyMarkup,
|
|
|
|
} from '../../types'
|
2021-06-06 15:20:41 +03:00
|
|
|
import {
|
|
|
|
normalizeDate,
|
|
|
|
normalizeMessageId,
|
|
|
|
randomUlong,
|
|
|
|
} from '../../utils/misc-utils'
|
2021-05-01 00:30:53 +03:00
|
|
|
import { tl } from '@mtcute/tl'
|
2021-05-16 14:43:23 +03:00
|
|
|
import { assertIsUpdatesGroup } from '../../utils/updates-utils'
|
|
|
|
import { createUsersChatsIndex } from '../../utils/peer-utils'
|
2021-05-01 00:30:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a group of media.
|
|
|
|
*
|
2021-06-10 02:31:48 +03:00
|
|
|
* To add a caption to the group, add caption to the first
|
|
|
|
* media in the group and don't add caption for any other.
|
|
|
|
*
|
2021-05-01 00:30:53 +03:00
|
|
|
* @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,
|
2021-06-10 02:31:48 +03:00
|
|
|
medias: (InputMediaLike | string)[],
|
2021-05-01 00:30:53 +03:00
|
|
|
params?: {
|
|
|
|
/**
|
|
|
|
* Message to reply to. Either a message object or message ID.
|
|
|
|
*/
|
|
|
|
replyTo?: number | Message
|
|
|
|
|
2021-05-27 15:57:05 +03:00
|
|
|
/**
|
|
|
|
* Message to comment to. Either a message object or message ID.
|
|
|
|
*
|
|
|
|
* This overwrites `replyTo` if it was passed
|
|
|
|
*/
|
|
|
|
commentTo?: number | Message
|
|
|
|
|
2021-05-01 00:30:53 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
2021-05-25 11:58:14 +03:00
|
|
|
*
|
|
|
|
* You can also pass `0x7FFFFFFE`, this will send the message
|
|
|
|
* once the peer is online
|
2021-05-01 00:30:53 +03:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
}
|
2021-05-16 14:43:23 +03:00
|
|
|
): Promise<Message[]> {
|
2021-05-01 00:30:53 +03:00
|
|
|
if (!params) params = {}
|
|
|
|
|
2021-05-27 15:57:05 +03:00
|
|
|
let peer = await this.resolvePeer(chatId)
|
2021-05-01 00:30:53 +03:00
|
|
|
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
|
|
|
|
|
2021-05-27 15:57:05 +03:00
|
|
|
let replyTo = normalizeMessageId(params.replyTo)
|
|
|
|
if (params.commentTo) {
|
2021-06-06 15:20:41 +03:00
|
|
|
;[peer, replyTo] = await this._getDiscussionMessage(
|
|
|
|
peer,
|
|
|
|
normalizeMessageId(params.commentTo)!
|
|
|
|
)
|
2021-05-27 15:57:05 +03:00
|
|
|
}
|
|
|
|
|
2021-05-01 00:30:53 +03:00
|
|
|
const multiMedia: tl.RawInputSingleMedia[] = []
|
|
|
|
|
|
|
|
for (let i = 0; i < medias.length; i++) {
|
2021-06-10 02:31:48 +03:00
|
|
|
let media = medias[i]
|
|
|
|
|
|
|
|
if (typeof media === 'string') {
|
|
|
|
media = {
|
|
|
|
type: 'auto',
|
|
|
|
file: media,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 00:30:53 +03:00
|
|
|
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,
|
2021-05-27 15:57:05 +03:00
|
|
|
replyToMsgId: replyTo,
|
2021-05-01 00:30:53 +03:00
|
|
|
randomId: randomUlong(),
|
|
|
|
scheduleDate: normalizeDate(params.schedule),
|
|
|
|
replyMarkup,
|
|
|
|
clearDraft: params.clearDraft,
|
|
|
|
})
|
|
|
|
|
2021-05-16 14:43:23 +03:00
|
|
|
assertIsUpdatesGroup('_findMessageInUpdate', res)
|
|
|
|
this._handleUpdate(res, true)
|
|
|
|
|
|
|
|
const { users, chats } = createUsersChatsIndex(res)
|
|
|
|
|
|
|
|
return res.updates
|
|
|
|
.filter(
|
|
|
|
(u) =>
|
|
|
|
u._ === 'updateNewMessage' ||
|
|
|
|
u._ === 'updateNewChannelMessage' ||
|
|
|
|
u._ === 'updateNewScheduledMessage'
|
|
|
|
)
|
|
|
|
.map(
|
|
|
|
(u) =>
|
|
|
|
new Message(
|
|
|
|
this,
|
|
|
|
(u as any).message,
|
|
|
|
users,
|
|
|
|
chats,
|
|
|
|
u._ === 'updateNewScheduledMessage'
|
|
|
|
)
|
|
|
|
)
|
2021-05-01 00:30:53 +03:00
|
|
|
}
|