feat(bot): support tagged assistant replies in topics

This commit is contained in:
2026-03-11 12:15:56 +04:00
parent 9553ca342a
commit fce2d30beb
7 changed files with 587 additions and 93 deletions

View File

@@ -0,0 +1,42 @@
import type { Context } from 'grammy'
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
function getMessageText(ctx: Pick<Context, 'msg'>): string | null {
const message = ctx.msg
if (!message || !('text' in message) || typeof message.text !== 'string') {
return null
}
return message.text
}
export function stripExplicitBotMention(ctx: Pick<Context, 'msg' | 'me'>): {
originalText: string
strippedText: string
} | null {
const text = getMessageText(ctx)
const username = ctx.me.username
if (!text || !username) {
return null
}
const mentionPattern = new RegExp(`(^|\\s)@${escapeRegExp(username)}\\b`, 'giu')
if (!mentionPattern.test(text)) {
return null
}
mentionPattern.lastIndex = 0
return {
originalText: text,
strippedText: text.replace(mentionPattern, '$1').replace(/\s+/gu, ' ').trim()
}
}
export function hasExplicitBotMention(ctx: Pick<Context, 'msg' | 'me'>): boolean {
return stripExplicitBotMention(ctx) !== null
}