feat: added telegram client bot, created DB

This commit is contained in:
Lisoveliy
2025-05-01 19:58:33 +03:00
parent f8f5acbd0e
commit 74994140c0
18 changed files with 445 additions and 30 deletions

View File

@@ -1,19 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.30.2" />
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
<PackageReference Include="Grpc.Tools" Version="2.71.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.30.2" />
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
<PackageReference Include="Grpc.Tools" Version="2.71.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.4" />
<PackageReference Include="Telegram.Bot" Version="22.5.1" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="..\Proto\*" GrpcServices="Client"></Protobuf>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -1,2 +1,44 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using Grpc.Net.Client;
using Proto = JOBot.Proto;
using Telegram.Bot;
using Telegram.Bot.Types;
using Microsoft.Extensions.Configuration;
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.Development.json", true)
.Build();
var botClient = new TelegramBotClient(config.GetValue<string>("TelegramToken")
?? throw new MissingFieldException("TelegramToken is not set"));
botClient.StartReceiving(HandleUpdate, HandleError);
Console.WriteLine("Бот запущен. Нажмите Enter для остановки.");
Console.ReadLine();
async Task HandleUpdate(ITelegramBotClient bot, Update update, CancellationToken ct)
{
using var channel = GrpcChannel.ForAddress(config.GetValue<string>("BackendHost")
?? throw new MissingFieldException("Host is not defined"));
var userClient = new Proto.User.UserClient(channel);
if (update.Message?.Text == "/start")
{
var reply = await userClient.RegisterAsync(new Proto.RegisterRequest
{
UserId = update.Message.Chat.Id,
Username = update.Message.Chat.Username ?? ""
});
await bot.SendMessage(
chatId: update.Message.Chat.Id,
text: reply.Success ? "✅ Вы зарегистрированы!" : "❌ Ошибка",
cancellationToken: ct);
}
}
Task HandleError(ITelegramBotClient bot, Exception ex, CancellationToken ct)
{
Console.WriteLine($"Ошибка: {ex.Message}");
return Task.CompletedTask;
}

View File

@@ -0,0 +1,4 @@
{
"TelegramToken": "7372524570:AAGLW5dXhW5Jd78i9Zguyga-5gfeSF9KU4I",
"BackendHost": "http://localhost:5001"
}

View File

@@ -0,0 +1,3 @@
{
}