UNPKG

umbot

Version:

Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber

84 lines (83 loc) 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vk = void 0; const TemplateTypeModel_1 = require("./TemplateTypeModel"); const mmApp_1 = require("../mmApp"); const VkRequest_1 = require("../api/VkRequest"); const button_1 = require("../components/button"); class Vk extends TemplateTypeModel_1.TemplateTypeModel { async init(query, controller) { if (query) { let content; if (typeof query === 'string') { content = JSON.parse(query); } else { content = { ...query }; } if (!this.controller) { this.controller = controller; } this.controller.requestObject = content; switch (content.type || null) { case 'confirmation': this.sendInInit = mmApp_1.mmApp.params.vk_confirmation_token; return true; case 'message_new': if (typeof content.object !== 'undefined') { const object = content.object; this.controller.userId = object.message.from_id; mmApp_1.mmApp.params.user_id = this.controller.userId; this.controller.userCommand = object.message.text.toLowerCase().trim(); this.controller.originalUserCommand = object.message.text.trim(); this.controller.messageId = object.message.id; this.controller.payload = object.message.payload || null; const user = await new VkRequest_1.VkRequest().usersGet(this.controller.userId); if (user) { const thisUser = { username: null, first_name: user.first_name || null, last_name: user.last_name || null, }; this.controller.nlu.setNlu({ thisUser }); } return true; } return false; default: this.error = 'Vk:init(): Некорректный тип данных!'; break; } } else { this.error = 'Vk:init(): Отправлен пустой запрос!'; } return false; } async getContext() { if (this.controller.isSend) { const keyboard = this.controller.buttons.getButtonJson(button_1.Buttons.T_VK_BUTTONS); const params = {}; if (keyboard) { params.keyboard = keyboard; } if (this.controller.card.images.length) { const attach = await this.controller.card.getCards(); if (attach.type) { params.template = attach; } else { params.attachments = attach; } } if (this.controller.sound.sounds.length) { const attach = await this.controller.sound.getSounds(this.controller.tts); params.attachments = { ...attach, ...params.attachments }; } const vkApi = new VkRequest_1.VkRequest(); await vkApi.messagesSend(this.controller.userId, this.controller.text, params); } return 'ok'; } } exports.Vk = Vk;