98 lines
2.4 KiB
C#
98 lines
2.4 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
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.Config;
|
|
using ZaoBot.OpenHab;
|
|
using ZaoBot.Problems;
|
|
|
|
namespace ZaoBot
|
|
{
|
|
class StatusMenuUserState : IUserState
|
|
{
|
|
public const string CMD_BACK = "Назад";
|
|
public const string CMD_CONFIRM_ITEM = "✅ ";
|
|
|
|
ITelegramBotClient botClient;
|
|
IServiceProvider services;
|
|
StatusManager statusManager;
|
|
|
|
public StatusMenuUserState(IServiceProvider services, ITelegramBotClient botClient,
|
|
IConfig config,
|
|
OpenHabClient openhab,
|
|
StatusManager statusManager)
|
|
{
|
|
this.botClient = botClient;
|
|
this.services = services;
|
|
this.statusManager = statusManager;
|
|
}
|
|
|
|
public async Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e)
|
|
{
|
|
var response = "";
|
|
List<List<KeyboardButton>> buttons = new List<List<KeyboardButton>>();
|
|
if (previous != this)
|
|
{
|
|
response = "Возможные проблемы:\n";
|
|
foreach (var item in statusManager.StatusItems)
|
|
{
|
|
response += item.label + ": " + item.status + "\n";
|
|
}
|
|
var confirmButtons = new List<KeyboardButton>();
|
|
foreach (var item in statusManager.StatusItems)
|
|
{
|
|
if (item.notified)
|
|
{
|
|
confirmButtons.Add(GetConfirmText(item));
|
|
}
|
|
}
|
|
if (confirmButtons.Count > 0) buttons.Add(confirmButtons);
|
|
}
|
|
else if (e.Message.Text.StartsWith(CMD_CONFIRM_ITEM))
|
|
{
|
|
foreach (var item in statusManager.StatusItems)
|
|
{
|
|
if (e.Message.Text == GetConfirmText(item))
|
|
{
|
|
item.notified = false;
|
|
}
|
|
}
|
|
response = CMD_CONFIRM_ITEM;
|
|
}
|
|
else if (e.Message.Text == CMD_BACK)
|
|
{
|
|
var newState = services.GetRequiredService<MainMenuUserState>();
|
|
await newState.ProcessMessage(this, e);
|
|
return newState;
|
|
}
|
|
else
|
|
{
|
|
response = "Я не понял";
|
|
}
|
|
buttons.Add(new List<KeyboardButton>()
|
|
{
|
|
CMD_BACK
|
|
});
|
|
var replyKeyboardMarkup = new ReplyKeyboardMarkup(buttons);
|
|
|
|
await botClient.SendTextMessageAsync(
|
|
chatId: e.Message.Chat,
|
|
text: response,
|
|
replyMarkup: replyKeyboardMarkup
|
|
);
|
|
return this;
|
|
}
|
|
|
|
private string GetConfirmText(Problems.Item item)
|
|
{
|
|
return CMD_CONFIRM_ITEM + item.label;
|
|
}
|
|
}
|
|
}
|