feat(client): editMessage method

also .edit and .editText bound methods for Message
This commit is contained in:
teidesu 2021-04-09 23:19:58 +03:00
parent e17ab84c37
commit 465a875460
4 changed files with 159 additions and 5 deletions

View file

@ -19,6 +19,7 @@ import { downloadAsIterable } from './methods/files/download-iterable'
import { downloadAsStream } from './methods/files/download-stream' import { downloadAsStream } from './methods/files/download-stream'
import { uploadFile } from './methods/files/upload-file' import { uploadFile } from './methods/files/upload-file'
import { deleteMessages } from './methods/messages/delete-messages' import { deleteMessages } from './methods/messages/delete-messages'
import { editMessage } from './methods/messages/edit-message'
import { _findMessageInUpdate } from './methods/messages/find-in-update' import { _findMessageInUpdate } from './methods/messages/find-in-update'
import { getHistory } from './methods/messages/get-history' import { getHistory } from './methods/messages/get-history'
import { getMessages } from './methods/messages/get-messages' import { getMessages } from './methods/messages/get-messages'
@ -435,8 +436,57 @@ export class TelegramClient extends BaseTelegramClient {
): Promise<boolean> { ): Promise<boolean> {
return deleteMessages.apply(this, arguments) return deleteMessages.apply(this, arguments)
} }
/**
* Edit message text and/or reply markup.
*
* @param chatId ID of the chat, its username, phone or `"me"` or `"self"`
* @param message Message or its ID
* @param params
*/
editMessage(
chatId: InputPeerLike,
message: number | Message,
params: {
/**
* New message text
*/
text?: string
protected _findMessageInUpdate(res: tl.TypeUpdates): 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
/**
* List of formatting entities to use instead of parsing via a
* parse mode.
*
* **Note:** Passing this makes the method ignore {@link parseMode}
*/
entities?: tl.TypeMessageEntity[]
/**
* Whether to disable links preview in this message
*/
disableWebPreview?: boolean
/**
* For bots: inline or reply markup or an instruction
* to hide a reply keyboard or to force a reply.
*/
replyMarkup?: ReplyMarkup
}
): Promise<Message> {
return editMessage.apply(this, arguments)
}
protected _findMessageInUpdate(
res: tl.TypeUpdates,
isEdit = false
): Message {
return _findMessageInUpdate.apply(this, arguments) return _findMessageInUpdate.apply(this, arguments)
} }
/** /**

View file

@ -0,0 +1,69 @@
import { TelegramClient } from '../../client'
import { BotKeyboard, InputPeerLike, Message, ReplyMarkup } from '../../types'
import { tl } from '@mtcute/tl'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Edit message text and/or reply markup.
*
* @param chatId ID of the chat, its username, phone or `"me"` or `"self"`
* @param message Message or its ID
* @param params
* @internal
*/
export async function editMessage(
this: TelegramClient,
chatId: InputPeerLike,
message: number | Message,
params: {
/**
* New message text
*/
text?: string
/**
* 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
/**
* List of formatting entities to use instead of parsing via a
* parse mode.
*
* **Note:** Passing this makes the method ignore {@link parseMode}
*/
entities?: tl.TypeMessageEntity[]
/**
* Whether to disable links preview in this message
*/
disableWebPreview?: boolean
/**
* For bots: inline or reply markup or an instruction
* to hide a reply keyboard or to force a reply.
*/
replyMarkup?: ReplyMarkup
}
): Promise<Message> {
const [content, entities] = await this._parseEntities(
params.text,
params.parseMode,
params.entities
)
const res = await this.call({
_: 'messages.editMessage',
id: typeof message === 'number' ? message : message.id,
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
noWebpage: params.disableWebPreview,
replyMarkup: BotKeyboard._convertToTl(params.replyMarkup),
message: content,
entities,
})
return this._findMessageInUpdate(res, true) as any
}

View file

@ -6,7 +6,8 @@ import { createUsersChatsIndex } from '../../utils/peer-utils'
/** @internal */ /** @internal */
export function _findMessageInUpdate( export function _findMessageInUpdate(
this: TelegramClient, this: TelegramClient,
res: tl.TypeUpdates res: tl.TypeUpdates,
isEdit = false
): Message { ): Message {
if (!(res._ === 'updates' || res._ === 'updatesCombined')) if (!(res._ === 'updates' || res._ === 'updatesCombined'))
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
@ -17,9 +18,14 @@ export function _findMessageInUpdate(
for (const u of res.updates) { for (const u of res.updates) {
if ( if (
u._ === 'updateNewMessage' || isEdit && (
u._ === 'updateNewChannelMessage' || u._ === 'updateEditMessage' ||
u._ === 'updateNewScheduledMessage' u._ === 'updateEditChannelMessage'
) || !isEdit && (
u._ === 'updateNewMessage' ||
u._ === 'updateNewChannelMessage' ||
u._ === 'updateNewScheduledMessage'
)
) { ) {
const { users, chats } = createUsersChatsIndex(res) const { users, chats } = createUsersChatsIndex(res)

View file

@ -802,6 +802,35 @@ export class Message {
delete(revoke = false): Promise<boolean> { delete(revoke = false): Promise<boolean> {
return this.client.deleteMessages(this.chat.inputPeer, this.id, revoke) return this.client.deleteMessages(this.chat.inputPeer, this.id, revoke)
} }
/**
* Edit this message's text and/or reply markup
*
* @see TelegramClient.editMessage
*/
edit(params: Parameters<TelegramClient['editMessage']>[2]): Promise<Message> {
return this.client.editMessage(this.chat.inputPeer, this.id, params)
}
/**
* Edit message text and optionally reply markup.
*
* Convenience method that just wraps {@link edit},
* passing positional `text` as object field.
*
* @param text New message text
* @param params Additional parameters
* @see TelegramClient.editMessage
*/
editText(
text: string,
params?: Omit<Parameters<TelegramClient['editMessage']>[2], 'text'>
): Promise<Message> {
return this.edit({
text,
...(params || {})
})
}
} }
makeInspectable(Message, ['empty', 'isScheduled'], ['link']) makeInspectable(Message, ['empty', 'isScheduled'], ['link'])