refactor(client): moved handling of inline messages to a separate method
This commit is contained in:
parent
f45b602423
commit
b29883116b
3 changed files with 43 additions and 31 deletions
|
@ -86,6 +86,7 @@ import { getHistory } from './methods/messages/get-history'
|
||||||
import { getMessageGroup } from './methods/messages/get-message-group'
|
import { getMessageGroup } from './methods/messages/get-message-group'
|
||||||
import { getMessages } from './methods/messages/get-messages'
|
import { getMessages } from './methods/messages/get-messages'
|
||||||
import { iterHistory } from './methods/messages/iter-history'
|
import { iterHistory } from './methods/messages/iter-history'
|
||||||
|
import { _normalizeInline } from './methods/messages/normalize-inline'
|
||||||
import { _parseEntities } from './methods/messages/parse-entities'
|
import { _parseEntities } from './methods/messages/parse-entities'
|
||||||
import { pinMessage } from './methods/messages/pin-message'
|
import { pinMessage } from './methods/messages/pin-message'
|
||||||
import { readHistory } from './methods/messages/read-history'
|
import { readHistory } from './methods/messages/read-history'
|
||||||
|
@ -1546,13 +1547,13 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
/**
|
/**
|
||||||
* Edit sent inline message text, media and reply markup.
|
* Edit sent inline message text, media and reply markup.
|
||||||
*
|
*
|
||||||
* @param id
|
* @param messageId
|
||||||
* Inline message ID, either as a TL object, or as a
|
* Inline message ID, either as a TL object, or as a
|
||||||
* TDLib and Bot API compatible string
|
* TDLib and Bot API compatible string
|
||||||
* @param params
|
* @param params
|
||||||
*/
|
*/
|
||||||
editInlineMessage(
|
editInlineMessage(
|
||||||
id: tl.TypeInputBotInlineMessageID | string,
|
messageId: tl.TypeInputBotInlineMessageID | string,
|
||||||
params: {
|
params: {
|
||||||
/**
|
/**
|
||||||
* New message text
|
* New message text
|
||||||
|
@ -2052,7 +2053,6 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
* @param fromChatId Target chat ID
|
* @param fromChatId Target chat ID
|
||||||
* @param message Message ID to forward
|
* @param message Message ID to forward
|
||||||
* @param params
|
* @param params
|
||||||
|
|
||||||
*/
|
*/
|
||||||
sendCopy(
|
sendCopy(
|
||||||
toChatId: InputPeerLike,
|
toChatId: InputPeerLike,
|
||||||
|
@ -2244,7 +2244,6 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
* @param chatId ID of the chat, its username, phone or `"me"` or `"self"`
|
* @param chatId ID of the chat, its username, phone or `"me"` or `"self"`
|
||||||
* @param text Text of the message
|
* @param text Text of the message
|
||||||
* @param params Additional sending parameters
|
* @param params Additional sending parameters
|
||||||
|
|
||||||
*/
|
*/
|
||||||
sendText(
|
sendText(
|
||||||
chatId: InputPeerLike,
|
chatId: InputPeerLike,
|
||||||
|
@ -2949,6 +2948,7 @@ export class TelegramClient extends BaseTelegramClient {
|
||||||
getMessageGroup = getMessageGroup
|
getMessageGroup = getMessageGroup
|
||||||
getMessages = getMessages
|
getMessages = getMessages
|
||||||
iterHistory = iterHistory
|
iterHistory = iterHistory
|
||||||
|
protected _normalizeInline = _normalizeInline
|
||||||
protected _parseEntities = _parseEntities
|
protected _parseEntities = _parseEntities
|
||||||
pinMessage = pinMessage
|
pinMessage = pinMessage
|
||||||
readHistory = readHistory
|
readHistory = readHistory
|
||||||
|
|
|
@ -1,23 +1,11 @@
|
||||||
import { TelegramClient } from '../../client'
|
import { TelegramClient } from '../../client'
|
||||||
import { BotKeyboard, InputMediaLike, ReplyMarkup } from '../../types'
|
import { BotKeyboard, InputMediaLike, ReplyMarkup } from '../../types'
|
||||||
import { tl } from '@mtcute/tl'
|
import { tl } from '@mtcute/tl'
|
||||||
import { parseInlineMessageId } from '../../utils/inline-utils'
|
|
||||||
import { TelegramConnection } from '@mtcute/core'
|
|
||||||
|
|
||||||
// @extension
|
|
||||||
interface EditInlineExtension {
|
|
||||||
_connectionsForInline: Record<number, TelegramConnection>
|
|
||||||
}
|
|
||||||
|
|
||||||
// @initialize
|
|
||||||
function _initializeEditInline(this: TelegramClient) {
|
|
||||||
this._connectionsForInline = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Edit sent inline message text, media and reply markup.
|
* Edit sent inline message text, media and reply markup.
|
||||||
*
|
*
|
||||||
* @param id
|
* @param messageId
|
||||||
* Inline message ID, either as a TL object, or as a
|
* Inline message ID, either as a TL object, or as a
|
||||||
* TDLib and Bot API compatible string
|
* TDLib and Bot API compatible string
|
||||||
* @param params
|
* @param params
|
||||||
|
@ -25,7 +13,7 @@ function _initializeEditInline(this: TelegramClient) {
|
||||||
*/
|
*/
|
||||||
export async function editInlineMessage(
|
export async function editInlineMessage(
|
||||||
this: TelegramClient,
|
this: TelegramClient,
|
||||||
id: tl.TypeInputBotInlineMessageID | string,
|
messageId: tl.TypeInputBotInlineMessageID | string,
|
||||||
params: {
|
params: {
|
||||||
/**
|
/**
|
||||||
* New message text
|
* New message text
|
||||||
|
@ -81,19 +69,7 @@ export async function editInlineMessage(
|
||||||
let entities: tl.TypeMessageEntity[] | undefined
|
let entities: tl.TypeMessageEntity[] | undefined
|
||||||
let media: tl.TypeInputMedia | undefined = undefined
|
let media: tl.TypeInputMedia | undefined = undefined
|
||||||
|
|
||||||
if (typeof id === 'string') {
|
const [id, connection] = await this._normalizeInline(messageId)
|
||||||
id = parseInlineMessageId(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
let connection = this.primaryConnection
|
|
||||||
if (id.dcId !== connection.params.dc.id) {
|
|
||||||
if (!(id.dcId in this._connectionsForInline)) {
|
|
||||||
this._connectionsForInline[
|
|
||||||
id.dcId
|
|
||||||
] = await this.createAdditionalConnection(id.dcId)
|
|
||||||
}
|
|
||||||
connection = this._connectionsForInline[id.dcId]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.media) {
|
if (params.media) {
|
||||||
media = await this._normalizeInputMedia(params.media, params, true)
|
media = await this._normalizeInputMedia(params.media, params, true)
|
||||||
|
|
36
packages/client/src/methods/messages/normalize-inline.ts
Normal file
36
packages/client/src/methods/messages/normalize-inline.ts
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import { TelegramClient } from '../../client'
|
||||||
|
import { tl } from '@mtcute/tl'
|
||||||
|
import { TelegramConnection } from '@mtcute/core'
|
||||||
|
import { parseInlineMessageId } from '../../utils/inline-utils'
|
||||||
|
|
||||||
|
// @extension
|
||||||
|
interface InlineExtension {
|
||||||
|
_connectionsForInline: Record<number, TelegramConnection>
|
||||||
|
}
|
||||||
|
|
||||||
|
// @initialize
|
||||||
|
function _initializeInline(this: TelegramClient) {
|
||||||
|
this._connectionsForInline = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
export async function _normalizeInline(
|
||||||
|
this: TelegramClient,
|
||||||
|
id: string | tl.TypeInputBotInlineMessageID
|
||||||
|
): Promise<[tl.TypeInputBotInlineMessageID, TelegramConnection]> {
|
||||||
|
if (typeof id === 'string') {
|
||||||
|
id = parseInlineMessageId(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
let connection = this.primaryConnection
|
||||||
|
if (id.dcId !== connection.params.dc.id) {
|
||||||
|
if (!(id.dcId in this._connectionsForInline)) {
|
||||||
|
this._connectionsForInline[
|
||||||
|
id.dcId
|
||||||
|
] = await this.createAdditionalConnection(id.dcId)
|
||||||
|
}
|
||||||
|
connection = this._connectionsForInline[id.dcId]
|
||||||
|
}
|
||||||
|
|
||||||
|
return [id, connection]
|
||||||
|
}
|
Loading…
Reference in a new issue