Skip to content

Commit 2cdee08

Browse files
committed
fix(telegram): ignore commands for other bots
1 parent 8075063 commit 2cdee08

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎extensions/telegram/src/bot-message-context.body.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,51 @@ describe("resolveTelegramInboundBody", () => {
494494
expect(result?.effectiveWasMentioned).toBe(true);
495495
});
496496

497+
it("ignores leading commands addressed to another bot when mentions are optional", async () => {
498+
const logger = createLogger();
499+
const command = "/status@other_bot";
500+
const result = await resolveGroup({
501+
logger,
502+
message: {
503+
text: command,
504+
entities: [{ type: "bot_command", offset: 0, length: command.length }],
505+
},
506+
overrides: {
507+
groupConfig: { requireMention: false } as never,
508+
requireMention: false,
509+
},
510+
});
511+
512+
expect(result).toBeNull();
513+
});
514+
515+
it("keeps this bot's leading command when a later command targets another bot", async () => {
516+
const logger = createLogger();
517+
const ownCommand = "/inspect@bot";
518+
const otherCommand = "/weather@other_bot";
519+
const text = `${ownCommand} ${otherCommand}`;
520+
const result = await resolveGroup({
521+
logger,
522+
message: {
523+
text,
524+
entities: [
525+
{ type: "bot_command", offset: 0, length: ownCommand.length },
526+
{
527+
type: "bot_command",
528+
offset: ownCommand.length + 1,
529+
length: otherCommand.length,
530+
},
531+
],
532+
},
533+
overrides: {
534+
groupConfig: { requireMention: false } as never,
535+
requireMention: false,
536+
},
537+
});
538+
539+
expect(result?.rawBody).toBe(text);
540+
});
541+
497542
it("does not transcribe group audio for unauthorized senders", async () => {
498543
transcribeFirstAudioMock.mockReset();
499544
const logger = createLogger();

‎extensions/telegram/src/bot-message-context.body.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
buildSenderName,
4545
extractTelegramLocation,
4646
getTelegramTextParts,
47+
hasLeadingBotCommandAddressedToOtherBot,
4748
hasBotMentionInText,
4849
hasBotMention,
4950
renderTelegramTextEntities,
@@ -191,6 +192,15 @@ export async function resolveTelegramInboundBody(params: {
191192
providerPolicy: providerMentionPatterns,
192193
});
193194
const messageTextParts = getTelegramTextParts(msg);
195+
if (botUsername && hasLeadingBotCommandAddressedToOtherBot(msg, botUsername)) {
196+
logInboundDrop({
197+
log: logVerbose,
198+
channel: "telegram",
199+
reason: "command addressed to another bot",
200+
target: senderId ?? "unknown",
201+
});
202+
return null;
203+
}
194204
const allowForCommands = isGroup ? effectiveGroupAllow : effectiveDmAllow;
195205
const useAccessGroups = true;
196206
const hasControlCommandInMessage = hasControlCommand(messageTextParts.text, cfg, {

‎extensions/telegram/src/bot/body-helpers.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,26 @@ export function hasBotMention(msg: Message, botUsername: string) {
327327
return false;
328328
}
329329

330+
export function hasLeadingBotCommandAddressedToOtherBot(
331+
msg: Message,
332+
botUsername: string,
333+
): boolean {
334+
const { text, entities } = getTelegramTextParts(msg);
335+
const normalizedBotUsername = normalizeLowercaseStringOrEmpty(botUsername).replace(/^@/u, "");
336+
if (!normalizedBotUsername) {
337+
return false;
338+
}
339+
const leadingCommand = entities.find(
340+
(entity) => entity.type === "bot_command" && entity.offset === 0,
341+
);
342+
if (!leadingCommand) {
343+
return false;
344+
}
345+
const command = text.slice(0, leadingCommand.length);
346+
const target = command.match(/^\/[^@\s]+@([a-z0-9_]+)$/iu)?.[1];
347+
return Boolean(target && target.toLowerCase() !== normalizedBotUsername);
348+
}
349+
330350
export function hasBotMentionInText(text: string, botUsername: string): boolean {
331351
return hasStandaloneTelegramMention(
332352
normalizeLowercaseStringOrEmpty(text),

0 commit comments

Comments
 (0)