JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 63
  • Score
    100M100P100Q70834F
  • License MIT

I18n integration for NestJS Telegraf

Package Exports

  • nestjs-telegraf-i18n
  • nestjs-telegraf-i18n/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nestjs-telegraf-i18n) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nestjs-telegraf-i18n

Seamless mix of nestjs-telegraf and nestjs-i18n

npm install nestjs-telegraf-i18n

Prerequisites

  • installed nestjs-telegraf, telegraf, nestjs-i18n
npm install nestjs-telegraf telegraf nestjs-i18n

Initialization

Provide the Module with the i18n extended context and add the new middleware that injects the i18n context into the Telegraf context

Sync

@Module({
    imports: [
        TelegrafModule.forRoot({
            token: "<your_bot_token>",
            options: {
                contextType: TelegrafI18nContext,
            },
        }),
    ],
    providers: [ TelegrafI18nMiddleware ],
})
export class TelegramModule {}

Or Async

@Module({
    imports: [
        TelegrafModule.forRootAsync({
            useFactory: () => ({
                token: "<your_bot_token>",
                options: {
                    contextType: TelegrafI18nContext,
                },
            }),
        }),
    ],
    providers: [ TelegrafI18nMiddleware ],
})
export class TelegramModule {}

Usage

The middleware will inject the i18n object into the Telegraf context. In your function make the ctx aware that it has the object by providing the type TelegrafI18nContext If you have multiple telegraf context types chain them with &

import { Scenes } from 'telegraf';
import { Ctx, Start, Update } from 'nestjs-telegraf';
import { TelegrafI18nContext } from "nestjs-telegraf-i18n";

@Update()
export class BotUpdate {
    @Start()
    async start_command(@Ctx() ctx: Scenes.WizardContext & TelegrafI18nContext) {
        const internationalized_hello_message = ctx.i18n.t("i18n.menus.hello.message");
        await ctx.reply(internationalized_hello_message);
    }
}

Type Safety

You can use the built in type safety features from nestjs-i18n Follow their instruction to generate the translations type and you can pass it to the extended context.

import { Scenes } from 'telegraf';
import { Ctx, Start, Update } from 'nestjs-telegraf';
import { TelegrafI18nContext } from "nestjs-telegraf-i18n";
import { I18nTranslations } from './generated/i18n.generated.ts';

@Update()
export class BotUpdate {
    @Start()
    async start_command(@Ctx() ctx: Scenes.WizardContext & TelegrafI18nContext<I18nTranslations>) {
        const internationalized_hello_message = ctx.i18n.t("i18n.menus.hello.message");
        await ctx.reply(internationalized_hello_message);
    }
}