fix(telegram): keep bot reply answers anchored to current message (#90475)

Co-authored-by: Moeed Ahmed <moeedahmed@Moeed-Mac-mini.local>
This commit is contained in:
Moeed Ahmed
2026-06-22 20:17:07 +01:00
committed by GitHub
parent c4cac33af6
commit 5cafe4b0cf
2 changed files with 40 additions and 2 deletions
@@ -1284,6 +1284,39 @@ describe("dispatchTelegramMessage draft streaming", () => {
expectRecordFields((delivery.replies as Array<unknown>)[0], { replyToId: "9001" });
});
it("keeps bot-reply answers anchored to the current user message", async () => {
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ dispatcherOptions }) => {
await dispatcherOptions.deliver({ text: "Hello", replyToId: "1001" }, { kind: "final" });
return { queuedFinal: true };
});
deliverReplies.mockResolvedValue({ delivered: true });
await dispatchWithContext({
context: createContext({
msg: {
message_id: 1001,
reply_to_message: {
message_id: 9001,
from: { is_bot: true },
},
} as unknown as TelegramMessageContext["msg"],
ctxPayload: {
MessageSid: "1001",
ReplyToId: "9001",
ReplyToBody: "quoted bot reply",
ReplyToQuoteText: " quoted bot reply\n",
ReplyToIsQuote: true,
} as unknown as TelegramMessageContext["ctxPayload"],
}),
});
const delivery = expectDeliverRepliesParams({
replyQuoteMessageId: 9001,
replyQuoteText: " quoted bot reply\n",
});
expectRecordFields((delivery.replies as Array<unknown>)[0], { replyToId: "1001" });
});
it("keeps answer draft stream for current message replies with native quote candidates", async () => {
const draftStream = createDraftStream();
createTelegramDraftStream.mockReturnValue(draftStream);
@@ -947,6 +947,7 @@ export const dispatchTelegramMessage = async ({
replyQuoteText && !ctxPayload.ReplyToIsExternal
? resolveTelegramReplyId(ctxPayload.ReplyToId)
: undefined;
const replyQuoteTargetsBotMessage = msg.reply_to_message?.from?.is_bot === true;
const replyQuoteByMessageId: TelegramNativeQuoteCandidateByMessageId = {};
if (replyToMode !== "off") {
if (replyQuoteText && replyQuoteMessageId != null) {
@@ -992,7 +993,9 @@ export const dispatchTelegramMessage = async ({
!isRoomEvent && streamReasoningDraft && !streamReasoningInProgressDraft;
const draftReplyToMessageId =
replyToMode !== "off" && typeof msg.message_id === "number"
? (replyQuoteMessageId ?? msg.message_id)
? replyQuoteTargetsBotMessage
? msg.message_id
: (replyQuoteMessageId ?? msg.message_id)
: undefined;
const draftMinInitialChars = streamMode === "progress" ? 0 : DRAFT_MIN_INITIAL_CHARS;
const progressSeed = `${route.accountId}:${chatId}:${threadSpec.id ?? ""}`;
@@ -1448,7 +1451,9 @@ export const dispatchTelegramMessage = async ({
});
const implicitQuoteReplyTargetId =
replyQuoteMessageId != null ? String(replyQuoteMessageId) : undefined;
!replyQuoteTargetsBotMessage && replyQuoteMessageId != null
? String(replyQuoteMessageId)
: undefined;
const currentMessageIdForQuoteReply =
implicitQuoteReplyTargetId && ctxPayload.MessageSid ? ctxPayload.MessageSid : undefined;
const replyQuotePosition =