umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
144 lines (143 loc) • 5.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SoundTokens = void 0;
const Model_1 = require("./db/Model");
const mmApp_1 = require("../mmApp");
const api_1 = require("../api");
const Text_1 = require("../utils/standard/Text");
class SoundTokens extends Model_1.Model {
TABLE_NAME = 'SoundTokens';
static T_ALISA = 0;
static T_VK = 1;
static T_TELEGRAM = 2;
static T_MARUSIA = 3;
soundToken;
path;
type;
isAttachContent;
constructor() {
super();
this.soundToken = null;
this.path = null;
this.type = SoundTokens.T_ALISA;
this.isAttachContent = false;
}
tableName() {
return this.TABLE_NAME;
}
rules() {
return [
{
name: ['soundToken', 'path'],
type: 'string',
max: 150,
},
{
name: ['type'],
type: 'integer',
},
];
}
attributeLabels() {
return {
soundToken: 'ID',
path: 'Sound path',
type: 'Type',
};
}
async getToken() {
const where = {
path: this.path,
type: this.type,
};
switch (this.type) {
case SoundTokens.T_ALISA:
if (await this.whereOne(where)) {
return this.soundToken;
}
else {
const yImage = new api_1.YandexSoundRequest(mmApp_1.mmApp.params.yandex_token || null, mmApp_1.mmApp.params.app_id || null);
let res = null;
if (this.path) {
if (Text_1.Text.isUrl(this.path)) {
mmApp_1.mmApp.saveLog('SoundTokens.log', 'SoundTokens:getToken() - Нельзя отправить звук в навык для Алисы через url!');
return null;
}
else {
res = await yImage.downloadSoundFile(this.path);
}
}
if (res) {
this.soundToken = res.id;
if (await this.save(true)) {
return this.soundToken;
}
}
}
break;
case SoundTokens.T_VK:
if (await this.whereOne(where)) {
return this.soundToken;
}
else if (this.path) {
const vkApi = new api_1.VkRequest();
const uploadServerResponse = await vkApi.docsGetMessagesUploadServer(mmApp_1.mmApp.params.user_id, 'audio_message');
if (uploadServerResponse) {
const uploadResponse = await vkApi.upload(uploadServerResponse.upload_url, this.path);
if (uploadResponse) {
const doc = await vkApi.docsSave(uploadResponse.file, 'Voice message');
if (doc) {
this.soundToken = `doc${doc.owner_id}_${doc.id}`;
if (await this.save(true)) {
return this.soundToken;
}
}
}
}
}
break;
case SoundTokens.T_TELEGRAM: {
const telegramApi = new api_1.TelegramRequest();
if (await this.whereOne(where)) {
await telegramApi.sendAudio(mmApp_1.mmApp.params.user_id, this.soundToken);
return this.soundToken;
}
else if (this.path) {
const sound = await telegramApi.sendAudio(mmApp_1.mmApp.params.user_id, this.path);
if (sound && sound.ok && sound.result.audio) {
if (typeof sound.result.audio.file_id !== 'undefined') {
this.soundToken = sound.result.audio.file_id;
if (await this.save(true)) {
return this.soundToken;
}
}
}
}
break;
}
case SoundTokens.T_MARUSIA:
if (await this.whereOne(where)) {
return this.soundToken;
}
else if (this.path) {
const marusiaApi = new api_1.MarusiaRequest();
const uploadServerResponse = await marusiaApi.marusiaGetAudioUploadLink();
if (uploadServerResponse) {
const uploadResponse = await marusiaApi.upload(uploadServerResponse.audio_upload_link, this.path);
if (uploadResponse) {
const doc = await marusiaApi.marusiaCreateAudio(uploadResponse);
if (doc) {
this.soundToken = doc.id;
if (await this.save(true)) {
return this.soundToken;
}
}
}
}
}
return null;
}
return null;
}
}
exports.SoundTokens = SoundTokens;