mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-22 20:46:42 +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
|
|
@ -7,6 +7,7 @@ import {
|
|||
SessionModule,
|
||||
CreditModule,
|
||||
TodoApiService,
|
||||
I18nModule,
|
||||
} from '@manacore/bot-services';
|
||||
|
||||
// Factory provider for TodoApiService
|
||||
|
|
@ -26,6 +27,7 @@ const todoApiServiceProvider = {
|
|||
TranscriptionModule.forRoot(),
|
||||
SessionModule.forRoot({ storageMode: 'redis' }),
|
||||
CreditModule.forRoot(),
|
||||
I18nModule.forRoot(),
|
||||
],
|
||||
providers: [MatrixService, todoApiServiceProvider],
|
||||
exports: [MatrixService],
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ import {
|
|||
CreditService,
|
||||
TodoApiService,
|
||||
Task as ApiTask,
|
||||
I18nService,
|
||||
Language,
|
||||
LANGUAGE_NAMES,
|
||||
} from '@manacore/bot-services';
|
||||
import { HELP_TEXT, WELCOME_TEXT, BOT_INTRODUCTION } from '../config/configuration';
|
||||
|
||||
|
|
@ -44,7 +47,8 @@ export class MatrixService extends BaseMatrixService {
|
|||
private todoApiService: TodoApiService,
|
||||
private transcriptionService: TranscriptionService,
|
||||
private sessionService: SessionService,
|
||||
private creditService: CreditService
|
||||
private creditService: CreditService,
|
||||
private i18nService: I18nService
|
||||
) {
|
||||
super(configService);
|
||||
}
|
||||
|
|
@ -108,7 +112,11 @@ export class MatrixService extends BaseMatrixService {
|
|||
if (body.startsWith('!')) {
|
||||
const [command, ...args] = body.slice(1).split(' ');
|
||||
await this.executeCommand(roomId, event, userId, command.toLowerCase(), args.join(' '));
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback: treat any message as a task
|
||||
await this.handleAddTask(roomId, event, userId, body);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error handling message: ${error}`);
|
||||
await this.sendReply(roomId, event, 'Ein Fehler ist aufgetreten. Bitte versuche es erneut.');
|
||||
|
|
@ -299,12 +307,64 @@ export class MatrixService extends BaseMatrixService {
|
|||
await this.handlePinHelp(roomId, event);
|
||||
break;
|
||||
|
||||
case 'language':
|
||||
case 'sprache':
|
||||
case 'lang':
|
||||
await this.handleLanguage(roomId, event, userId, args);
|
||||
break;
|
||||
|
||||
default:
|
||||
// Unknown command - ignore silently or send help
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async handleLanguage(
|
||||
roomId: string,
|
||||
event: MatrixRoomEvent,
|
||||
userId: string,
|
||||
args: string
|
||||
) {
|
||||
const lang = args.trim().toLowerCase();
|
||||
|
||||
// Show current language if no argument
|
||||
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;
|
||||
}
|
||||
|
||||
// Validate and set language
|
||||
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];
|
||||
|
||||
// Respond in the new language
|
||||
if (lang === 'de') {
|
||||
await this.sendReply(roomId, event, `Sprache geändert zu: **${langName}**`);
|
||||
} else {
|
||||
await this.sendReply(roomId, event, `Language changed to: **${langName}**`);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleAddTask(
|
||||
roomId: string,
|
||||
event: MatrixRoomEvent,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue