Added user state (nested menus) and sub-menu to control different potential problems and system status

This commit is contained in:
ftc
2022-04-28 09:10:41 +03:00
parent 23b6ae390e
commit 2e03e8b747
7 changed files with 375 additions and 132 deletions
+74
View File
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.ReplyMarkups;
using ZaoBot.OpenHab;
namespace ZaoBot
{
class StatusMenuUserState : IUserState
{
public const string CMD_BACK = "Назад";
ITelegramBotClient botClient;
private JsonDocument config;
HttpClient http = new HttpClient();
OpenHabClient openhab;
public StatusMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab)
{
this.botClient = botClient;
this.config = config;
this.openhab = openhab;
}
public async Task<IUserState> ProcessMessage(IUserState previous, MessageEventArgs e)
{
var response = "";
if (previous != this)
{
response = "Возможные проблемы:\n";
var items = await openhab.GetItems();
foreach (var item in items)
{
if (!item.name.StartsWith("Problem_")) continue;
response += item.label + ": ";
var state = await openhab.GetItemState(item.name);
if (state.state == "OFF") response += "OK";
else if (state.state == "ON") response += "PROBLEM!";
else response += "UNKNOWN";
response += "\n";
}
}
else if (e.Message.Text == CMD_BACK)
{
var newState = new MainMenuUserState(botClient, config, openhab);
await newState.ProcessMessage(this, e);
return newState;
}
else
{
response = "Я не понял";
}
var replyKeyboardMarkup = new ReplyKeyboardMarkup(
new KeyboardButton[][]
{
new KeyboardButton[] { CMD_BACK },
},
resizeKeyboard: true
);
await botClient.SendTextMessageAsync(
chatId: e.Message.Chat,
text: response,
replyMarkup: replyKeyboardMarkup
);
return this;
}
}
}