nestjs-telegram
nestjs-telegram copied to clipboard
Nest can't resolve dependencies of the TELEGRAM_MODULE_OPTIONS
Hi, when I follow steps from the readme I got following error:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the TELEGRAM_MODULE_OPTIONS (?).
Please make sure that the argument ConfigService at index [0] is available in the TelegramModule context.
I got the same did any one solve this problem ?
To use async module import like this:
TelegramModule.forRootAsync({
useFactory: async (configService: ConfigService) => ({
botKey: configService.get('BOT_TOKEN'),
}),
inject: [ConfigService],
}),
You need to make your config service globally visible, so in AppModule just add isGlobal: true
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
],
providers: [AppService],
})
export class AppModule {}
I tried this way and no more error:
@Injectable()
export class HookTelegramService {
private readonly telegram: TelegramService
async sendMessage(sendMessageParam: TelegramSendMessageParams): Promise<TelegramMessage> {
const result = await this.telegram.sendMessage(sendMessageParam).toPromise()
return result
}
}
Move private readonly telegram: TelegramService
outside constructor
but this.telegram
return undefined
Hi, when I follow steps from the readme I got following error:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the TELEGRAM_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the TelegramModule context.
I tried to use OnModuleInit
and run perfect.
export class HookTelegramService implements OnModuleInit {
private readonly logger = new Logger(HookTelegramService.name)
private telegram: TelegramService
constructor(private moduleRef: ModuleRef) {}
onModuleInit() {
this.telegram = this.moduleRef.get(TelegramService, { strict: false })
}
async sendMessage(sendMessageParam: TelegramSendMessageParams): Promise<TelegramMessage> {
const result = await this.telegram.sendMessage(sendMessageParam).toPromise()
return result
}
}
you have to inject the service.
constructor(private readonly telegram: TelegramService) {}
I removed the TelegramService from exports and providers and the error disappeared.
you need to change your async import to include the ConfigModule:
TelegramModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
botKey: configService.get('TELEGRAM_API_Key')
}),
inject: [ConfigService]
}),