H
Size: a a a
H
DK
i
H
H
DK
let root = new TelegramButton({id: 'Меню', title: 'Меню', action: function (user) {
sendTo('telegram', new DeleteResponse().build(user, []));
return new Response()
.data(function (text) {
text.addTitle('Система управления домом');
});
}});
root.addMenu({id: 'menu', title: 'Меню', action: function () {
return new EditResponse()
.data(function (text) {
text.addTitle('Кухня')
.addState('Температура', 'zigbee.0.00158d0002734196.temperature').mod('°C')
.br()
.addTitle('Рабочая')
.addState('CO2', WorkRoom.co2).mod('ppm')
.addState('Температура', WorkRoom.temperature).mod('°C')
;
});
}});
let menu = root.find('menu');
menu
.addMenu({id: 'kitchen_room', title: "Кухня", action: function () {
return new EditResponse()
.data(function (text) {
text.addTitle('Кухня')
.addState('Влажность', 'zigbee.0.00158d0002734196.humidity').mod('%')
.addState('Температура', 'zigbee.0.00158d0002734196.temperature').mod('°C')
.addState('Освещенность','zigbee.0.00158d0002b6ffb9.illuminance').mod('lux')
.addState('Плита Влажность', 'zigbee.0.00158d000358bc6e.humidity').mod('%')
.addState('Плита Температура', 'zigbee.0.00158d000358bc6e.temperature').mod('°C')
.addBState('Свет общий', 'zigbee.0.00158d00029a862b.right_state', 'Вкл 💡', 'Выкл')
.addBState('Свет подсветка', 'zigbee.0.00158d00024f2e16.state', 'Вкл 💡', 'Выкл')
;
});
}})
;
menu.find('kitchen_room')
.addMenu({id: 'kitchen_room_main_light', title: 'Свет общий', runParent: true, delay: 1000, action: function () {
let stateName = 'mihome.0.devices.ctrl_neutral2_158d00029a862b.channel_1';
setState(stateName, !getState(stateName).val);
}})
.addMenu({id: 'kitchen_room_light', title: "Свет лента", runParent: true, delay: 1000, action: function () {
let stateName = 'mihome.0.devices.plug_158d00024f2e16.state';
setState(stateName, !getState(stateName).val);
}})
.backButton()
;
root.listen();
DK
DK
DK
class Response {
constructor() {
this.stringBuilder = new StringBuilder();
}
data(calback) {
calback(this.stringBuilder);
return this;
}
build(user, menu) {
return {
user: user,
text: new Date() + "\n\n" + this.stringBuilder.build(),
reply_markup: {
inline_keyboard: menu
}
}
}
}
class EditResponse extends Response {
build(user, menu) {
return {
user: user,
text: new Date() + "\n\n" + this.stringBuilder.build(),
editMessageText: {
options: {
chat_id: getState("telegram.0.communicate.requestChatId").val,
message_id: getState("telegram.0.communicate.requestMessageId").val,
parse_mode: 'markdown',
reply_markup: {inline_keyboard: menu}
}
}
}
}
}
class DeleteResponse extends Response {
build(user, menu) {
return {
user: user,
text: this.stringBuilder.build(),
deleteMessage: {
options: {
chat_id: getState("telegram.0.communicate.requestChatId").val,
message_id: getState("telegram.0.communicate.requestMessageId").val,
}
}
}
}
}
class ClearMenuResponse extends Response {
build(user, menu) {
log("Удаляем клавиатуру.");
return {
user: user,
text: new Date() + "\n\n" + this.stringBuilder.build(),
reply_markup: {
// inline_keyboard: menu
//keyboard: [[]],
//resize_keyboard: true,
remove_keyboard: true
}
}
}
}
class TelegramButton {
constructor(option = null) {
this.option = {
id: "",
title: "",
parent: null,
menu: new Map(),
action: function () {},
runParent: false,
delay: 1
};
Object.assign(this.option, option);
}
addMenu(option) {
option.parent = this;
let menu = new TelegramButton(option);
this.option.menu.set(option.id, menu);
return this;
}
parent() {
return this.option.parent;
}
run(user) {
return this.option.action(user);
}
backButton() {
return this.addMenu({id: this.parent().option.id, title: "◀️ Назад"});
}
find(command) {
if (this.option.id === command) {
return this;
}
let object = null;
this.option.menu.forEach(function (item) {
if (item.find(command)) {
object = item.find(command);
}
});
return object;
}
sendToUser(user) {
let response = this.run(user);
sendTo('telegram', response.build(null, this.keyboard()));
}
keyboard() {
let menu = [];
this.option.menu.forEach(function (item) {
menu.push({
text: item.option.title,
callback_data: item.option.id
});
});
let size = 3;
let subarray = [];
for (let i = 0; i <Math.ceil(menu.length/size); i++){
subarray[i] = menu.slice((i*size), (i*size) + size);
}
return subarray;
}
listen() {
if (this.option.parent) {
throw new Error('Только конрневой элемент может начать обрабатывать запросы');
}
let self = this;
on({id: "telegram.0.communicate.request", change: 'any'}, function (obj) {
let command = obj.state.val.substring(obj.state.val.indexOf(']') + 1);
let user = obj.state.val.substring(obj.state.val.indexOf('[') + 1, obj.state.val.indexOf(']'));
DK
let action = self.find(command);
if (action) {
let delay = action.option.delay;
if (action.option.runParent) {
action.run(user);
action = action.option.parent;
}
setTimeout(function () {
let response = action.run(user);
sendTo('telegram', response.build(user, action.keyboard()))
}, delay);
}
});
}
}
АК
В
H
H
В
H
В
H
H
В