Added user state (nested menus) and sub-menu to control different potential problems and system status
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
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 MainMenuUserState : IUserState
|
||||
{
|
||||
const string CMD_WEATHER = "Погода";
|
||||
const string CMD_STATUS = "Статус";
|
||||
const string CMD_LEFT_GATE = "Открой левые ворота";
|
||||
const string CMD_RIGHT_GATE = "Открой правые ворота";
|
||||
|
||||
ITelegramBotClient botClient;
|
||||
private JsonDocument config;
|
||||
HttpClient http = new HttpClient();
|
||||
OpenHabClient openhab;
|
||||
|
||||
public MainMenuUserState(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 (e != null)
|
||||
{
|
||||
if (!VerifyUser(e.Message.Chat.Id, e.Message.Chat.Username))
|
||||
{
|
||||
Console.WriteLine($"Received message {e.Message.Text} from invalid user {e.Message.Chat.Username}");
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: e.Message.Chat,
|
||||
text: "Ваш ID " + e.Message.Chat.Id + " сообщите его администратору"
|
||||
);
|
||||
return this;
|
||||
}
|
||||
Console.WriteLine($"Received a text message in chat {e.Message.Chat.Id}.");
|
||||
|
||||
if (e.Message.Text == CMD_LEFT_GATE)
|
||||
{
|
||||
response = await OpenLeftGate();
|
||||
}
|
||||
else if (e.Message.Text == CMD_RIGHT_GATE)
|
||||
{
|
||||
response = await OpenRightGate();
|
||||
}
|
||||
else if (e.Message.Text == CMD_WEATHER)
|
||||
{
|
||||
response = await ShowWeather();
|
||||
}
|
||||
else if (e.Message.Text == CMD_STATUS)
|
||||
{
|
||||
var newState = new StatusMenuUserState(botClient, config, openhab);
|
||||
await newState.ProcessMessage(this, e);
|
||||
return newState;
|
||||
}
|
||||
else if (e.Message.Text == "")
|
||||
{
|
||||
response = "Превед!";
|
||||
}
|
||||
else if (previous != this) response = "Главное меню";
|
||||
else response = "Я не понял :-(";
|
||||
|
||||
if (e.Message.Text != "") response += "\nЧто дальше?";
|
||||
else response = "\nЧего изволите?";
|
||||
}
|
||||
|
||||
var replyKeyboardMarkup = new ReplyKeyboardMarkup(
|
||||
new KeyboardButton[][]
|
||||
{
|
||||
new KeyboardButton[] { CMD_WEATHER },
|
||||
new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE },
|
||||
new KeyboardButton[] { CMD_STATUS },
|
||||
},
|
||||
resizeKeyboard: true
|
||||
);
|
||||
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: e.Message.Chat,
|
||||
text: response,
|
||||
replyMarkup: replyKeyboardMarkup
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
private async Task<string> ShowWeather()
|
||||
{
|
||||
try
|
||||
{
|
||||
string response = "";
|
||||
foreach (var element in config.RootElement.GetProperty("items_weather").EnumerateObject())
|
||||
{
|
||||
var result = await openhab.GetItemState(element.Name);
|
||||
response += element.Value + ": " + ParseState(result) + "\n";
|
||||
}
|
||||
return response;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return "Fail: " + e.Message;
|
||||
}
|
||||
}
|
||||
|
||||
private string ParseState(ItemState state)
|
||||
{
|
||||
float value;
|
||||
if (!float.TryParse(state.state, out value))
|
||||
{
|
||||
return "null";
|
||||
}
|
||||
try
|
||||
{
|
||||
return printf.Printf.sprintf(state.pattern, value);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> OpenRightGate()
|
||||
{
|
||||
return await openhab.PostItemState(config.RootElement.GetProperty("item_right_gate").GetString(), "ON");
|
||||
}
|
||||
|
||||
private async Task<string> OpenLeftGate()
|
||||
{
|
||||
return await openhab.PostItemState(config.RootElement.GetProperty("item_left_gate").GetString(), "ON");
|
||||
}
|
||||
|
||||
private bool VerifyUser(long id, string username)
|
||||
{
|
||||
Console.WriteLine("Verify user " + username + " id " + id);
|
||||
foreach (var element in config.RootElement.GetProperty("valid_users").EnumerateArray())
|
||||
{
|
||||
if (username == element.GetString())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
foreach (var element in config.RootElement.GetProperty("valid_user_ids").EnumerateArray())
|
||||
{
|
||||
if (id.ToString() == element.GetString())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user