feat(client): sendScheduled and getScheduledMessages methods
This commit is contained in:
parent
a33548e790
commit
ca1e5c9efa
3 changed files with 199 additions and 0 deletions
|
@ -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<Message | null>
|
||||
/**
|
||||
* 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<Message>
|
||||
/**
|
||||
* 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<Message>
|
||||
/**
|
||||
* 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<Message[]>
|
||||
/**
|
||||
* 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
|
||||
|
|
|
@ -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<Message | null>
|
||||
/**
|
||||
* 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<number>
|
||||
): Promise<MaybeArray<Message | null>> {
|
||||
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
|
||||
}
|
78
packages/client/src/methods/messages/send-scheduled.ts
Normal file
78
packages/client/src/methods/messages/send-scheduled.ts
Normal file
|
@ -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<Message>
|
||||
|
||||
/**
|
||||
* 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<Message[]>
|
||||
|
||||
/** @internal */
|
||||
export async function sendScheduled(
|
||||
this: TelegramClient,
|
||||
peer: InputPeerLike,
|
||||
ids: MaybeArray<number>
|
||||
): Promise<MaybeArray<Message>> {
|
||||
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
|
||||
}
|
Loading…
Reference in a new issue