feat: added telegram client bot, created DB

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

13
.idea/.idea.JOBot/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/projectSettingsUpdater.xml
/modules.xml
/contentModel.xml
/.idea.JOBot.iml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -10,6 +10,7 @@ public class User
[Key] [Key]
public required long TelegramId { get; set; } public required long TelegramId { get; set; }
[MaxLength(50)]
public string? Username { get; set; } public string? Username { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.Now; public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
} }

View File

@ -0,0 +1,52 @@
// <auto-generated />
using System;
using JOBot.Backend.DAL.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace JOBot.Backend.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20250501164641_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("JOBot.Backend.DAL.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<long>("TelegramId")
.HasColumnType("bigint");
b.Property<string>("Username")
.HasColumnType("text");
b.HasKey("Id");
b.HasAlternateKey("TelegramId");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace JOBot.Backend.Data.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
TelegramId = table.Column<long>(type: "bigint", nullable: false),
Username = table.Column<string>(type: "text", nullable: true),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
table.UniqueConstraint("AK_Users_TelegramId", x => x.TelegramId);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}

View File

@ -0,0 +1,50 @@
// <auto-generated />
using System;
using JOBot.Backend.DAL.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace JOBot.Backend.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20250501165245_UpdateUser")]
partial class UpdateUser
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("JOBot.Backend.DAL.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<long>("TelegramId")
.HasColumnType("bigint");
b.Property<string>("Username")
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasAlternateKey("TelegramId");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,50 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace JOBot.Backend.Data.Migrations
{
/// <inheritdoc />
public partial class UpdateUser : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CreatedAt",
table: "Users");
migrationBuilder.AlterColumn<string>(
name: "Username",
table: "Users",
type: "character varying(50)",
maxLength: 50,
nullable: true,
oldClrType: typeof(string),
oldType: "text",
oldNullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Username",
table: "Users",
type: "text",
nullable: true,
oldClrType: typeof(string),
oldType: "character varying(50)",
oldMaxLength: 50,
oldNullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "CreatedAt",
table: "Users",
type: "timestamp with time zone",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
}
}
}

View File

@ -0,0 +1,53 @@
// <auto-generated />
using System;
using JOBot.Backend.DAL.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace JOBot.Backend.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20250501165633_AddTimeStamp")]
partial class AddTimeStamp
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("JOBot.Backend.DAL.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<long>("TelegramId")
.HasColumnType("bigint");
b.Property<string>("Username")
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasAlternateKey("TelegramId");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace JOBot.Backend.Data.Migrations
{
/// <inheritdoc />
public partial class AddTimeStamp : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "CreatedAt",
table: "Users",
type: "timestamp with time zone",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CreatedAt",
table: "Users");
}
}
}

View File

@ -0,0 +1,50 @@
// <auto-generated />
using System;
using JOBot.Backend.DAL.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace JOBot.Backend.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
partial class AppDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("JOBot.Backend.DAL.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<long>("TelegramId")
.HasColumnType("bigint");
b.Property<string>("Username")
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasAlternateKey("TelegramId");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Server.Kestrel.Core;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration); var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services); startup.ConfigureServices(builder.Services);
var app = builder.Build(); var app = builder.Build();

View File

@ -4,7 +4,7 @@
"http": { "http": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": false,
"applicationUrl": "http://localhost:5253", "applicationUrl": "http://localhost:5253",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
@ -13,7 +13,7 @@
"https": { "https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": false,
"applicationUrl": "https://localhost:7176;http://localhost:5253", "applicationUrl": "https://localhost:7176;http://localhost:5253",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"

View File

@ -12,15 +12,16 @@ public class UserService(AppDbContext dbContext) : User.UserBase
RegisterRequest request, RegisterRequest request,
ServerCallContext context) ServerCallContext context)
{ {
if(!dbContext.Users.Where(x => x.TelegramId == request.UserId) if(!dbContext.Users
.Any()) .Any(x => x.TelegramId == request.UserId))
{ {
dbContext.Users.Add(new Models.User() dbContext.Users.Add(new Models.User
{ {
TelegramId = request.UserId, TelegramId = request.UserId,
Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null
}); });
dbContext.SaveChanges();
return Task.FromResult(new RegisterResponse return Task.FromResult(new RegisterResponse
{ {
Success = true Success = true

View File

@ -1,12 +1,20 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"ConnectionStrings": { "ConnectionStrings": {
"PostgreSQL": "Host=localhost;Port=5432;Database=jobot;Username=postgres;Password=LocalDbPass" "PostgreSQL": "Host=localhost;Port=5432;Database=jobot;Username=postgres;Password=LocalDbPass"
},
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5001",
"Protocols": "Http2"
}
}
} }
} }

View File

@ -1,19 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.30.2" /> <PackageReference Include="Google.Protobuf" Version="3.30.2" />
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" /> <PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
<PackageReference Include="Grpc.Tools" Version="2.71.0"> <PackageReference Include="Grpc.Tools" Version="2.71.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> <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> </Project>

View File

@ -1,2 +1,44 @@
// See https://aka.ms/new-console-template for more information using Grpc.Net.Client;
Console.WriteLine("Hello, World!"); 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 @@
{
}

View File

@ -5,9 +5,11 @@ service User {
rpc Register (RegisterRequest) returns (RegisterResponse); rpc Register (RegisterRequest) returns (RegisterResponse);
} }
import "google/protobuf/wrappers.proto";
message RegisterRequest{ message RegisterRequest{
int64 user_id = 1; int64 user_id = 1;
string username = 2; google.protobuf.StringValue username = 2;
} }
message RegisterResponse{ message RegisterResponse{