Skip to content

Commit f189790

Browse files
authored
Merge pull request qwibitai#45 from qwibitai/fix/telegram-slash-command-filter
fix: only skip /chatid and /ping, let other / messages through
2 parents c4f4455 + 59e7afe commit f189790

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/channels/telegram.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,29 @@ describe('TelegramChannel', () => {
295295
expect(opts.onMessage).not.toHaveBeenCalled();
296296
});
297297

298-
it('skips command messages (starting with /)', async () => {
298+
it('skips bot commands (/chatid, /ping) but passes other / messages through', async () => {
299299
const opts = createTestOpts();
300300
const channel = new TelegramChannel('test-token', opts);
301301
await channel.connect();
302302

303-
const ctx = createTextCtx({ text: '/start' });
304-
await triggerTextMessage(ctx);
305-
303+
// Bot commands should be skipped
304+
const ctx1 = createTextCtx({ text: '/chatid' });
305+
await triggerTextMessage(ctx1);
306306
expect(opts.onMessage).not.toHaveBeenCalled();
307307
expect(opts.onChatMetadata).not.toHaveBeenCalled();
308+
309+
const ctx2 = createTextCtx({ text: '/ping' });
310+
await triggerTextMessage(ctx2);
311+
expect(opts.onMessage).not.toHaveBeenCalled();
312+
313+
// Non-bot /commands should flow through
314+
const ctx3 = createTextCtx({ text: '/remote-control' });
315+
await triggerTextMessage(ctx3);
316+
expect(opts.onMessage).toHaveBeenCalledTimes(1);
317+
expect(opts.onMessage).toHaveBeenCalledWith(
318+
'tg:100200300',
319+
expect.objectContaining({ content: '/remote-control' }),
320+
);
308321
});
309322

310323
it('extracts sender name from first_name', async () => {

src/channels/telegram.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ export class TelegramChannel implements Channel {
8080
ctx.reply(`${ASSISTANT_NAME} is online.`);
8181
});
8282

83+
// Telegram bot commands handled above — skip them in the general handler
84+
// so they don't also get stored as messages. All other /commands flow through.
85+
const TELEGRAM_BOT_COMMANDS = new Set(['chatid', 'ping']);
86+
8387
this.bot.on('message:text', async (ctx) => {
84-
// Skip commands
85-
if (ctx.message.text.startsWith('/')) return;
88+
if (ctx.message.text.startsWith('/')) {
89+
const cmd = ctx.message.text.slice(1).split(/[\s@]/)[0].toLowerCase();
90+
if (TELEGRAM_BOT_COMMANDS.has(cmd)) return;
91+
}
8692

8793
const chatJid = `tg:${ctx.chat.id}`;
8894
let content = ctx.message.text;

0 commit comments

Comments
 (0)