Artem Kuroptev
Artem Kuroptev

Reputation: 29

Broadcast messaging service in Nest with Nestjs-Telegraf

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

Upvotes: 0

Views: 637

Answers (1)

oleksandrh324110
oleksandrh324110

Reputation: 31

You should do @InjectBot() private bot: Telegraf inside your service's constructor arguments

Upvotes: 0

Related Questions