umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
134 lines (133 loc) • 4.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TelegramRequest = void 0;
const Request_1 = require("./request/Request");
const mmApp_1 = require("../mmApp");
const util_1 = require("../utils/standard/util");
class TelegramRequest {
API_ENDPOINT = 'https://api.telegram.org/bot';
_request;
_error;
token;
constructor() {
this._request = new Request_1.Request();
this._request.maxTimeQuery = 5500;
this.token = null;
this._error = null;
if (typeof mmApp_1.mmApp.params.telegram_token !== 'undefined') {
this.initToken(mmApp_1.mmApp.params.telegram_token);
}
}
initToken(token) {
this.token = token;
}
_getUrl() {
return `${this.API_ENDPOINT}${mmApp_1.mmApp.params.telegram_token}/`;
}
_initPostFile(type, file) {
if ((0, util_1.isFile)(file)) {
this._request.post[type] = Request_1.Request.getAttachFile(file);
}
else {
this._request.post[type] = file;
}
}
async call(method, userId = null) {
if (userId) {
this._request.post.chat_id = userId;
}
if (this.token) {
if (method) {
const data = await this._request.send(this._getUrl() + method);
if (data.status && data.data) {
if (!data.data.ok) {
this._error = data.data.description;
this._log();
return null;
}
return data.data;
}
this._log(data.err);
}
}
else {
this._log('Не указан telegram токен!');
}
return null;
}
sendMessage(chatId, message, params = null) {
this._request.post = {
chat_id: chatId,
text: message,
};
if (params) {
this._request.post = { ...params, ...this._request.post };
}
return this.call('sendMessage');
}
sendPoll(chatId, question, options, params = null) {
this._request.post = {
chat_id: chatId,
question,
};
let isSend = true;
if (options) {
const countOptions = options.length;
if (countOptions > 1) {
if (countOptions > 10) {
options = options.slice(0, 10);
}
this._request.post.options = JSON.stringify(options);
}
else {
isSend = false;
}
}
if (isSend) {
if (params) {
this._request.post = { ...params, ...this._request.post };
}
return this.call('sendPoll');
}
else {
this._log('Недостаточное количество вариантов. Должно быть от 2 - 10 вариантов!');
return null;
}
}
sendPhoto(userId, file, desc = null, params = null) {
this._initPostFile('photo', file);
if (desc) {
this._request.post.caption = desc;
}
if (params) {
this._request.post = { ...params, ...this._request.post };
}
return this.call('sendPhoto', userId);
}
sendDocument(userId, file, params = null) {
this._initPostFile('document', file);
if (params) {
this._request.post = { ...params, ...this._request.post };
}
return this.call('sendDocument', userId);
}
sendAudio(userId, file, params = null) {
this._initPostFile('audio', file);
if (params) {
this._request.post = { ...params, ...this._request.post };
}
return this.call('sendAudio', userId);
}
sendVideo(userId, file, params = null) {
this._initPostFile('video', file);
if (params) {
this._request.post = { ...params, ...this._request.post };
}
return this.call('sendVideo', userId);
}
_log(error = '') {
error = `\n(${Date.now()}): Произошла ошибка при отправке запроса по адресу: ${this._request.url}\nОшибка:\n${error}\n${this._error}\n`;
mmApp_1.mmApp.saveLog('telegramApi.log', error);
}
}
exports.TelegramRequest = TelegramRequest;