Merge pull request #45 from qwibitai/fix/telegram-slash-command-filter
fix: only skip /chatid and /ping, let other / messages through
This commit is contained in:
@@ -295,16 +295,29 @@ describe('TelegramChannel', () => {
|
|||||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('skips command messages (starting with /)', async () => {
|
it('skips bot commands (/chatid, /ping) but passes other / messages through', async () => {
|
||||||
const opts = createTestOpts();
|
const opts = createTestOpts();
|
||||||
const channel = new TelegramChannel('test-token', opts);
|
const channel = new TelegramChannel('test-token', opts);
|
||||||
await channel.connect();
|
await channel.connect();
|
||||||
|
|
||||||
const ctx = createTextCtx({ text: '/start' });
|
// Bot commands should be skipped
|
||||||
await triggerTextMessage(ctx);
|
const ctx1 = createTextCtx({ text: '/chatid' });
|
||||||
|
await triggerTextMessage(ctx1);
|
||||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||||
expect(opts.onChatMetadata).not.toHaveBeenCalled();
|
expect(opts.onChatMetadata).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
const ctx2 = createTextCtx({ text: '/ping' });
|
||||||
|
await triggerTextMessage(ctx2);
|
||||||
|
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// Non-bot /commands should flow through
|
||||||
|
const ctx3 = createTextCtx({ text: '/remote-control' });
|
||||||
|
await triggerTextMessage(ctx3);
|
||||||
|
expect(opts.onMessage).toHaveBeenCalledTimes(1);
|
||||||
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||||
|
'tg:100200300',
|
||||||
|
expect.objectContaining({ content: '/remote-control' }),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('extracts sender name from first_name', async () => {
|
it('extracts sender name from first_name', async () => {
|
||||||
|
|||||||
@@ -80,9 +80,15 @@ export class TelegramChannel implements Channel {
|
|||||||
ctx.reply(`${ASSISTANT_NAME} is online.`);
|
ctx.reply(`${ASSISTANT_NAME} is online.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Telegram bot commands handled above — skip them in the general handler
|
||||||
|
// so they don't also get stored as messages. All other /commands flow through.
|
||||||
|
const TELEGRAM_BOT_COMMANDS = new Set(['chatid', 'ping']);
|
||||||
|
|
||||||
this.bot.on('message:text', async (ctx) => {
|
this.bot.on('message:text', async (ctx) => {
|
||||||
// Skip commands
|
if (ctx.message.text.startsWith('/')) {
|
||||||
if (ctx.message.text.startsWith('/')) return;
|
const cmd = ctx.message.text.slice(1).split(/[\s@]/)[0].toLowerCase();
|
||||||
|
if (TELEGRAM_BOT_COMMANDS.has(cmd)) return;
|
||||||
|
}
|
||||||
|
|
||||||
const chatJid = `tg:${ctx.chat.id}`;
|
const chatJid = `tg:${ctx.chat.id}`;
|
||||||
let content = ctx.message.text;
|
let content = ctx.message.text;
|
||||||
|
|||||||
Reference in New Issue
Block a user