umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
66 lines (65 loc) • 2.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Telegram = void 0;
const TemplateTypeModel_1 = require("./TemplateTypeModel");
const mmApp_1 = require("../mmApp");
const api_1 = require("../api");
const button_1 = require("../components/button");
class Telegram extends TemplateTypeModel_1.TemplateTypeModel {
async init(query, controller) {
if (query) {
let content;
if (typeof query === 'string') {
content = JSON.parse(query);
}
else {
content = { ...query };
}
if (!this.controller) {
this.controller = controller;
}
this.controller.requestObject = content;
if (typeof content.message !== 'undefined') {
this.controller.userId = content.message.chat.id;
mmApp_1.mmApp.params.user_id = this.controller.userId;
this.controller.userCommand = content.message.text.toLowerCase().trim();
this.controller.originalUserCommand = content.message.text;
this.controller.messageId = content.message.message_id;
const thisUser = {
username: content.message.chat.username || null,
first_name: content.message.chat.first_name || null,
last_name: content.message.chat.last_name || null,
};
this.controller.nlu.setNlu({ thisUser });
return true;
}
}
else {
this.error = 'Telegram:init(): Отправлен пустой запрос!';
}
return false;
}
async getContext() {
if (this.controller.isSend) {
const telegramApi = new api_1.TelegramRequest();
const params = {};
const keyboard = this.controller.buttons.getButtonJson(button_1.Buttons.T_TELEGRAM_BUTTONS);
if (keyboard) {
params.reply_markup = keyboard;
}
params.parse_mode = 'markdown';
await telegramApi.sendMessage(this.controller.userId, this.controller.text, params);
if (this.controller.card.images.length) {
const res = await this.controller.card.getCards();
if (res) {
await telegramApi.sendPoll(this.controller.userId, res.question, res.options);
}
}
if (this.controller.sound.sounds.length) {
await this.controller.sound.getSounds(this.controller.tts);
}
}
return 'ok';
}
}
exports.Telegram = Telegram;