commit 41269da76c3dba24778080dd5fdf0a21428cb200 Author: Denis Denisov Date: Sat Mar 20 14:04:36 2021 +0300 Initial commit, added bot to control garage gates diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..197ca03 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.vs/ +bin/ +obj/ +ZaoBot.csproj.user +zaobot.json diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..9b1ed09 --- /dev/null +++ b/Program.cs @@ -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); + } + + + } +} diff --git a/Properties/PublishProfiles/FolderProfile.pubxml b/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..83ef5b4 --- /dev/null +++ b/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,16 @@ + + + + + Release + Any CPU + bin\Release\netcoreapp3.1\publish\ + FileSystem + netcoreapp3.1 + linux-x64 + false + False + + \ No newline at end of file diff --git a/Properties/PublishProfiles/FolderProfile.pubxml.user b/Properties/PublishProfiles/FolderProfile.pubxml.user new file mode 100644 index 0000000..312c6e3 --- /dev/null +++ b/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/ZaoBot.csproj b/ZaoBot.csproj new file mode 100644 index 0000000..875d691 --- /dev/null +++ b/ZaoBot.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/ZaoBot.sln b/ZaoBot.sln new file mode 100644 index 0000000..bd9d51c --- /dev/null +++ b/ZaoBot.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30804.86 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZaoBot", "ZaoBot.csproj", "{307FAC2B-FB7D-44C7-A99C-0BDA364852EE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {307FAC2B-FB7D-44C7-A99C-0BDA364852EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {307FAC2B-FB7D-44C7-A99C-0BDA364852EE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {307FAC2B-FB7D-44C7-A99C-0BDA364852EE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {307FAC2B-FB7D-44C7-A99C-0BDA364852EE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FBBB86B4-C052-4EB9-B24C-873E062808AA} + EndGlobalSection +EndGlobal diff --git a/zaobot.service b/zaobot.service new file mode 100644 index 0000000..1ee5c3f --- /dev/null +++ b/zaobot.service @@ -0,0 +1,14 @@ +[Unit] +Description=Zaozerye Home Bot +After=network.target + +[Service] +ExecStart=/usr/bin/dotnet /root/zaobot/ZaoBot.dll +User=root +Group=root +RuntimeDirectory=/root/zaobot +RuntimeDirectoryMode=0755 +ProtectHome=false + +[Install] +WantedBy=multi-user.target