From cbedc34bf0e42726ec6998351d4883486243fc7f Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 31 Mar 2026 11:29:13 +0200 Subject: [PATCH] Bot Command: Fix always sending command in main thread (#6799) --- src/global/actions/api/bots.ts | 13 ++++++++----- src/global/helpers/messages.ts | 15 +++++++++++++++ src/global/selectors/messages.ts | 12 ++---------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/global/actions/api/bots.ts b/src/global/actions/api/bots.ts index 541d12c4f..1a82bc11d 100644 --- a/src/global/actions/api/bots.ts +++ b/src/global/actions/api/bots.ts @@ -27,9 +27,10 @@ import { debounce } from '../../../util/schedulers'; import { getServerTime } from '../../../util/serverTime'; import { extractCurrentThemeParams } from '../../../util/themeStyle'; import { callApi } from '../../../api/gramjs'; -import { getMainUsername } from '../../helpers'; import { + getMainUsername, getWebAppKey, + prepareMessageReplyInfo, } from '../../helpers'; import { addActionHandler, getGlobal, setGlobal, @@ -247,7 +248,8 @@ addActionHandler('sendBotCommand', (global, actions, payload): ActionReturnType const lastMessageId = selectChatLastMessageId(global, chat.id); void sendBotCommand( - chat, command, selectDraft(global, chat.id, threadId)?.replyInfo, selectSendAs(global, chat.id), lastMessageId, + chat, threadId, command, selectDraft(global, chat.id, threadId)?.replyInfo, selectSendAs(global, chat.id), + lastMessageId, ); }); @@ -270,7 +272,7 @@ addActionHandler('restartBot', async (global, actions, payload): Promise = global = getGlobal(); global = removeBlockedUser(global, bot.id); setGlobal(global); - void sendBotCommand(chat, '/start', undefined, selectSendAs(global, chatId), lastMessageId); + void sendBotCommand(chat, MAIN_THREAD_ID, '/start', undefined, selectSendAs(global, chatId), lastMessageId); }); addActionHandler('loadTopInlineBots', async (global): Promise => { @@ -1304,11 +1306,12 @@ async function searchInlineBot(global: T, { } async function sendBotCommand( - chat: ApiChat, command: string, replyInfo?: ApiInputMessageReplyInfo, sendAs?: ApiPeer, lastMessageId?: number, + chat: ApiChat, threadId: ThreadId, command: string, replyInfo?: ApiInputMessageReplyInfo, + sendAs?: ApiPeer, lastMessageId?: number, ) { await callApi('sendMessage', { chat, - replyInfo, + replyInfo: prepareMessageReplyInfo(threadId, replyInfo), text: command, sendAs, lastMessageId, diff --git a/src/global/helpers/messages.ts b/src/global/helpers/messages.ts index 07b4af041..057cb4dc8 100644 --- a/src/global/helpers/messages.ts +++ b/src/global/helpers/messages.ts @@ -2,6 +2,7 @@ import type { TeactNode } from '../../lib/teact/teact'; import type { ApiAttachment, + ApiInputMessageReplyInfo, ApiMessage, ApiMessageEntityTextUrl, ApiPeer, @@ -573,3 +574,17 @@ export function groupMessageIdsByThreadId( return grouped; } + +export function prepareMessageReplyInfo( + threadId: ThreadId, additionalReplyInfo?: ApiInputMessageReplyInfo, +): ApiInputMessageReplyInfo | undefined { + const isMainThread = threadId === MAIN_THREAD_ID; + if (!additionalReplyInfo && isMainThread) return undefined; + + return { + type: 'message', + ...additionalReplyInfo, + replyToMsgId: additionalReplyInfo?.replyToMsgId || Number(threadId), + replyToTopId: additionalReplyInfo?.replyToTopId || (!isMainThread ? Number(threadId) : undefined), + }; +} diff --git a/src/global/selectors/messages.ts b/src/global/selectors/messages.ts index 3d235c88b..19ece30c9 100644 --- a/src/global/selectors/messages.ts +++ b/src/global/selectors/messages.ts @@ -68,6 +68,7 @@ import { isOwnMessage, isServiceNotificationMessage, isUserRightBanned, + prepareMessageReplyInfo, } from '../helpers'; import { getMessageReplyInfo } from '../helpers/replies'; import { @@ -1446,17 +1447,8 @@ export function selectMessageReplyInfo( ) { const chat = selectChat(global, chatId); if (!chat) return undefined; - const isMainThread = threadId === MAIN_THREAD_ID; - if (!additionalReplyInfo && isMainThread) return undefined; - const replyInfo: ApiInputMessageReplyInfo = { - type: 'message', - ...additionalReplyInfo, - replyToMsgId: additionalReplyInfo?.replyToMsgId || Number(threadId), - replyToTopId: additionalReplyInfo?.replyToTopId || (!isMainThread ? Number(threadId) : undefined), - }; - - return replyInfo; + return prepareMessageReplyInfo(threadId, additionalReplyInfo); } export function selectReplyMessage(global: T, message: ApiMessage) {