Initial commit, added bot to control garage gates
This commit is contained in:
+127
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace ZaoBot
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static ITelegramBotClient botClient;
|
||||
static JsonDocument config;
|
||||
static HttpClient http = new HttpClient();
|
||||
|
||||
const string CMD_LEFT_GATE = "Открой левые ворота";
|
||||
const string CMD_RIGHT_GATE = "Открой правые ворота";
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
config = LoadConfig();
|
||||
var token = config.RootElement.GetProperty("token").GetString();
|
||||
botClient = new TelegramBotClient(token);
|
||||
var me = botClient.GetMeAsync().Result;
|
||||
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
|
||||
botClient.OnMessage += OnBotMessage;
|
||||
botClient.StartReceiving();
|
||||
while (true) Thread.Sleep(10000);
|
||||
}
|
||||
|
||||
private static async void OnBotMessage(object sender, MessageEventArgs e)
|
||||
{
|
||||
if (e.Message.Text != 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;
|
||||
}
|
||||
Console.WriteLine($"Received a text message in chat {e.Message.Chat.Id}.");
|
||||
|
||||
if (e.Message.Text == CMD_LEFT_GATE)
|
||||
{
|
||||
await OpenLeftGate();
|
||||
}
|
||||
if (e.Message.Text == CMD_RIGHT_GATE)
|
||||
{
|
||||
await OpenRightGate();
|
||||
}
|
||||
|
||||
var replyKeyboardMarkup = new ReplyKeyboardMarkup(
|
||||
new KeyboardButton[][]
|
||||
{
|
||||
new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE },
|
||||
},
|
||||
resizeKeyboard: true
|
||||
);
|
||||
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: e.Message.Chat,
|
||||
text: "Чего изволите?",
|
||||
replyMarkup: replyKeyboardMarkup
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task OpenRightGate()
|
||||
{
|
||||
var url = config.RootElement.GetProperty("openhab_url").GetString();
|
||||
url += config.RootElement.GetProperty("item_right_gate").GetString();
|
||||
await Post(url, "ON");
|
||||
}
|
||||
|
||||
private static async Task 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);
|
||||
}
|
||||
|
||||
private static async Task OpenLeftGate()
|
||||
{
|
||||
var url = config.RootElement.GetProperty("openhab_url").GetString();
|
||||
url += config.RootElement.GetProperty("item_left_gate").GetString();
|
||||
await Post(url, "ON");
|
||||
}
|
||||
|
||||
private static 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;
|
||||
}
|
||||
|
||||
private static JsonDocument LoadConfig()
|
||||
{
|
||||
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
var config = Path.Combine(assemblyFolder, "zaobot.json");
|
||||
string configStr = File.ReadAllText(config);
|
||||
return JsonDocument.Parse(configStr);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user