Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to work with multiple bots? #854

Closed
Daniyar111 opened this issue Jun 25, 2022 · 6 comments
Closed

how to work with multiple bots? #854

Daniyar111 opened this issue Jun 25, 2022 · 6 comments

Comments

@Daniyar111
Copy link

I have 2 bots which are initialized in app.module.ts:

TelegrafModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      botName: 'admin',
      useFactory: async (config: ConfigService) => {
        return {
          middlewares: [adminSessions.middleware()],
          token: config.get<string>('BOT_ADMIN_TOKEN'),
          include: [BotModule]
        }
      }
    }),
    TelegrafModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      botName: 'client',
      useFactory: async (config: ConfigService) => {
        return {
          middlewares: [clientSessions.middleware()],
          token: config.get<string>('BOT_CLIENT_TOKEN'),
          include: [BotModule]
        }
      }
    }),

And I have bot.module.ts which is

@Module({
  providers: [
  {
    provide: AdminBotUpdate,
    useFactory: (bot: Telegraf<Context>) => {
      return new AdminBotUpdate(bot);
    },
    inject: [getBotToken('admin')]
  }, {
    provide: ClientBotUpdate,
    useFactory: (bot: Telegraf<Context>) => {
      return new ClientBotUpdate(bot);
    },
    inject: [getBotToken('client')]
  }
  ]
})
export class BotModule {}

and it should work with @Update() class with admin.update.ts and client.update.ts:

@Update()
@Injectable()
export class AdminBotUpdate {

  constructor(
      @InjectBot('admin') private adminBot: Telegraf<Context>
  ) {}

  @Hears('hi')
  async hears(@Ctx() ctx: Context) {
    await ctx.reply('Hey from admin');
  }
}
@Update()
@Injectable()
export class ClientBotUpdate {

  constructor(
      @InjectBot('client') private clientBot: Telegraf<Context>
  ) {}

  @Hears('hi')
  async hears(@Ctx() ctx: Context) {
    await ctx.reply('Hey from client');
  }
}

but there is no answer in bots. If I change BotModule to

@Module({
  providers: [
      AdminBotUpdate,
      ClientBotUpdate
  ]
})
export class BotModule {}

is working just with admin.update.ts

@Daniyar111
Copy link
Author

Solved it by dividing into 2 modules: AdminBotModule, ClientBotModule. And include: [AdminBotModule] to admin TelegrafModule.forRootAsync(), and include: [ClientBotModule] to client TelegrafModule.forRootAsync()

@lasthead
Copy link

@Daniyar111 , Hi! Could you share a more detailed code of your main app.module and AdminBotModule? I trying add a second Bot to my app, but it doesn't work :(

@Daniyar111
Copy link
Author

@lasthead Hi!

app.module.ts:

@Module({
  imports: [
    ...,
    TelegrafModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      botName: 'admin',
      useFactory: async (config: ConfigService) => {
        return {
          middlewares: [adminSessions.middleware()],
          token: config.get<string>('BOT_ADMIN_TOKEN'),
          include: [AdminBotModule]
        }
      }
    }),
    TelegrafModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      botName: 'client',
      useFactory: async (config: ConfigService) => {
        return {
          middlewares: [clientSessions.middleware()],
          token: config.get<string>('BOT_CLIENT_TOKEN'),
          include: [ClientBotModule]
        }
      }
    }),
    AdminBotModule,
    ClientBotModule
  ],
  providers: [],
})
export class AppModule {}

client-bot.module.ts:

@Module({
  providers: [
    ClientBotUpdate,
    ClientBotService
  ],
  imports: []
})
export class ClientBotModule {}

admin-bot.module.ts:

@Module({
  providers: [
    AdminBotUpdate,
    AdminBotService
  ],
  imports: []
})
export class AdminBotModule {}

@lasthead
Copy link

@Daniyar111 , many thanks! However, I don't quite understand what to specify in middleware to divided requests from each other?

For example:
in app1.app.update.ts
@Start() async startCommand(ctx: Context) { await ctx.reply('test') }

in app2.app.update.ts i have the same method and they conflict with each other

I think I misunderstand the concept and... maybe this check needs to be implemented inside the app.update.ts

@Daniyar111
Copy link
Author

Daniyar111 commented Jul 23, 2022

@lasthead If you did app.module and bot1.module, bot2.module as I wrote, it must not conflict with each other.. could you share your more detailed code?

I don't quite understand what to specify in middleware to divided requests from each other

Nothing need to add there.. I added just sessions to middleware, it doesn't affect to requests

maybe this check needs to be implemented inside the app.update.ts

no check needed..

just divide your code structure to

-- bot1
---- bot1.service (for working with db and etc)
---- bot1.update (for Telegram requests)
---- bot1.module (for moduling bot1)
-- bot2
---- bot2.service (for working with db and etc)
---- bot2.update (for Telegram requests)
---- bot2.module (for moduling bot2)
-- app.module

So they must work independent of each other

@lasthead
Copy link

lasthead commented Jul 25, 2022

@Daniyar111 , After your comment, I rechecked my code again and... found my error. I just missed row: imports: [bot.module], to TelegrafModule.forRootAsync({ ... }) that's all 🙂. Now it works correctly.
Thank you for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants