Initial commit, added bot to control garage gates

This commit is contained in:
ftc
2021-03-20 14:04:36 +03:00
commit 41269da76c
7 changed files with 205 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
.vs/
bin/
obj/
ZaoBot.csproj.user
zaobot.json
+127
View File
@@ -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);
}
}
}
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
</PropertyGroup>
</Project>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
+12
View File
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="15.7.1" />
</ItemGroup>
</Project>
+25
View File
@@ -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
+14
View File
@@ -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