Skip to content

Commit 9667ece

Browse files
Moeed Ahmedmoeedahmed
authored andcommitted
fix(telegram): keep bot reply answers anchored to current message
1 parent 3ce3ed6 commit 9667ece

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎extensions/telegram/src/bot-message-dispatch.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,39 @@ describe("dispatchTelegramMessage draft streaming", () => {
11361136
expectRecordFields((delivery.replies as Array<unknown>)[0], { replyToId: "9001" });
11371137
});
11381138

1139+
it("keeps bot-reply answers anchored to the current user message", async () => {
1140+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ dispatcherOptions }) => {
1141+
await dispatcherOptions.deliver({ text: "Hello", replyToId: "1001" }, { kind: "final" });
1142+
return { queuedFinal: true };
1143+
});
1144+
deliverReplies.mockResolvedValue({ delivered: true });
1145+
1146+
await dispatchWithContext({
1147+
context: createContext({
1148+
msg: {
1149+
message_id: 1001,
1150+
reply_to_message: {
1151+
message_id: 9001,
1152+
from: { is_bot: true },
1153+
},
1154+
} as unknown as TelegramMessageContext["msg"],
1155+
ctxPayload: {
1156+
MessageSid: "1001",
1157+
ReplyToId: "9001",
1158+
ReplyToBody: "quoted bot reply",
1159+
ReplyToQuoteText: " quoted bot reply\n",
1160+
ReplyToIsQuote: true,
1161+
} as unknown as TelegramMessageContext["ctxPayload"],
1162+
}),
1163+
});
1164+
1165+
const delivery = expectDeliverRepliesParams({
1166+
replyQuoteMessageId: 9001,
1167+
replyQuoteText: " quoted bot reply\n",
1168+
});
1169+
expectRecordFields((delivery.replies as Array<unknown>)[0], { replyToId: "1001" });
1170+
});
1171+
11391172
it("keeps answer draft stream for current message replies with native quote candidates", async () => {
11401173
const draftStream = createDraftStream();
11411174
createTelegramDraftStream.mockReturnValue(draftStream);

‎extensions/telegram/src/bot-message-dispatch.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ export const dispatchTelegramMessage = async ({
912912
replyQuoteText && !ctxPayload.ReplyToIsExternal
913913
? resolveTelegramReplyId(ctxPayload.ReplyToId)
914914
: undefined;
915+
const replyQuoteTargetsBotMessage = msg.reply_to_message?.from?.is_bot === true;
915916
const replyQuoteByMessageId: TelegramNativeQuoteCandidateByMessageId = {};
916917
if (replyToMode !== "off") {
917918
if (replyQuoteText && replyQuoteMessageId != null) {
@@ -957,7 +958,9 @@ export const dispatchTelegramMessage = async ({
957958
!isRoomEvent && streamReasoningDraft && !streamReasoningInProgressDraft;
958959
const draftReplyToMessageId =
959960
replyToMode !== "off" && typeof msg.message_id === "number"
960-
? (replyQuoteMessageId ?? msg.message_id)
961+
? replyQuoteTargetsBotMessage
962+
? msg.message_id
963+
: (replyQuoteMessageId ?? msg.message_id)
961964
: undefined;
962965
const draftMinInitialChars = streamMode === "progress" ? 0 : DRAFT_MIN_INITIAL_CHARS;
963966
const progressSeed = `${route.accountId}:${chatId}:${threadSpec.id ?? ""}`;
@@ -1405,7 +1408,9 @@ export const dispatchTelegramMessage = async ({
14051408
});
14061409

14071410
const implicitQuoteReplyTargetId =
1408-
replyQuoteMessageId != null ? String(replyQuoteMessageId) : undefined;
1411+
!replyQuoteTargetsBotMessage && replyQuoteMessageId != null
1412+
? String(replyQuoteMessageId)
1413+
: undefined;
14091414
const currentMessageIdForQuoteReply =
14101415
implicitQuoteReplyTargetId && ctxPayload.MessageSid ? ctxPayload.MessageSid : undefined;
14111416
const replyQuotePosition =

0 commit comments

Comments
 (0)