refact
This commit is contained in:
parent
290e283721
commit
7f906091fa
3 changed files with 36 additions and 27 deletions
|
|
@ -23,6 +23,7 @@ import {Inject, Logger} from "@nestjs/common";
|
||||||
import AppConsts from "../constants";
|
import AppConsts from "../constants";
|
||||||
import {ClientProxy} from "@nestjs/microservices";
|
import {ClientProxy} from "@nestjs/microservices";
|
||||||
import {catchError} from "rxjs";
|
import {catchError} from "rxjs";
|
||||||
|
import {TGHandlers} from "./handlers";
|
||||||
|
|
||||||
@Update()
|
@Update()
|
||||||
export class BotUpdate {
|
export class BotUpdate {
|
||||||
|
|
@ -95,31 +96,7 @@ export class BotUpdate {
|
||||||
|
|
||||||
@On('callback_query')
|
@On('callback_query')
|
||||||
async onInlineQuery(@Ctx() ctx: any) {
|
async onInlineQuery(@Ctx() ctx: any) {
|
||||||
let data = undefined;
|
await TGHandlers.handleCallback(ctx, this.logger, this.gameService);
|
||||||
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)
|
@Hears(Messages.CHANGE_PHOTO)
|
||||||
|
|
|
||||||
32
src/bot/handlers.ts
Normal file
32
src/bot/handlers.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
import {Logger} from "@nestjs/common";
|
||||||
|
import {ClientProxy} from "@nestjs/microservices";
|
||||||
|
|
||||||
|
export class TGHandlers {
|
||||||
|
static async handleCallback(ctx:any, logger: Logger, gameService: ClientProxy) {
|
||||||
|
let data = undefined;
|
||||||
|
try {
|
||||||
|
data = JSON.parse(ctx.update.callback_query.data);
|
||||||
|
} catch(error) {
|
||||||
|
logger.verbose(`[onInlineQuery]: can't parse json callback, checking content for other payload`);
|
||||||
|
}
|
||||||
|
if(data) {
|
||||||
|
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;
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import { Messages } from '../tg.text';
|
||||||
import {Context} from "../context.interface";
|
import {Context} from "../context.interface";
|
||||||
import AppConsts from "../../constants";
|
import AppConsts from "../../constants";
|
||||||
import {ClientProxy} from "@nestjs/microservices";
|
import {ClientProxy} from "@nestjs/microservices";
|
||||||
|
import {TGHandlers} from "../handlers";
|
||||||
|
|
||||||
@Scene(QUIZ_SCENE)
|
@Scene(QUIZ_SCENE)
|
||||||
export class QuizScene {
|
export class QuizScene {
|
||||||
|
|
@ -50,8 +51,7 @@ export class QuizScene {
|
||||||
|
|
||||||
@On('callback_query')
|
@On('callback_query')
|
||||||
async onInlineQuery(@Ctx() ctx: Context) {
|
async onInlineQuery(@Ctx() ctx: Context) {
|
||||||
|
await TGHandlers.handleCallback(ctx, this.logger, this.gameService);
|
||||||
this.logger.verbose(`emit callback for ${ctx.callbackQuery}`);
|
|
||||||
}
|
}
|
||||||
@On('text')
|
@On('text')
|
||||||
async onText(@Message('text') text: string, @Ctx() ctx: Context) {
|
async onText(@Message('text') text: string, @Ctx() ctx: Context) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue