141 lines
3.8 KiB
TypeScript
141 lines
3.8 KiB
TypeScript
import {
|
||
Command,
|
||
Ctx,
|
||
Hears,
|
||
Start,
|
||
Update,
|
||
Sender,
|
||
Help,
|
||
On, Message,
|
||
} from 'nestjs-telegraf';
|
||
import { UpdateType as TelegrafUpdateType } from 'telegraf/typings/telegram-types';
|
||
import { Context } from './context.interface';
|
||
import { ExtraReplyMessage } from 'telegraf/typings/telegram-types';
|
||
import { Markup } from 'telegraf';
|
||
import {
|
||
QUIZ_SCENE,
|
||
REGISTER_PHOTO_SCENE,
|
||
REGISTER_SCENE_ID,
|
||
} from './scenes/scenes.const';
|
||
import { Messages } from './tg.text';
|
||
import { GlobalCommands } from './global-commands';
|
||
import {Inject, Logger} from "@nestjs/common";
|
||
import AppConsts from "../constants";
|
||
import {ClientProxy} from "@nestjs/microservices";
|
||
import {catchError} from "rxjs";
|
||
import {TGHandlers} from "./handlers";
|
||
|
||
@Update()
|
||
export class BotUpdate {
|
||
readonly numbers = Messages.answerNumbers;
|
||
private readonly logger = new Logger(BotUpdate.name);
|
||
constructor(
|
||
@Inject(AppConsts.GameServiceName) private gameService: ClientProxy,
|
||
private globalCmd: GlobalCommands,
|
||
) {
|
||
}
|
||
|
||
@Start()
|
||
async startCommand(@Ctx() ctx: Context) {
|
||
this.logger.verbose(`Sending GuestInfo to MQTT for ${ctx.message.from.first_name} / ${ctx.message.from.id}`);
|
||
this.gameService.send({cmd: 'GuestInfo'}, {user: ctx.from.id})
|
||
.pipe(catchError((val) => {
|
||
console.log(val);
|
||
return 'Error';
|
||
}),)
|
||
.subscribe(async (result) => {
|
||
if (result) {
|
||
await ctx.reply(
|
||
`🤟 Все путем, ты уже зарегистрирован, расслабься и жди указаний\r\nМожет быть`,
|
||
);
|
||
this.globalCmd.printCommands(ctx);
|
||
} else {
|
||
let reply = `👋 Привет, ${ctx.message.from.first_name}\\!\r\n`;
|
||
reply +=
|
||
'Я не вижу тебя в списке зарегистрированных участников, пройдем регистрацию?';
|
||
await ctx.replyWithMarkdownV2(reply, {
|
||
reply_markup: {
|
||
keyboard: [[{text: Messages.IM_IN}]],
|
||
},
|
||
});
|
||
}
|
||
});
|
||
}
|
||
@Hears(Messages.IM_IN)
|
||
async onRegisterCommand(@Ctx() ctx: Context): Promise<void> {
|
||
await ctx.scene.enter(REGISTER_SCENE_ID);
|
||
}
|
||
|
||
@Hears(Messages.GO)
|
||
async onGoCommand(@Ctx() ctx: Context) {
|
||
await ctx.scene.enter(QUIZ_SCENE);
|
||
|
||
}
|
||
|
||
@Command('photo')
|
||
async onPhotoCommand(@Ctx() ctx: Context) {
|
||
await ctx.scene.enter(REGISTER_PHOTO_SCENE);
|
||
}
|
||
|
||
@Command('cards')
|
||
async onCardCommand(@Ctx() ctx: Context) {
|
||
this.gameService.emit({ cmd: 'GetCards'}, { user: ctx.from.id, inline: false});
|
||
}
|
||
|
||
@Command('question')
|
||
async onQuestionCommand(@Ctx() ctx: Context) {
|
||
this.gameService.emit({ cmd: 'GetQuestion'}, { user: ctx.from.id, inline: false });
|
||
}
|
||
|
||
@Command('next')
|
||
async onNextCommand(@Ctx() ctx: Context) {
|
||
if(ctx.from.id === 11178819) {
|
||
this.gameService.emit({ cmd: 'CompleteQueue'}, { user: ctx.from.id })
|
||
}
|
||
}
|
||
|
||
@On('callback_query')
|
||
async onInlineQuery(@Ctx() ctx: any) {
|
||
await TGHandlers.handleCallback(ctx, this.logger, this.gameService);
|
||
}
|
||
|
||
@Hears(Messages.CHANGE_PHOTO)
|
||
onChangePhoto(@Ctx() ctx: Context) {
|
||
ctx.scene.enter(REGISTER_PHOTO_SCENE);
|
||
}
|
||
|
||
@On('text')
|
||
async onMsg(@Message('text') msg: string, @Ctx() ctx: Context) {
|
||
const chars = [...msg];
|
||
if (['1', '2', '3', '4'].includes(chars[0])) {
|
||
ctx.scene.enter(QUIZ_SCENE, { answering: true }, false);
|
||
}
|
||
if (msg.includes(Messages.EMOJI_CARD)) {
|
||
ctx.scene.enter(QUIZ_SCENE, { answering: true}, false);
|
||
}
|
||
}
|
||
|
||
@On("callback_query")
|
||
async onQuery(@Ctx() ctx: Context) {
|
||
console.log(ctx);
|
||
}
|
||
}
|
||
|
||
/*
|
||
|
||
|
||
@Help()
|
||
async helpCommand(@Ctx() ctx: Context) {
|
||
await ctx.reply('ты пидор');
|
||
}
|
||
|
||
@On('sticker')
|
||
async onSticker(@Ctx() ctx: Context) {
|
||
console.log(ctx.message.from);
|
||
await ctx.reply('👍');
|
||
}
|
||
|
||
|
||
|
||
|
||
*/
|