diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 87553c41..9836f768 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -101,6 +101,7 @@ import { getHistory } from './methods/messages/get-history' import { getMessageGroup } from './methods/messages/get-message-group' import { getMessagesUnsafe } from './methods/messages/get-messages-unsafe' import { getMessages } from './methods/messages/get-messages' +import { getScheduledMessages } from './methods/messages/get-scheduled-messages' import { iterHistory } from './methods/messages/iter-history' import { _normalizeInline } from './methods/messages/normalize-inline' import { _parseEntities } from './methods/messages/parse-entities' @@ -111,6 +112,7 @@ import { searchMessages } from './methods/messages/search-messages' import { sendCopy } from './methods/messages/send-copy' import { sendMediaGroup } from './methods/messages/send-media-group' import { sendMedia } from './methods/messages/send-media' +import { sendScheduled } from './methods/messages/send-scheduled' import { sendText } from './methods/messages/send-text' import { sendTyping } from './methods/messages/send-typing' import { sendVote } from './methods/messages/send-vote' @@ -2183,6 +2185,29 @@ export interface TelegramClient extends BaseTelegramClient { messageIds: number[], fromReply?: boolean ): Promise<(Message | null)[]> + /** + * Get a single scheduled message in chat by its ID + * + * @param chatId Chat's marked ID, its username, phone or `"me"` or `"self"` + * @param messageId Scheduled message ID + */ + getScheduledMessages( + chatId: InputPeerLike, + messageId: number + ): Promise + /** + * Get scheduled messages in chat by their IDs + * + * Fot messages that were not found, `null` will be + * returned at that position. + * + * @param chatId Chat's marked ID, its username, phone or `"me"` or `"self"` + * @param messageIds Scheduled messages IDs + */ + getScheduledMessages( + chatId: InputPeerLike, + messageIds: number[] + ): Promise<(Message | null)[]> /** * Iterate through a chat history sequentially. * @@ -2643,6 +2668,28 @@ export interface TelegramClient extends BaseTelegramClient { clearDraft?: boolean } ): Promise + /** + * Send s previously scheduled message. + * + * Note that if the message belongs to a media group, + * the entire group will be sent, but only + * the first message will be returned (in this overload). + * + * @param peer Chat where the messages were scheduled + * @param id ID of the message + */ + sendScheduled(peer: InputPeerLike, id: number): Promise + /** + * Send previously scheduled message(s) + * + * Note that if the message belongs to a media group, + * the entire group will be sent, and all the messages + * will be returned. + * + * @param peer Chat where the messages were scheduled + * @param ids ID(s) of the messages + */ + sendScheduled(peer: InputPeerLike, ids: number[]): Promise /** * Send a text message * @@ -3427,6 +3474,7 @@ export class TelegramClient extends BaseTelegramClient { getMessageGroup = getMessageGroup getMessagesUnsafe = getMessagesUnsafe getMessages = getMessages + getScheduledMessages = getScheduledMessages iterHistory = iterHistory protected _normalizeInline = _normalizeInline protected _parseEntities = _parseEntities @@ -3437,6 +3485,7 @@ export class TelegramClient extends BaseTelegramClient { sendCopy = sendCopy sendMediaGroup = sendMediaGroup sendMedia = sendMedia + sendScheduled = sendScheduled sendText = sendText sendTyping = sendTyping sendVote = sendVote diff --git a/packages/client/src/methods/messages/get-scheduled-messages.ts b/packages/client/src/methods/messages/get-scheduled-messages.ts new file mode 100644 index 00000000..d18958c3 --- /dev/null +++ b/packages/client/src/methods/messages/get-scheduled-messages.ts @@ -0,0 +1,72 @@ +import { TelegramClient } from '../../client' +import { MaybeArray } from '@mtcute/core' +import { + createUsersChatsIndex, + isInputPeerChannel, + normalizeToInputChannel, +} from '../../utils/peer-utils' +import { tl } from '@mtcute/tl' +import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types' + +/** + * Get a single scheduled message in chat by its ID + * + * @param chatId Chat's marked ID, its username, phone or `"me"` or `"self"` + * @param messageId Scheduled message ID + * @internal + */ +export async function getScheduledMessages( + this: TelegramClient, + chatId: InputPeerLike, + messageId: number +): Promise +/** + * Get scheduled messages in chat by their IDs + * + * Fot messages that were not found, `null` will be + * returned at that position. + * + * @param chatId Chat's marked ID, its username, phone or `"me"` or `"self"` + * @param messageIds Scheduled messages IDs + * @internal + */ +export async function getScheduledMessages( + this: TelegramClient, + chatId: InputPeerLike, + messageIds: number[] +): Promise<(Message | null)[]> + +/** @internal */ +export async function getScheduledMessages( + this: TelegramClient, + chatId: InputPeerLike, + messageIds: MaybeArray +): Promise> { + const peer = await this.resolvePeer(chatId) + + const isSingle = !Array.isArray(messageIds) + if (isSingle) messageIds = [messageIds as number] + + const res = await this.call({ + _: 'messages.getScheduledMessages', + peer, + id: messageIds as number[], + }) + + if (res._ === 'messages.messagesNotModified') + throw new MtCuteTypeAssertionError( + 'getMessages', + '!messages.messagesNotModified', + res._ + ) + + const { users, chats } = createUsersChatsIndex(res) + + const ret = res.messages.map((msg) => { + if (msg._ === 'messageEmpty') return null + + return new Message(this, msg, users, chats) + }) + + return isSingle ? ret[0] : ret +} diff --git a/packages/client/src/methods/messages/send-scheduled.ts b/packages/client/src/methods/messages/send-scheduled.ts new file mode 100644 index 00000000..8e475a82 --- /dev/null +++ b/packages/client/src/methods/messages/send-scheduled.ts @@ -0,0 +1,78 @@ +import { InputPeerLike, Message } from '../../types' +import { MaybeArray } from '@mtcute/core' +import { TelegramClient } from '../../client' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' +import { createUsersChatsIndex } from '../../utils/peer-utils' + +/** + * Send s previously scheduled message. + * + * Note that if the message belongs to a media group, + * the entire group will be sent, but only + * the first message will be returned (in this overload). + * + * @param peer Chat where the messages were scheduled + * @param id ID of the message + * @internal + */ +export async function sendScheduled( + this: TelegramClient, + peer: InputPeerLike, + id: number +): Promise + +/** + * Send previously scheduled message(s) + * + * Note that if the message belongs to a media group, + * the entire group will be sent, and all the messages + * will be returned. + * + * @param peer Chat where the messages were scheduled + * @param ids ID(s) of the messages + * @internal + */ +export async function sendScheduled( + this: TelegramClient, + peer: InputPeerLike, + ids: number[] +): Promise + +/** @internal */ +export async function sendScheduled( + this: TelegramClient, + peer: InputPeerLike, + ids: MaybeArray +): Promise> { + const isSingle = !Array.isArray(ids) + if (isSingle) ids = [ids as number] + + const res = await this.call({ + _: 'messages.sendScheduledMessages', + peer: await this.resolvePeer(peer), + id: (ids as number[]) + }) + + assertIsUpdatesGroup('sendScheduled', res) + this._handleUpdate(res, true) + + const { users, chats } = createUsersChatsIndex(res) + + const msgs = res.updates + .filter( + (u) => + u._ === 'updateNewMessage' || + u._ === 'updateNewChannelMessage' + ) + .map( + (u) => + new Message( + this, + (u as any).message, + users, + chats + ) + ) + + return isSingle ? msgs[0] : msgs +}