Добавьте файлы проекта.

This commit is contained in:
Professional 2025-03-12 17:52:00 +07:00
parent 5b25828c18
commit 33a881539f
4 changed files with 128 additions and 0 deletions

80
Program.cs Normal file
View File

@ -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;
}
}

4
appsettings.json Normal file
View File

@ -0,0 +1,4 @@
{
"BotToken": "7983301191:AAH9qcYmKPP44QvvNpkP71tppv_BAaqZC20",
"Database": "Data Source=bot.db"
}

19
ТГ бот.csproj Normal file
View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>ТГ_бот</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Telegram.Bot" Version="22.4.4" />
</ItemGroup>
</Project>

25
ТГ бот.sln Normal file
View File

@ -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