From 33a881539f7bc0632182974e1afec62b95c99c8e Mon Sep 17 00:00:00 2001 From: Professional Date: Wed, 12 Mar 2025 17:52:00 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D1=8C=D1=82?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program.cs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ appsettings.json | 4 +++ ТГ бот.csproj | 19 ++++++++++++ ТГ бот.sln | 25 +++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 Program.cs create mode 100644 appsettings.json create mode 100644 ТГ бот.csproj create mode 100644 ТГ бот.sln diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..1f5abe9 --- /dev/null +++ b/Program.cs @@ -0,0 +1,80 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Data.Sqlite; +using Serilog; +using Telegram.Bot; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; + +class Program +{ + private static string _botToken = string.Empty; + private static TelegramBotClient _botClient = null!; + + static async Task Main() + { + // Загружаем конфигурацию из appsettings.json + var config = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .Build(); + + _botToken = config["BotToken"] ?? throw new Exception("BotToken не найден в конфигурации!"); + + // Настраиваем логирование + Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day) + .CreateLogger(); + + Log.Information("Запуск Telegram-бота..."); + + _botClient = new TelegramBotClient(_botToken); + var me = await _botClient.GetMe(); + Log.Information($"Бот {me.FirstName} запущен!"); + + _botClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync); + + Console.ReadLine(); + } + + private static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) + { + if (update.Type != UpdateType.Message || update.Message?.Text == null) + return; + + var message = update.Message; + Log.Information($"Сообщение от {message.Chat.Id}: {message.Text}"); + + if (message.Text == "/start") + { + await botClient.SendMessage( + chatId: message.Chat.Id, + text: "Привет! Я бот для сбора заявок на ремонт оборудования. Отправь /report для подачи заявки." + ); + } + else if (message.Text == "/report") + { + await botClient.SendMessage( + chatId: message.Chat.Id, + text: "Пожалуйста, отправьте описание проблемы." + ); + } + else + { + await botClient.SendMessage( + chatId: message.Chat.Id, + text: "Неизвестная команда. Используйте /start." + ); + } + } + + private static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) + { + Log.Error($"Ошибка: {exception.Message}"); + return Task.CompletedTask; + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..3fb831e --- /dev/null +++ b/appsettings.json @@ -0,0 +1,4 @@ +{ + "BotToken": "7983301191:AAH9qcYmKPP44QvvNpkP71tppv_BAaqZC20", + "Database": "Data Source=bot.db" +} diff --git a/ТГ бот.csproj b/ТГ бот.csproj new file mode 100644 index 0000000..861a92f --- /dev/null +++ b/ТГ бот.csproj @@ -0,0 +1,19 @@ + + + + Exe + net8.0 + ТГ_бот + enable + enable + + + + + + + + + + + diff --git a/ТГ бот.sln b/ТГ бот.sln new file mode 100644 index 0000000..b8792d9 --- /dev/null +++ b/ТГ бот.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35828.75 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ТГ бот", "ТГ бот.csproj", "{0253E30B-1FF3-4BAA-A520-2F7D3823DE21}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0253E30B-1FF3-4BAA-A520-2F7D3823DE21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0253E30B-1FF3-4BAA-A520-2F7D3823DE21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0253E30B-1FF3-4BAA-A520-2F7D3823DE21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0253E30B-1FF3-4BAA-A520-2F7D3823DE21}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AAF97491-E46B-4B4D-B728-30E243F12F63} + EndGlobalSection +EndGlobal