DEV Community

ILshat Khamitov
ILshat Khamitov

Posted on

Create facts generator for telegram bot with NestJS

You need to go to google and find a site with a free joke generator, the site must not be a SPA

I chose the second
I chose the second

Create new library facts-generator

npm run -- nx g @nrwl/nest:lib facts-generator/server

endy@endy-virtual-machine:~/Projects/current/kaufman-bot$ npm run -- nx g @nrwl/nest:lib facts-generator/server

> kaufman-bot@0.0.0 nx
> nx "g" "@nrwl/nest:lib" "facts-generator/server"

CREATE libs/facts-generator/server/
CREATE libs/facts-generator/server/.babelrc
CREATE libs/facts-generator/server/src/index.ts
CREATE libs/facts-generator/server/tsconfig.json
CREATE libs/facts-generator/server/tsconfig.lib.json
UPDATE tsconfig.base.json
CREATE libs/facts-generator/server/project.json
UPDATE workspace.json
CREATE libs/facts-generator/server/.eslintrc.json
CREATE libs/facts-generator/server/jest.config.js
CREATE libs/facts-generator/server/tsconfig.spec.json
CREATE libs/facts-generator/server/src/lib/facts-generator-server.module.ts
Enter fullscreen mode Exit fullscreen mode

Go to the site and define a selector for fact text
Go to the site and define a selector for fact text

Go to developer console panel and check selector
Go to developer console panel and check selector

Create file libs/facts-generator/server/src/lib/facts-generator-services/facts-generator.service.ts

import { ScraperService } from '@kaufman-bot/html-scraper/server';
import { Injectable } from '@nestjs/common';

export class FactsGeneratorService {
  constructor(private readonly scraperService: ScraperService) {}

  async onMessage(msg) {
    let result = await this.scraperService.onMessage(msg);
    if (result !== null) {
      result = result.replace('\n\nTweet []', '');
    return result;
Enter fullscreen mode Exit fullscreen mode

Create file libs/facts-generator/server/src/lib/facts-generator.module.ts

import { ScraperModule } from '@kaufman-bot/html-scraper/server';
import { DynamicModule, Module } from '@nestjs/common';
import { getText } from 'class-validator-multi-lang';
import { FactsGeneratorService } from './facts-generator-services/facts-generator.service';

export class FactsGeneratorModule {
  static forRoot(): DynamicModule {
    return {
      module: FactsGeneratorModule,
      imports: [
          contentSelector: '#z',
          help: getText('Random facts generator'),
          spyWords: [getText('facts')],
          removeWords: [getText('get'), getText('please')],
          uri: '',
      providers: [FactsGeneratorService],
      exports: [ScraperModule, FactsGeneratorService],
Enter fullscreen mode Exit fullscreen mode

Run generate all needed files

npm run generate

Update file apps/server/src/app/app.module.ts

import { CurrencyConverterModule } from '@kaufman-bot/currency-converter/server';
import { FactsGeneratorModule } from '@kaufman-bot/facts-generator/server';
import { Module } from '@nestjs/common';
import env from 'env-var';
import { TelegrafModule } from 'nestjs-telegraf';
import { AppController } from './app.controller';
import { AppService } from './app.service';

  imports: [
      token: env.get('TELEGRAM_BOT_TOKEN').required().asString(),
  controllers: [AppController],
  providers: [AppService],
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Update file apps/server/src/app/app.service.ts

import { CurrencyConverterService } from '@kaufman-bot/currency-converter/server';
import { FactsGeneratorService } from '@kaufman-bot/facts-generator/server';
import { Injectable, Logger } from '@nestjs/common';
import { Hears, Help, Message, On, Start, Update } from 'nestjs-telegraf';
import { Context } from 'telegraf';

export class AppService {
  private readonly logger = new Logger(;

    private readonly currencyConverterService: CurrencyConverterService,
    private readonly factsGeneratorService: FactsGeneratorService
  ) {}

  getData(): { message: string } {
    return { message: 'Welcome to server!' };

  async startCommand(ctx: Context) {
    await ctx.reply('Welcome');

  async helpCommand(ctx: Context) {
    await ctx.reply('Send me a sticker');

  async onSticker(ctx: Context) {
    await ctx.reply('👍');

  async hearsHi(ctx: Context) {
    await ctx.reply('Hey there');

  async onMessage(@Message() msg) {
    try {
      let replayMessage = await this.currencyConverterService.onMessage(msg);
      if (replayMessage === null) {
        replayMessage = this.factsGeneratorService.onMessage(msg);
      return replayMessage;
    } catch (err) {
      this.logger.error(err, err.stack);
Enter fullscreen mode Exit fullscreen mode

Test from telegram
Test from telegram

Top comments (0)