From d589e3128d5cff1066b95d933327e906a8903bb2 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 19 Feb 2026 13:55:29 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20chatbot=20=D0=BE=D1=82=D0=B2=D0=B5?= =?UTF-8?q?=D1=87=D0=B0=D0=B5=D1=82=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=20@=D1=83=D0=BF=D0=BE=D0=BC=D0=B8=D0=BD?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B8=20=D0=B8=D0=BB=D0=B8=20reply=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B5=D0=B3=D0=BE=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Бот больше не реагирует на все сообщения в чате. Отвечает только когда: - упомянут через @username в тексте - сообщение является ответом (reply) на сообщение бота @mention автоматически вырезается из текста перед отправкой в Claude. Co-Authored-By: Claude Opus 4.6 --- src/index.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6be0d46..89f60c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -595,14 +595,42 @@ bot.on('text', async (ctx) => { } // No active project session — use chatbot if enabled + // Only respond when bot is mentioned (@username) or message is a reply to bot if (isChatbotEnabled()) { + const botInfo = bot.botInfo; + const botId = botInfo?.id; + const botUsername = botInfo?.username?.toLowerCase(); + + // Check if bot is mentioned via @username in message entities + const entities = ctx.message.entities || []; + const isMentioned = botUsername && entities.some(e => + e.type === 'mention' && + message.substring(e.offset, e.offset + e.length).toLowerCase() === `@${botUsername}` + ); + + // Check if message is a reply to bot's own message + const replyTo = ctx.message.reply_to_message; + const isReplyToBot = replyTo?.from?.id === botId; + + if (!isMentioned && !isReplyToBot) { + // Not addressed to bot — ignore silently + return; + } + + // Strip @mention from message text before sending to Claude + let cleanMessage = message; + if (isMentioned && botUsername) { + cleanMessage = message.replace(new RegExp(`@${botUsername}`, 'gi'), '').trim(); + } + if (!cleanMessage) cleanMessage = message; + const userChatId = ctx.chat.id.toString(); - console.log(`🤖 Chatbot handling message from ${from}: "${message.substring(0, 50)}..."`); + console.log(`🤖 Chatbot handling message from ${from}: "${cleanMessage.substring(0, 50)}..."`); // Fire-and-forget: don't block Telegraf's handler handleChatbotMessage( userChatId, - message, + cleanMessage, async () => { await ctx.sendChatAction('typing'); },