UNPKG

umbot

Version:

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

163 lines (162 loc) 5.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VkRequest = void 0; const Request_1 = require("./request/Request"); const mmApp_1 = require("../mmApp"); class VkRequest { VK_API_VERSION = '5.103'; VK_API_ENDPOINT = 'https://api.vk.com/method/'; _vkApiVersion; _request; _error; token; isAttachContent; constructor() { this._request = new Request_1.Request(); this._request.maxTimeQuery = 5500; this.isAttachContent = false; if (mmApp_1.mmApp.params.vk_api_version) { this._vkApiVersion = mmApp_1.mmApp.params.vk_api_version; } else { this._vkApiVersion = this.VK_API_VERSION; } this.token = null; this._error = null; if (mmApp_1.mmApp.params.vk_token) { this.initToken(mmApp_1.mmApp.params.vk_token); } } initToken(token) { this.token = token; } async call(method) { if (this.token) { this._request.header = null; this._request.post.access_token = this.token; this._request.post.v = this._vkApiVersion; const data = await this._request.send(this.VK_API_ENDPOINT + method); if (data.status && data.data) { this._error = JSON.stringify(data.err || []); if (typeof data.data.error !== 'undefined') { this._error = JSON.stringify(data.data.error); this._log(); return null; } return data.data.response || data.data; } this._log(data.err); } else { this._log('Не указан vk токен!'); } return null; } async upload(url, file) { this._request.attach = file; this._request.isAttachContent = this.isAttachContent; this._request.header = Request_1.Request.HEADER_FORM_DATA; const data = await this._request.send(url); if (data.status && data.data) { if (typeof data.data.error !== 'undefined') { this._error = JSON.stringify(data.data.error); this._log(); return null; } return data.data; } this._log(data.err); return null; } async messagesSend(peerId, message, params = null) { const method = 'messages.send'; this._request.post = { peer_id: peerId, message, }; if (typeof peerId !== 'number') { this._request.post.domain = peerId; delete this._request.post.peer_id; } if (params) { if (typeof params.random_id !== 'undefined') { this._request.post.random_id = params.random_id; } else { this._request.post.random_id = Date.now(); } if (typeof params.attachments !== 'undefined') { this._request.post.attachment = params.attachments.join(','); delete params.attachments; } if (typeof params.template !== 'undefined') { if (typeof params.template !== 'string') { params.template = JSON.stringify(params.template); } this._request.post.template = params.template; delete params.template; } if (typeof params.keyboard !== 'undefined') { if (typeof this._request.post.template !== 'undefined') { await this.call(method); delete this._request.post.template; } if (typeof params.keyboard !== 'string') { params.template = JSON.stringify(params.keyboard); } this._request.post.keyboard = params.keyboard; delete params.keyboard; } if (Object.keys(params).length) { this._request.post = { ...params, ...this._request.post }; } } return await this.call(method); } async usersGet(userId, params = null) { if (typeof userId !== 'number') { this._request.post = { user_ids: userId }; } else { this._request.post = { user_id: userId }; } if (params) { this._request.post = { ...this._request.post, ...params }; } return this.call('users.get'); } async photosGetMessagesUploadServer(peerId) { this._request.post = { peer_id: peerId }; return this.call('photos.getMessagesUploadServer'); } async photosSaveMessagesPhoto(photo, server, hash) { this._request.post = { photo, server, hash, }; return this.call('photos.saveMessagesPhoto'); } async docsGetMessagesUploadServer(peerId, type) { this._request.post = { peer_id: peerId, type, }; return this.call('docs.getMessagesUploadServe'); } async docsSave(file, title, tags = null) { this._request.post = { file, title, }; if (tags) { this._request.post.tags = tags; } return this.call('docs.save'); } _log(error = '') { error = `\n(${Date}): Произошла ошибка при отправке запроса по адресу: ${this._request.url}\nОшибка:\n${error}\n${this._error}\n`; mmApp_1.mmApp.saveLog('vkApi.log', error); } } exports.VkRequest = VkRequest;