umbot
Version:
Мультиплатформенный фреймворк для создания голосовых навыков и чат-ботов с единой бизнес-логикой. Встроенная поддержка ВКонтакте, Telegram, Viber, MAX, Яндекс Алисы, Маруси и Сбера SmartApp. Архитектура на адаптерах позволяет подключать любые другие платф
68 lines (67 loc) • 2.92 kB
JavaScript
;
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");
/**
* Получение токена, необходимого для воспроизведения звуков в Telegram
* @param controller Контроллер приложения
* @param path Путь до аудиофайла
*/
async function getSoundInDB(controller, path) {
let isCbCalled = false;
const result = await (0, utils_1.getSoundToken)(path, constants_1.T_TELEGRAM, controller, async (model) => {
const api = new API_1.TelegramRequest(controller.appContext);
const sound = await api.sendAudio(controller.userId, path);
isCbCalled = true;
if (sound?.ok && sound.result?.audio?.file_id !== undefined) {
model.soundToken = sound.result.audio.file_id;
if (await model.save(true)) {
return model.soundToken;
}
}
return null;
});
if (!isCbCalled && result) {
await new API_1.TelegramRequest(controller.appContext).sendAudio(controller.userId, result);
}
return result;
}
/**
* Получение корректного ответа для озвучивания запроса пользователю Telegram
* @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);
}
else {
await new API_1.TelegramRequest(controller.appContext).sendAudio(controller.userId, sText);
}
if (sText) {
data.push(sText);
}
}
}
}
if (text) {
const speechKit = new API_1.YandexSpeechKit(controller.appContext.appConfig.tokens[constants_1.T_TELEGRAM].speech_kit_token + '', controller.appContext);
const content = await speechKit.getTts(text);
if (content) {
await new API_1.TelegramRequest(controller.appContext).sendAudio(controller.userId, content.fileName);
await (0, index_1.unlink)(content.fileName);
}
}
return data;
}