65 lines
2.7 KiB
TypeScript
65 lines
2.7 KiB
TypeScript
import { Update, Ctx, Start, Help, On, Message, Command, Action, InjectBot, Hears } from 'nestjs-telegraf';
|
|
import { Context, Telegraf, Markup } from 'telegraf';
|
|
import { UsersService } from '../users/users.service';
|
|
|
|
@Update()
|
|
export class BotService {
|
|
constructor(
|
|
private readonly usersService: UsersService,
|
|
@InjectBot() private readonly bot: Telegraf<Context>
|
|
) { }
|
|
|
|
@Start()
|
|
async start(@Ctx() ctx: Context) {
|
|
const user = ctx.from;
|
|
if (user) {
|
|
await this.usersService.create({
|
|
id: user.id,
|
|
fullName: `${user.first_name} ${user.last_name || ''}`.trim(),
|
|
});
|
|
await ctx.reply(
|
|
'Приветствую тебя, мой дорогой друг. Я бот, который будет писать тебе мотивирующие цитаты. Сейчас цитаты буду приходит один раз в час, в настройках можно изменить это время.',
|
|
Markup.keyboard([
|
|
['⚙️ Настройки']
|
|
]).resize()
|
|
);
|
|
}
|
|
}
|
|
|
|
@Help()
|
|
async help(@Ctx() ctx: Context) {
|
|
await ctx.reply(
|
|
'Я буду присылать тебе мотивирующие цитаты. Используй меню для настроек.',
|
|
Markup.keyboard([
|
|
['⚙️ Настройки']
|
|
]).resize()
|
|
);
|
|
}
|
|
|
|
@Command('settings')
|
|
@Hears('⚙️ Настройки')
|
|
async settings(@Ctx() ctx: Context) {
|
|
await ctx.reply('Выберите частоту получения цитат:', Markup.inlineKeyboard([
|
|
[Markup.button.callback('1 час', 'frequency_1'), Markup.button.callback('3 часа', 'frequency_3')],
|
|
[Markup.button.callback('5 часов', 'frequency_5'), Markup.button.callback('7 часов', 'frequency_7')],
|
|
[Markup.button.callback('9 часов', 'frequency_9'), Markup.button.callback('12 часов', 'frequency_12')],
|
|
]));
|
|
}
|
|
|
|
@Action(/^frequency_(\d+)$/)
|
|
async onFrequencySelect(@Ctx() ctx: Context & { match: RegExpExecArray }) {
|
|
const user = ctx.from;
|
|
if (!user) return;
|
|
|
|
const hours = parseInt(ctx.match[1]);
|
|
await this.usersService.update(user.id, { frequency: hours });
|
|
|
|
await ctx.answerCbQuery();
|
|
await ctx.editMessageText(`Отлично! Теперь я буду присылать цитаты каждые ${hours} ч.`);
|
|
}
|
|
|
|
async sendAdminMessage(userId: number, message: string) {
|
|
await this.bot.telegram.sendMessage(userId, message);
|
|
}
|
|
}
|