universal_bot-ts
79 строк · 3.2 Кб
1import {TemplateTypeModel, BotController, mmApp, Buttons} from '../../../../../src';
2import {UserButton} from '../Components/UserButton';
3import {UserCard} from '../Components/UserCard';
4import {UserSound} from '../Components/UserSound';
5
6interface IUserApp {
7userId: string;
8data: {
9text: string
10};
11}
12
13export class UserApp extends TemplateTypeModel {
14/**
15* Инициализация параметров
16*
17* @param query
18* @param controller
19* @return bool
20* @see TemplateTypeModel::init() Смотри тут
21*/
22public async init(query: string | IUserApp, controller: BotController): Promise<boolean> {
23if (query) {
24let content: IUserApp;
25if (typeof query === 'string') {
26content = <IUserApp>JSON.parse(query);
27} else {
28content = {...query}
29}
30this.controller = controller;
31this.controller.requestObject = content;
32/**
33* Инициализация основных параметров приложения
34*/
35this.controller.userCommand = content.data.text;
36this.controller.originalUserCommand = content.data.text;
37
38this.controller.userId = 'Идентификатор пользователя. Берется из content';
39mmApp.params.user_id = this.controller.userId;
40return true;
41} else {
42this.error = 'UserApp:init(): Отправлен пустой запрос!';
43}
44return false;
45}
46
47/**
48* Отправка ответа пользователю
49*
50* @return {Promise<string>}
51* @see TemplateTypeModel::getContext() Смотри тут
52*/
53public async getContext(): Promise<string> {
54// Проверяем отправлять ответ пользователю или нет
55if (this.controller.isSend) {
56/**
57* Отправляем ответ в нужном формате
58*/
59const buttonClass = new UserButton();// Класс отвечающий за отображение кнопок. Должен быть унаследован от TemplateButtonTypes
60/*
61* Получение кнопок
62*/
63const buttons = this.controller.buttons.getButtons(Buttons.T_USER_APP_BUTTONS, buttonClass);
64
65const cardClass = new UserCard();// Класс отвечающий за отображение карточек. Должен быть унаследован от TemplateCardTypes
66/*
67* Получить информацию о карточке
68*/
69const cards = await this.controller.card.getCards(cardClass);
70
71const soundClass = new UserSound();// Класс отвечающий за отображение звуков. Должен быть унаследован от TemplateSoundTypes
72/*
73* Получить все звуки
74*/
75const sounds = await this.controller.sound.getSounds('', soundClass);
76}
77return 'ok';
78}
79}
80