tgd-telegram-service/src/bot/bot.update.ts
2024-11-12 21:40:25 +04:00

164 lines
5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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";
@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) {
let data = undefined;
try {
data = JSON.parse(ctx.update.callback_query.data);
} catch(error) {
this.logger.verbose(`[onInlineQuery]: can't parse json callback, checking content for other payload`);
}
if(data) {
this.gameService.emit({ cmd: 'ApplyDebuff'}, { ...ctx.callbackQuery, from: ctx.from.id });
await ctx.editMessageReplyMarkup(undefined);
await ctx.editMessageText("ответ принят!");
}
if(ctx.update.callback_query.message.text.indexOf('Внимание') !== -1) {
const answer =ctx.update.callback_query.data;
this.gameService.emit({ cmd: "QuestionAnswer"}, { user: ctx.from.id, answer: answer, name: ctx.from.first_name });
await ctx.editMessageReplyMarkup(undefined);
await ctx.editMessageText("ответ принят!");
} else if(ctx.update.callback_query.data.startsWith('card/')) {
const cardtoplay = ctx.update.callback_query.data.substring('card/'.length);
this.gameService.emit({cmd: 'CardPlayed'}, {text: cardtoplay, user: ctx.update.callback_query.from.id})
await ctx.editMessageReplyMarkup(undefined);
await ctx.editMessageText("карта выбрана");
} else
{
//console.log(ctx.update);
}
}
@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('👍');
}
*/