Добавил команду с выдачей погоды
This commit is contained in:
+72
-10
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
@@ -18,11 +20,13 @@ namespace ZaoBot
|
||||
static JsonDocument config;
|
||||
static HttpClient http = new HttpClient();
|
||||
|
||||
const string CMD_WEATHER = "Погода";
|
||||
const string CMD_LEFT_GATE = "Открой левые ворота";
|
||||
const string CMD_RIGHT_GATE = "Открой правые ворота";
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
config = LoadConfig();
|
||||
var token = config.RootElement.GetProperty("token").GetString();
|
||||
botClient = new TelegramBotClient(token);
|
||||
@@ -48,18 +52,32 @@ namespace ZaoBot
|
||||
}
|
||||
Console.WriteLine($"Received a text message in chat {e.Message.Chat.Id}.");
|
||||
|
||||
var response = "";
|
||||
if (e.Message.Text == CMD_LEFT_GATE)
|
||||
{
|
||||
await OpenLeftGate();
|
||||
response = await OpenLeftGate();
|
||||
}
|
||||
if (e.Message.Text == CMD_RIGHT_GATE)
|
||||
else if (e.Message.Text == CMD_RIGHT_GATE)
|
||||
{
|
||||
await OpenRightGate();
|
||||
response = await OpenRightGate();
|
||||
}
|
||||
else if (e.Message.Text == CMD_WEATHER)
|
||||
{
|
||||
response = await ShowWeather();
|
||||
}
|
||||
else if (e.Message.Text == "")
|
||||
{
|
||||
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 },
|
||||
},
|
||||
resizeKeyboard: true
|
||||
@@ -67,31 +85,75 @@ namespace ZaoBot
|
||||
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: e.Message.Chat,
|
||||
text: "Чего изволите?",
|
||||
text: response,
|
||||
replyMarkup: replyKeyboardMarkup
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task OpenRightGate()
|
||||
private static async Task<string> ShowWeather()
|
||||
{
|
||||
try
|
||||
{
|
||||
string response = "";
|
||||
foreach (var element in config.RootElement.GetProperty("items_weather").EnumerateObject())
|
||||
{
|
||||
var url = config.RootElement.GetProperty("openhab_url").GetString();
|
||||
url += element.Name;
|
||||
var result = await http.GetAsync(url);
|
||||
var content = await result.Content.ReadAsStringAsync();
|
||||
response += element.Value + ": " + ParseContent(content) + "\n";
|
||||
}
|
||||
return response;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return "Fail: " + e.Message;
|
||||
}
|
||||
}
|
||||
|
||||
private static string ParseContent(string content)
|
||||
{
|
||||
var doc = JsonDocument.Parse(content);
|
||||
float value;
|
||||
if (!float.TryParse(doc.RootElement.GetProperty("state").GetString(), out value))
|
||||
{
|
||||
return "null";
|
||||
}
|
||||
try
|
||||
{
|
||||
var stateDescription = doc.RootElement.GetProperty("stateDescription");
|
||||
var pattern = stateDescription.GetProperty("pattern");
|
||||
return printf.Printf.sprintf(pattern.GetString(), value);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<string> OpenRightGate()
|
||||
{
|
||||
var url = config.RootElement.GetProperty("openhab_url").GetString();
|
||||
url += config.RootElement.GetProperty("item_right_gate").GetString();
|
||||
await Post(url, "ON");
|
||||
return await Post(url, "ON");
|
||||
}
|
||||
|
||||
private static async Task Post(string url, string v)
|
||||
private static async Task<string> Post(string url, string v)
|
||||
{
|
||||
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(v));
|
||||
content.Headers.Add("Content-Type", "text/plain");
|
||||
await http.PostAsync(url, content);
|
||||
var result = await http.PostAsync(url, content);
|
||||
if (result.IsSuccessStatusCode) return "OK";
|
||||
else return "Fail. " + result.ReasonPhrase;
|
||||
|
||||
}
|
||||
|
||||
private static async Task OpenLeftGate()
|
||||
private static async Task<string> OpenLeftGate()
|
||||
{
|
||||
var url = config.RootElement.GetProperty("openhab_url").GetString();
|
||||
url += config.RootElement.GetProperty("item_left_gate").GetString();
|
||||
await Post(url, "ON");
|
||||
return await Post(url, "ON");
|
||||
}
|
||||
|
||||
private static bool VerifyUser(long id, string username)
|
||||
|
||||
Reference in New Issue
Block a user