Bot Command: Fix always sending command in main thread (#6799)

This commit is contained in:
Alexander Zinchuk 2026-03-31 11:29:13 +02:00
parent b98f790308
commit cbedc34bf0
3 changed files with 25 additions and 15 deletions

View File

@ -27,9 +27,10 @@ import { debounce } from '../../../util/schedulers';
import { getServerTime } from '../../../util/serverTime'; import { getServerTime } from '../../../util/serverTime';
import { extractCurrentThemeParams } from '../../../util/themeStyle'; import { extractCurrentThemeParams } from '../../../util/themeStyle';
import { callApi } from '../../../api/gramjs'; import { callApi } from '../../../api/gramjs';
import { getMainUsername } from '../../helpers';
import { import {
getMainUsername,
getWebAppKey, getWebAppKey,
prepareMessageReplyInfo,
} from '../../helpers'; } from '../../helpers';
import { import {
addActionHandler, getGlobal, setGlobal, addActionHandler, getGlobal, setGlobal,
@ -247,7 +248,8 @@ addActionHandler('sendBotCommand', (global, actions, payload): ActionReturnType
const lastMessageId = selectChatLastMessageId(global, chat.id); const lastMessageId = selectChatLastMessageId(global, chat.id);
void sendBotCommand( 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<void> =
global = getGlobal(); global = getGlobal();
global = removeBlockedUser(global, bot.id); global = removeBlockedUser(global, bot.id);
setGlobal(global); 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<void> => { addActionHandler('loadTopInlineBots', async (global): Promise<void> => {
@ -1304,11 +1306,12 @@ async function searchInlineBot<T extends GlobalState>(global: T, {
} }
async function sendBotCommand( 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', { await callApi('sendMessage', {
chat, chat,
replyInfo, replyInfo: prepareMessageReplyInfo(threadId, replyInfo),
text: command, text: command,
sendAs, sendAs,
lastMessageId, lastMessageId,

View File

@ -2,6 +2,7 @@ import type { TeactNode } from '../../lib/teact/teact';
import type { import type {
ApiAttachment, ApiAttachment,
ApiInputMessageReplyInfo,
ApiMessage, ApiMessage,
ApiMessageEntityTextUrl, ApiMessageEntityTextUrl,
ApiPeer, ApiPeer,
@ -573,3 +574,17 @@ export function groupMessageIdsByThreadId(
return grouped; 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),
};
}

View File

@ -68,6 +68,7 @@ import {
isOwnMessage, isOwnMessage,
isServiceNotificationMessage, isServiceNotificationMessage,
isUserRightBanned, isUserRightBanned,
prepareMessageReplyInfo,
} from '../helpers'; } from '../helpers';
import { getMessageReplyInfo } from '../helpers/replies'; import { getMessageReplyInfo } from '../helpers/replies';
import { import {
@ -1446,17 +1447,8 @@ export function selectMessageReplyInfo<T extends GlobalState>(
) { ) {
const chat = selectChat(global, chatId); const chat = selectChat(global, chatId);
if (!chat) return undefined; if (!chat) return undefined;
const isMainThread = threadId === MAIN_THREAD_ID;
if (!additionalReplyInfo && isMainThread) return undefined;
const replyInfo: ApiInputMessageReplyInfo = { return prepareMessageReplyInfo(threadId, additionalReplyInfo);
type: 'message',
...additionalReplyInfo,
replyToMsgId: additionalReplyInfo?.replyToMsgId || Number(threadId),
replyToTopId: additionalReplyInfo?.replyToTopId || (!isMainThread ? Number(threadId) : undefined),
};
return replyInfo;
} }
export function selectReplyMessage<T extends GlobalState>(global: T, message: ApiMessage) { export function selectReplyMessage<T extends GlobalState>(global: T, message: ApiMessage) {