Broadcast messaging service in Nest with Nestjs-Telegraf

210 views Asked by At

I'm new to NestJs so it's probably problem with understanding dependency injection.

I have configured bot handler like this:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TelegrafModule as Telegraf } from 'nestjs-telegraf';
import { TelegrafUpdate } from './update.service';
import { SubscribersService } from './subscribers.service';
import { BroadcastService } from './broadcast.service';

@Module({
  imports: [
    Telegraf.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => {
        return {
          token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
        };
      },
      inject: [ConfigService],
    }),
  ],
  providers: [TelegrafUpdate, SubscribersService, BroadcastService],
})
export class TelegrafModule {}

Getting error Nest can't resolve dependencies of the BroadcastService (?, SubscribersService). Please make sure that the argument Telegraf at index [0] is available in the TelegrafModule context. in my Broadcast service:

@Injectable()
export class BroadcastService {
  constructor(
    private telegraf: Telegraf,
    private subscribersService: SubscribersService,
  ) {}

  sendBroadcast(message: string) {
    this.subscribersService.getSubscribers().forEach((subscriber) => {
      this.telegraf.telegram.sendMessage(subscriber, message);
    });
  }
}

Kinda workaround i found is to pass message context from message handler to broadcastService.sendBroadcast, but in doesn't look like correct approach and it won't allow to create features like scheduled messages or ad broadcasting

0

There are 0 answers