umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
142 lines (141 loc) • 5.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageTokens = 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 ImageTokens extends Model_1.Model {
TABLE_NAME = 'ImageTokens';
static T_ALISA = 0;
static T_VK = 1;
static T_TELEGRAM = 2;
static T_MARUSIA = 3;
imageToken;
path;
type;
caption;
constructor() {
super();
this.imageToken = null;
this.path = null;
this.type = ImageTokens.T_ALISA;
this.caption = null;
}
tableName() {
return this.TABLE_NAME;
}
rules() {
return [
{
name: ['imageToken', 'path'],
type: 'string',
max: 150,
},
{
name: ['type'],
type: 'integer',
},
];
}
attributeLabels() {
return {
imageToken: 'ID',
path: 'Image path',
type: 'Type',
};
}
async getToken() {
const where = { path: this.path, type: this.type };
switch (this.type) {
case ImageTokens.T_ALISA:
if (await this.whereOne(where)) {
return this.imageToken;
}
else {
const yImage = new api_1.YandexImageRequest(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)) {
res = await yImage.downloadImageUrl(this.path);
}
else {
res = await yImage.downloadImageFile(this.path);
}
}
if (res) {
this.imageToken = res.id;
if (await this.save(true)) {
return this.imageToken;
}
}
}
break;
case ImageTokens.T_MARUSIA:
where.type = ImageTokens.T_MARUSIA;
if (await this.whereOne(where)) {
return this.imageToken;
}
else if (this.path) {
const marusiaApi = new api_1.MarusiaRequest();
const uploadServerResponse = await marusiaApi.marusiaGetPictureUploadLink();
if (uploadServerResponse) {
const uploadResponse = await marusiaApi.upload(uploadServerResponse.picture_upload_link, this.path);
if (uploadResponse) {
const photo = await marusiaApi.marusiaSavePicture(uploadResponse.photo, uploadResponse.server, uploadResponse.hash);
if (photo) {
this.imageToken = photo.photo_id;
if (await this.save(true)) {
return this.imageToken;
}
}
}
}
}
break;
case ImageTokens.T_VK:
where.type = ImageTokens.T_VK;
if (await this.whereOne(where)) {
return this.imageToken;
}
else if (this.path) {
const vkApi = new api_1.VkRequest();
const uploadServerResponse = await vkApi.photosGetMessagesUploadServer(mmApp_1.mmApp.params.user_id);
if (uploadServerResponse) {
const uploadResponse = await vkApi.upload(uploadServerResponse.upload_url, this.path);
if (uploadResponse) {
const photo = await vkApi.photosSaveMessagesPhoto(uploadResponse.photo, uploadResponse.server, uploadResponse.hash);
if (photo) {
this.imageToken = `photo${photo.owner_id}_${photo.id}`;
if (await this.save(true)) {
return this.imageToken;
}
}
}
}
}
break;
case ImageTokens.T_TELEGRAM: {
const telegramApi = new api_1.TelegramRequest();
if (await this.whereOne(where)) {
await telegramApi.sendPhoto(mmApp_1.mmApp.params.user_id, this.imageToken, this.caption);
return this.imageToken;
}
else if (this.path) {
const photo = await telegramApi.sendPhoto(mmApp_1.mmApp.params.user_id, this.path, this.caption);
if (photo && photo.ok && photo.result.photo) {
if (typeof photo.result.photo.file_id !== 'undefined') {
this.imageToken = photo.result.photo.file_id;
if (await this.save(true)) {
return this.imageToken;
}
}
}
}
break;
}
}
return null;
}
}
exports.ImageTokens = ImageTokens;