UNPKG

umbot

Version:

Мультиплатформенный фреймворк для создания голосовых навыков и чат-ботов с единой бизнес-логикой. Встроенная поддержка ВКонтакте, Telegram, Viber, MAX, Яндекс Алисы, Маруси и Сбера SmartApp. Архитектура на адаптерах позволяет подключать любые другие платф

71 lines (70 loc) 3.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSoundInDB = getSoundInDB; exports.soundProcessing = soundProcessing; const index_1 = require("../../../index"); const API_1 = require("../API"); const utils_1 = require("../Base/utils"); const constants_1 = require("./constants"); /** * Получение токена, необходимого для воспроизведения звуков в Vk * @param controller Контроллер приложения * @param path Путь до аудиофайла * @param isAttachContent Определяет передано ли содержимое файла или сам файл */ async function getSoundInDB(controller, path, isAttachContent = false) { return (0, utils_1.getSoundToken)(path, constants_1.T_VK, controller, async (model) => { const vkApi = new API_1.VkRequest(controller.appContext); vkApi.isAttachContent = isAttachContent; const uploadServerResponse = await vkApi.docsGetMessagesUploadServer(controller.userId, 'audio_message'); if (uploadServerResponse) { const uploadResponse = await vkApi.upload(uploadServerResponse.upload_url, path); if (uploadResponse) { const doc = await vkApi.docsSave(uploadResponse.file, 'Voice message'); if (doc) { model.soundToken = `doc${doc.owner_id}_${doc.id}`; if (await model.save(true)) { return model.soundToken; } } } } return null; }); } /** * Получение корректного ответа для озвучивания запроса пользователю VK * @param soundInfo Информация необходимая для обработки аудио * @param controller Контроллер приложения */ async function soundProcessing(soundInfo, controller) { const { sounds, text } = soundInfo; const data = []; if (sounds) { for (let i = 0; i < sounds.length; i++) { const sound = sounds[i]; if (sound.sounds !== undefined && sound.key !== undefined) { let sText = index_1.Text.getText(sound.sounds); if (index_1.Text.isUrl(sText) || (await (0, index_1.isFile)(sText))) { sText = await getSoundInDB(controller, sText); } if (sText) { data.push(sText); } } } } if (text) { const speechKit = new API_1.YandexSpeechKit(controller.appContext.appConfig.tokens[constants_1.T_VK].speech_kit_token, controller.appContext); const content = await speechKit.getTts(text); let sText = null; if (content) { sText = await getSoundInDB(controller, content.fileName, true); await (0, index_1.unlink)(content.fileName); } if (sText) { data.push(sText); } } return data; }