feat: implement command service with response queue

main
borja (aider) 3 months ago
parent b7ed60a471
commit 63c3bb20e9

@ -1,5 +1,6 @@
import { Bun } from 'bun'; import { Bun } from 'bun';
import { CommandService } from './services/command'; import { CommandService } from './services/command';
import { ResponseQueue } from './services/response-queue';
const PORT = 3007; const PORT = 3007;
@ -57,12 +58,15 @@ export class WebhookServer {
// Forward to command service if applicable // Forward to command service if applicable
const messageText = data.message.conversation; const messageText = data.message.conversation;
if (messageText?.startsWith('/tarea')) { if (messageText?.startsWith('/tarea')) {
await CommandService.handle({ const responses = await CommandService.handle({
sender: data.key.participant, sender: data.key.participant,
groupId: data.key.remoteJid, groupId: data.key.remoteJid,
message: messageText, message: messageText,
mentions: data.contextInfo?.mentionedJid || [] mentions: data.contextInfo?.mentionedJid || []
}); });
// Queue responses for sending
await ResponseQueue.add(responses);
} }
} }
} }

@ -0,0 +1,38 @@
type CommandContext = {
sender: string;
groupId: string;
message: string;
mentions: string[];
};
export type CommandResponse = {
recipient: string;
message: string;
};
export class CommandService {
private static async processTareaCommand(
context: CommandContext
): Promise<CommandResponse[]> {
// Will implement actual command logic later
return [{
recipient: context.sender,
message: 'Command received: ' + context.message
}];
}
static async handle(context: CommandContext): Promise<CommandResponse[]> {
if (!context.message.startsWith('/tarea')) {
return [];
}
try {
return await this.processTareaCommand(context);
} catch (error) {
return [{
recipient: context.sender,
message: 'Error processing command'
}];
}
}
}

@ -0,0 +1,17 @@
type QueuedResponse = {
recipient: string;
message: string;
};
export const ResponseQueue = {
queue: [] as QueuedResponse[],
async add(responses: QueuedResponse[]) {
this.queue.push(...responses);
console.log('Queued responses:', responses);
},
async process() {
// Will implement actual processing later
}
};

@ -0,0 +1,38 @@
import { describe, test, expect, mock } from 'bun:test';
import { CommandService } from '../../../src/services/command';
describe('CommandService', () => {
const testContext = {
sender: '1234567890@s.whatsapp.net',
groupId: 'group-id@g.us',
message: '/tarea nueva Test task',
mentions: []
};
test('should ignore non-tarea commands', async () => {
const responses = await CommandService.handle({
...testContext,
message: '/othercommand'
});
expect(responses).toEqual([]);
});
test('should handle tarea commands', async () => {
const responses = await CommandService.handle(testContext);
expect(responses.length).toBe(1);
expect(responses[0].recipient).toBe(testContext.sender);
expect(responses[0].message).toInclude('Command received');
});
test('should return error response on failure', async () => {
const originalProcess = CommandService.processTareaCommand;
CommandService.processTareaCommand = mock(() => {
throw new Error('Test error');
});
const responses = await CommandService.handle(testContext);
expect(responses[0].message).toInclude('Error processing');
CommandService.processTareaCommand = originalProcess;
});
});
Loading…
Cancel
Save