feat(core): basic support for quick replies

This commit is contained in:
alina 🌸 2024-05-05 18:56:44 +03:00
parent de8033e6d2
commit a414ea9425
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
7 changed files with 71 additions and 5 deletions

View file

@ -63,9 +63,12 @@ export function _findMessageInUpdate(
!(
u._ === 'updateNewMessage' ||
u._ === 'updateNewChannelMessage' ||
u._ === 'updateNewScheduledMessage'
u._ === 'updateNewScheduledMessage' ||
u._ === 'updateQuickReplyMessage'
)
) { continue }
) {
continue
}
}
// this *may* break if updateMessageID comes after the message update

View file

@ -5,6 +5,7 @@ import { InputPeerLike, Message, PeersIndex } from '../../types/index.js'
import { assertIsUpdatesGroup } from '../../updates/utils.js'
import { normalizeDate } from '../../utils/misc-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { _normalizeQuickReplyShortcut } from './send-common.js'
// @exported
export interface ForwardMessageOptions {
@ -56,6 +57,12 @@ export interface ForwardMessageOptions {
*/
sendAs?: InputPeerLike
/**
* If passed, instead of sending the message, it will be saved into the
* given quick reply shortcut (either its ID or its shortcut string).
*/
quickReply?: number | string
/**
* Whether to dispatch the forwarded messages
* to the client's update handler.
@ -105,6 +112,7 @@ export async function forwardMessagesById(
dropMediaCaptions: noCaption,
noforwards: forbidForwards,
sendAs: sendAs ? await resolvePeer(client, sendAs) : undefined,
quickReplyShortcut: _normalizeQuickReplyShortcut(params.quickReply),
})
assertIsUpdatesGroup('messages.forwardMessages', res)

View file

@ -100,6 +100,12 @@ export interface CommonSendParams {
*/
sendAs?: InputPeerLike
/**
* If passed, instead of sending the message, it will be saved into the
* given quick reply shortcut (either its ID or its shortcut string).
*/
quickReply?: number | string
/**
* Whether to dispatch the returned message
* to the client's update handler.
@ -107,6 +113,28 @@ export interface CommonSendParams {
shouldDispatch?: true
}
/**
* @internal
* @noemit
*/
export function _normalizeQuickReplyShortcut(
shortcut: number | string | undefined,
): tl.TypeInputQuickReplyShortcut | undefined {
if (!shortcut) return undefined
if (typeof shortcut === 'number') {
return {
_: 'inputQuickReplyShortcutId',
shortcutId: shortcut,
}
}
return {
_: 'inputQuickReplyShortcut',
shortcut,
}
}
/**
* @internal
* @noemit
@ -172,6 +200,7 @@ export async function _processCommonSendParameters(
peer,
replyTo: tlReplyTo,
scheduleDate,
quickReplyShortcut: _normalizeQuickReplyShortcut(params.quickReply),
chainId: _getPeerChainId(client, peer, 'send'),
}
}

View file

@ -50,7 +50,11 @@ export async function sendMediaGroup(
): Promise<Message[]> {
if (!params) params = {}
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const { peer, replyTo, scheduleDate, chainId, quickReplyShortcut } = await _processCommonSendParameters(
client,
chatId,
params,
)
const multiMedia: tl.RawInputSingleMedia[] = []
@ -105,6 +109,7 @@ export async function sendMediaGroup(
noforwards: params.forbidForwards,
sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined,
invertMedia: params.invertMedia,
quickReplyShortcut,
},
{ chainId },
)

View file

@ -80,7 +80,11 @@ export async function sendMedia(
)
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const { peer, replyTo, scheduleDate, chainId, quickReplyShortcut } = await _processCommonSendParameters(
client,
chatId,
params,
)
const randomId = randomLong()
const res = await client.call(
@ -99,6 +103,7 @@ export async function sendMedia(
noforwards: params.forbidForwards,
sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined,
invertMedia: params.invert,
quickReplyShortcut,
},
{ chainId },
)

View file

@ -54,7 +54,11 @@ export async function sendText(
const [message, entities] = await _normalizeInputText(client, text)
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const { peer, replyTo, scheduleDate, chainId, quickReplyShortcut } = await _processCommonSendParameters(
client,
chatId,
params,
)
const randomId = randomLong()
const res = await client.call(
@ -73,6 +77,7 @@ export async function sendText(
noforwards: params.forbidForwards,
sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined,
invertMedia: params.invertMedia,
quickReplyShortcut,
},
{ chainId },
)

View file

@ -213,6 +213,17 @@ export class Message {
return this.raw.mentioned!
}
/**
* If non-null, this message is not actually sent, and is
* instead inside a group of "quick reply" messages
* under the given shortcut ID
*/
get quickReplyShortcutId(): number | null {
if (this.raw._ === 'messageService') return null
return this.raw.quickReplyShortcutId ?? null
}
/**
* If this message is generated from an inline query,
* information about the bot which generated it