feat: add group chat and mention-gating to channel adapters
Slack: add requireMention option, resolve bot user ID on connect. Telegram: add group chat mention/reply-to-bot detection, strip @mention from message text, default requireMention=true for groups. WhatsApp: add allowedGroupIds for group chat support, mention detection via mentionedIds and body text, strip bot mention from messages.
This commit is contained in:
@@ -15,6 +15,8 @@ import { splitMessage } from '../utils.js';
|
||||
export interface TelegramAdapterConfig {
|
||||
botToken: string;
|
||||
allowedChatIds: number[];
|
||||
/** Require bot mention or reply-to-bot to respond in group chats (default: true). */
|
||||
requireMention?: boolean;
|
||||
hookEngine?: HookEngine;
|
||||
}
|
||||
|
||||
@@ -32,6 +34,7 @@ export class TelegramAdapter implements ChannelAdapter {
|
||||
private bot: Bot | null = null;
|
||||
private messageHandler?: (msg: InboundMessage) => void;
|
||||
private config: TelegramAdapterConfig;
|
||||
private botInfo?: { id: number; username?: string };
|
||||
|
||||
get status(): ChannelStatus {
|
||||
return this._status;
|
||||
@@ -120,7 +123,33 @@ export class TelegramAdapter implements ChannelAdapter {
|
||||
this.bot.on('message:text', async (ctx) => {
|
||||
if (!this.messageHandler) return;
|
||||
|
||||
const text = ctx.message.text;
|
||||
// Group chat mention gating
|
||||
const isGroup = ctx.chat.type === 'group' || ctx.chat.type === 'supergroup';
|
||||
const requireMention = this.config.requireMention ?? true;
|
||||
|
||||
if (isGroup && requireMention && this.botInfo) {
|
||||
const rawText = ctx.message.text;
|
||||
const username = this.botInfo.username;
|
||||
|
||||
// Check for @bot_username mention
|
||||
const isMentioned = username
|
||||
? rawText.includes(`@${username}`)
|
||||
: false;
|
||||
|
||||
// Also allow replies to bot messages
|
||||
const isReplyToBot = ctx.message.reply_to_message?.from?.id === this.botInfo.id;
|
||||
|
||||
if (!isMentioned && !isReplyToBot) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let text = ctx.message.text;
|
||||
|
||||
// Strip bot mention from text
|
||||
if (isGroup && this.botInfo?.username) {
|
||||
text = text.replace(new RegExp(`@${this.botInfo.username}\\b`, 'g'), '').trim();
|
||||
}
|
||||
|
||||
// Show typing indicator while processing
|
||||
await ctx.replyWithChatAction('typing');
|
||||
@@ -140,6 +169,7 @@ export class TelegramAdapter implements ChannelAdapter {
|
||||
this.bot.start({
|
||||
onStart: (botInfo) => {
|
||||
console.log(`Telegram bot started: @${botInfo.username}`);
|
||||
this.botInfo = { id: botInfo.id, username: botInfo.username };
|
||||
this._status = 'connected';
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user