NESTJSの上でTelegramボットで有名な人々のランダムな引用を生成するために、モジュールをつくってください
23360 ワード
リンク
https://github.com/EndyKaufman/kaufman-bot - ボットのソースコード
https://telegram.me/DevelopKaufmanBot - 現在のロボット
https://www.forismatic.com/ - 英語とロシア語のランダム引用符を生成するサイト
作品の説明
あなたが人々の引用と冗談に一度に2つのモジュールの追加を記述するならば、非常に大きなポストは出ます
私はその仕事を2つの別々のポストに分けることに決めた
このモジュールはファクトジェネレータのコピーで、データ解析や他の設定のための別のサイトを使用します
このモジュールは以前使用されていないスクラッパーモジュールの機能を使用します.
1 )構文解析用のページアドレスにユーザのロケールを使用する
2 )複数素子セレクタ
3 )事前にインストールされた反アンチddosヘッダの変更
クリエイトクォート
ライブラリプラグインの追加
npm run -- nx g @nrwl/nest:lib quotes-generator/server
endy@endy-virtual-machine:~/Projects/current/kaufman-bot$ npm run -- nx g @nrwl/nest:lib quotes-generator/server
> kaufman-bot@0.0.0 nx
> nx "g" "@nrwl/nest:lib" "quotes-generator/server"
CREATE libs/quotes-generator/server/README.md
CREATE libs/quotes-generator/server/.babelrc
CREATE libs/quotes-generator/server/src/index.ts
CREATE libs/quotes-generator/server/tsconfig.json
CREATE libs/quotes-generator/server/tsconfig.lib.json
UPDATE tsconfig.base.json
CREATE libs/quotes-generator/server/project.json
UPDATE workspace.json
CREATE libs/quotes-generator/server/.eslintrc.json
CREATE libs/quotes-generator/server/jest.config.js
CREATE libs/quotes-generator/server/tsconfig.spec.json
CREATE libs/quotes-generator/server/src/lib/quotes-generator-server.module.ts
quotegeneratorserviceを追加する
LIBS/引用ジェネレータ/サーバ/src/lib/引用ジェネレータのサービス/引用ジェネレータ.サービスTS
import {
BotCommandsEnum,
BotCommandsProvider,
BotCommandsProviderActionMsg,
BotCommandsProviderActionResultType,
BotСommandsToolsService,
} from '@kaufman-bot/core/server';
import { ScraperService } from '@kaufman-bot/html-scraper/server';
import { Injectable } from '@nestjs/common';
@Injectable()
export class QuotesGeneratorService implements BotCommandsProvider {
constructor(
private readonly scraperService: ScraperService,
private readonly botСommandsToolsService: BotСommandsToolsService
) {}
async onHelp<
TMsg extends BotCommandsProviderActionMsg = BotCommandsProviderActionMsg
>(msg: TMsg) {
return await this.scraperService.onHelp(msg);
}
async onMessage<
TMsg extends BotCommandsProviderActionMsg = BotCommandsProviderActionMsg
>(msg: TMsg): Promise<BotCommandsProviderActionResultType<TMsg>> {
let locale = msg.from?.language_code;
if (!locale?.includes('ru') || !locale?.includes('en')) {
locale = 'en';
}
if (
this.botСommandsToolsService.checkCommands(
msg.text,
[...Object.keys(BotCommandsEnum)],
locale
)
) {
const result = await this.scraperService.onMessage(msg);
try {
if (result?.type === 'text') {
return {
type: 'text',
text: result.text.split('\\"').join('"').split('\n').join(' '),
};
}
return result;
} catch (err) {
console.debug(result);
console.error(err, err.stack);
throw err;
}
}
return null;
}
}
quotegeneratormoduleを追加する
LIBS/引用ジェネレータ/サーバ/src/lib/引用ジェネレータ.モジュールです.TS
import {
BotCommandsModule,
BOT_COMMANDS_PROVIDER,
} from '@kaufman-bot/core/server';
import { ScraperModule } from '@kaufman-bot/html-scraper/server';
import { DynamicModule, Module } from '@nestjs/common';
import { getText } from 'class-validator-multi-lang';
import { TranslatesModule } from 'nestjs-translates';
import { QuotesGeneratorService } from './quotes-generator-services/quotes-generator.service';
@Module({
imports: [TranslatesModule, BotCommandsModule],
exports: [TranslatesModule, BotCommandsModule],
})
export class QuotesGeneratorModule {
static forRoot(): DynamicModule {
return {
module: QuotesGeneratorModule,
imports: [
ScraperModule.forRoot({
name: getText('Quotes generator'),
descriptions: getText(
'Command to generate text with a random quotes'
),
usage: [getText('get quote'), getText('quotes help')],
contentSelector:
'forismatic > quote > quotetext, forismatic > quote > quoteauthor',
spyWords: [getText('quotes'), getText('quote')],
removeWords: [getText('get'), getText('please')],
uri: 'https://api.forismatic.com/api/1.0/?method=getQuote&format=xml&lang={{locale}}',
contentCodepage: 'utf8',
headers: [{}],
}),
],
providers: [
{
provide: BOT_COMMANDS_PROVIDER,
useClass: QuotesGeneratorService,
},
],
exports: [ScraperModule],
};
}
}
ファイルの準備
すべての単語を翻訳
すべての辞書のリストを見る
すべての必要な翻訳
変換po辞書のjsonへの準備ファイル
npm run generate
quotegeneratormoduleをappmoduleに追加する
アプリ/サーバ/src/app/app.モジュールです.TS
@Module({
imports: [
...
QuotesGeneratorModule.forRoot(),
...
],
...
})
export class AppModule {}
新しいロジックをチェックしてください
共通ヘルプメッセージ
英語で引用する
ロシア語で引用
次の投稿では英語とロシア語のジョークを追加します.
Reference
この問題について(NESTJSの上でTelegramボットで有名な人々のランダムな引用を生成するために、モジュールをつくってください), 我々は、より多くの情報をここで見つけました https://dev.to/endykaufman/create-module-for-generate-random-quote-of-famus-people-in-telegram-bot-on-nestjs-2cm4テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol