mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-22 00:06:41 +02:00
✨ feat(matrix-bots): add i18n system and direct message fallback
- Add I18nService with per-user language preferences (de/en) - Add !language/!sprache command to all 4 bots (todo, calendar, contacts, clock) - Add fallback behavior: messages without commands create tasks/events/contacts/timers - Improve clock bot duration parsing to accept bare numbers as minutes (e.g. "25" = 25min) - Add support for more duration formats: "25 minuten", "1 stunde", etc. Language preferences stored in SessionService, default configurable via BOT_DEFAULT_LANGUAGE env var.
This commit is contained in:
parent
5c688d713e
commit
c2c80efc50
17 changed files with 1626 additions and 18 deletions
|
|
@ -1,7 +1,12 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { MatrixService } from './matrix.service';
|
||||
import { ContactsModule } from '../contacts/contacts.module';
|
||||
import { SessionModule, TranscriptionModule, CreditModule } from '@manacore/bot-services';
|
||||
import {
|
||||
SessionModule,
|
||||
TranscriptionModule,
|
||||
CreditModule,
|
||||
I18nModule,
|
||||
} from '@manacore/bot-services';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
|
@ -11,6 +16,7 @@ import { SessionModule, TranscriptionModule, CreditModule } from '@manacore/bot-
|
|||
sttUrl: process.env.STT_URL || 'http://localhost:3020',
|
||||
}),
|
||||
CreditModule.forRoot(),
|
||||
I18nModule.forRoot(),
|
||||
],
|
||||
providers: [MatrixService],
|
||||
exports: [MatrixService],
|
||||
|
|
|
|||
|
|
@ -9,7 +9,14 @@ import {
|
|||
UserListMapper,
|
||||
} from '@manacore/matrix-bot-common';
|
||||
import { ContactsService, Contact } from '../contacts/contacts.service';
|
||||
import { SessionService, TranscriptionService, CreditService } from '@manacore/bot-services';
|
||||
import {
|
||||
SessionService,
|
||||
TranscriptionService,
|
||||
CreditService,
|
||||
I18nService,
|
||||
Language,
|
||||
LANGUAGE_NAMES,
|
||||
} from '@manacore/bot-services';
|
||||
import { HELP_MESSAGE } from '../config/configuration';
|
||||
|
||||
const CONTACT_CREATE_CREDITS = 0.02;
|
||||
|
|
@ -32,7 +39,8 @@ export class MatrixService extends BaseMatrixService {
|
|||
private readonly transcriptionService: TranscriptionService,
|
||||
private contactsService: ContactsService,
|
||||
private sessionService: SessionService,
|
||||
private creditService: CreditService
|
||||
private creditService: CreditService,
|
||||
private i18nService: I18nService
|
||||
) {
|
||||
super(configService);
|
||||
}
|
||||
|
|
@ -102,6 +110,10 @@ Sag "hilfe" fur alle Befehle!`;
|
|||
await this.handleCommand(roomId, event, sender, `!${detectedCommand}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback: treat any message as a new contact
|
||||
const args = message.trim().split(/\s+/);
|
||||
await this.handleCreateContact(roomId, event, sender, args);
|
||||
}
|
||||
|
||||
private async handleCommand(
|
||||
|
|
@ -188,6 +200,12 @@ Sag "hilfe" fur alle Befehle!`;
|
|||
await this.pinHelpMessage(roomId, event);
|
||||
break;
|
||||
|
||||
case 'language':
|
||||
case 'sprache':
|
||||
case 'lang':
|
||||
await this.handleLanguage(roomId, event, sender, argString);
|
||||
break;
|
||||
|
||||
default:
|
||||
await this.sendReply(
|
||||
roomId,
|
||||
|
|
@ -766,4 +784,47 @@ Sag "hilfe" fur alle Befehle!`;
|
|||
await this.sendReply(roomId, event, 'Fehler beim Pinnen der Hilfe.');
|
||||
}
|
||||
}
|
||||
|
||||
private async handleLanguage(
|
||||
roomId: string,
|
||||
event: MatrixRoomEvent,
|
||||
userId: string,
|
||||
args: string
|
||||
) {
|
||||
const lang = args.trim().toLowerCase();
|
||||
|
||||
if (!lang) {
|
||||
const currentLang = await this.i18nService.getLanguage(userId);
|
||||
const langName = LANGUAGE_NAMES[currentLang];
|
||||
const available = this.i18nService
|
||||
.getAvailableLanguages()
|
||||
.map((l) => `${l} (${LANGUAGE_NAMES[l]})`)
|
||||
.join(', ');
|
||||
await this.sendReply(
|
||||
roomId,
|
||||
event,
|
||||
`**Sprache / Language:** ${langName}\n\n**Verfügbar / Available:** ${available}\n\nÄndern / Change: \`!language de\` oder / or \`!language en\``
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.i18nService.isValidLanguage(lang)) {
|
||||
const available = this.i18nService.getAvailableLanguages().join(', ');
|
||||
await this.sendReply(
|
||||
roomId,
|
||||
event,
|
||||
`Unbekannte Sprache / Unknown language: ${lang}\n\nVerfügbar / Available: ${available}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.i18nService.setLanguage(userId, lang as Language);
|
||||
const langName = LANGUAGE_NAMES[lang as Language];
|
||||
|
||||
if (lang === 'de') {
|
||||
await this.sendReply(roomId, event, `Sprache geändert zu: **${langName}**`);
|
||||
} else {
|
||||
await this.sendReply(roomId, event, `Language changed to: **${langName}**`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue