Skip to content

Commit

Permalink
Add clock demo command
Browse files Browse the repository at this point in the history
  • Loading branch information
OperKH committed Feb 21, 2024
1 parent 91afde2 commit 418b58a
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Bot } from './bot/bot.class.js';
import { StartCommand, ClassifyMessageCommand, RecognizeSpeechCommand } from './bot/commands/index.js';
import { StartCommand, ClassifyMessageCommand, RecognizeSpeechCommand, ClockCommand } from './bot/commands/index.js';
import { ConfigService } from './config/config.service.js';

const configService = new ConfigService();
const bot = new Bot(configService);

bot.registerCommands([StartCommand, ClassifyMessageCommand, RecognizeSpeechCommand]);
bot.registerCommands([StartCommand, ClockCommand, ClassifyMessageCommand, RecognizeSpeechCommand]);
bot.start();

// Enable graceful stop
Expand Down
13 changes: 10 additions & 3 deletions src/bot/bot.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { session, Telegraf } from 'telegraf';
import { BotCommand } from 'telegraf/types';
import { Command } from './commands/command.class.js';
import { IBotContext } from './context/context.interface.js';
import { ConfigService } from '../config/config.service.js';
Expand All @@ -13,11 +14,17 @@ export class Bot {
}

registerCommands(commands: Array<{ new (bot: Telegraf<IBotContext>): Command }>) {
const botCommands: BotCommand[] = [];
for (const Command of commands) {
const command = new Command(this.bot);
this.commands.push(command);
command.handle();
const commandEntity = new Command(this.bot);
commandEntity.handle();
this.commands.push(commandEntity);
const { command, description } = commandEntity;
if (command && description) {
botCommands.push({ command, description });
}
}
this.bot.telegram.setMyCommands(botCommands);
}

start() {
Expand Down
8 changes: 2 additions & 6 deletions src/bot/commands/classifyMessage.command.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { Telegraf } from 'telegraf';
import { message } from 'telegraf/filters';

import { Command } from './command.class.js';
import { IBotContext } from '../context/context.interface.js';
import { AIService } from '../../services/ai.service.js';

export class ClassifyMessageCommand extends Command {
public command = null;
public description = null;
private aiService = AIService.getInstance();

constructor(bot: Telegraf<IBotContext>) {
super(bot);
}

handle(): void {
this.bot.on(message('text'), async (ctx) => {
const maxToxicScore = await this.aiService.getMaxToxicScore(ctx.message.text);
Expand Down
22 changes: 22 additions & 0 deletions src/bot/commands/clock.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Command } from './command.class.js';
import { ClockLoader } from '../loaders/clockLoader.class.js';

export class ClockCommand extends Command {
public command = 'clock';
public description = 'πŸ•“ Π”Π΅ΠΌΠΎ Π³ΠΎΠ΄ΠΈΠ½Π½ΠΈΠΊΠ°';

handle(): void {
this.bot.command(this.command, async (ctx) => {
await ctx.deleteMessage();
const clockLoader = new ClockLoader(ctx);
await clockLoader.start();
await new Promise((resolve) => setTimeout(resolve, 25000));
const messageId = clockLoader.stop();
if (messageId) {
await ctx.telegram.deleteMessage(ctx.chat.id, messageId);
}
});
}

async dispose() {}
}
4 changes: 3 additions & 1 deletion src/bot/commands/command.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { IBotContext } from '../context/context.interface.js';
export abstract class Command {
constructor(public readonly bot: Telegraf<IBotContext>) {}

abstract handle(): void;
abstract command: string | null;
abstract description: string | null;

abstract handle(): void;
abstract dispose(): Promise<void>;
}
1 change: 1 addition & 0 deletions src/bot/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './command.class.js';

export * from './classifyMessage.command.js';
export * from './clock.command.js';
export * from './recognizeSpeech.command.js';
export * from './start.command.js';
8 changes: 3 additions & 5 deletions src/bot/commands/recognizeSpeech.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import https from 'node:https';
import ffmpeg from 'fluent-ffmpeg';
import wavefile from 'wavefile';
import { Telegraf, NarrowedContext } from 'telegraf';
import { NarrowedContext } from 'telegraf';
import { Message, Update } from 'telegraf/types';
import { message } from 'telegraf/filters';

Expand All @@ -12,12 +12,10 @@ import { IBotContext } from '../context/context.interface.js';
import { AIService } from '../../services/ai.service.js';

export class RecognizeSpeechCommand extends Command {
public command = null;
public description = null;
private aiService = AIService.getInstance();

constructor(bot: Telegraf<IBotContext>) {
super(bot);
}

handle(): void {
this.bot.on(message('voice'), async (ctx) => {
await this.messageHandler(ctx, ctx.message.voice.file_id, 'ogg');
Expand Down
11 changes: 5 additions & 6 deletions src/bot/commands/start.command.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Telegraf } from 'telegraf';
import { Command } from './command.class.js';
import { IBotContext } from '../context/context.interface.js';

export class StartCommand extends Command {
constructor(bot: Telegraf<IBotContext>) {
super(bot);
}
public command = 'start';
public description = 'πŸ‘‹ ΠŸΡ€ΠΈΠ²Ρ–Ρ‚Π°Ρ‚ΠΈΡΡ';

handle(): void {
this.bot.start((ctx) => {
ctx.reply("ΠŸΡ€ΠΈΠ²Ρ–Ρ‚, я ΠΏΠΎΠΊΠΈ Π½Ρ–Ρ‡ΠΎΠ³ΠΎ Π½Π΅ Π²ΠΌΡ–ΡŽ, Π°Π»Π΅ ΠΎΠ±ΠΎΠ²'язково навчуся!");
ctx.reply(
'ΠŸΡ€ΠΈΠ²Ρ–Ρ‚, я Π²ΠΌΡ–ΡŽ Ρ€ΠΎΠ·ΠΏΡ–Π·Π½Π°Π²Π°Ρ‚ΠΈ ΠΌΠΎΠ²Ρƒ Ρ– прСдставляти Ρ—Ρ— Ρƒ вигляді тСксту, ΠΏΠΎΠ³Π°Π½ΠΎ Ρ€Π΅Π°Π³ΡƒΡŽ Π½Π° Π³Ρ€ΡƒΠ±Ρƒ ΠΌΠΎΠ²Ρƒ Ρ– Π½Π΅Π·Π°Π±Π°Ρ€ΠΎΠΌ навчуся взаємодіяти Π· ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°ΠΌΠΈ.',
);
});
}

Expand Down
35 changes: 35 additions & 0 deletions src/bot/loaders/clockLoader.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Loader } from './loader.class.js';

export class ClockLoader extends Loader {
// prettier-ignore
private emojiList = ['πŸ•›','πŸ•','πŸ•‘','πŸ•’','πŸ•“','πŸ•”','πŸ••','πŸ•§','πŸ•œ','πŸ•','πŸ•ž','πŸ•Ÿ','πŸ• ','πŸ•‘','πŸ•’','πŸ•£','πŸ•€','πŸ•₯','πŸ•¦','πŸ••','πŸ•–','πŸ•—','πŸ•˜','πŸ•™','πŸ•š'];
private emojiIndex = 0;
private messageId?: number;
private timeoutId: NodeJS.Timeout | number = 0;

get currentEmoji() {
return this.emojiList[this.emojiIndex];
}

async start(reply = false, ms = 300) {
if (this.messageId === undefined) {
const message = await this.ctx.reply(this.currentEmoji, {
reply_parameters: reply ? { message_id: this.ctx.message.message_id } : undefined,
});
this.messageId = message.message_id;
}

clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(async () => {
this.emojiIndex = this.emojiIndex < this.emojiList.length - 1 ? this.emojiIndex + 1 : 0;
await this.ctx.telegram.editMessageText(this.ctx.chat.id, this.messageId, '', this.currentEmoji);

this.start();
}, ms);
}

stop() {
clearTimeout(this.timeoutId);
return this.messageId;
}
}
11 changes: 11 additions & 0 deletions src/bot/loaders/loader.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NarrowedContext } from 'telegraf';
import { Message, Update } from 'telegraf/types';
import { IBotContext } from '../context/context.interface.js';

export abstract class Loader {
constructor(public readonly ctx: NarrowedContext<IBotContext, Update.MessageUpdate<Message>>) {}

public abstract start(...args: unknown[]): Promise<void>;

public abstract stop(): void;
}

0 comments on commit 418b58a

Please sign in to comment.