Skip to content

Commit

Permalink
Telegram userbot
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Mar 26, 2024
1 parent b2ef3d5 commit b4e726c
Show file tree
Hide file tree
Showing 29 changed files with 839 additions and 182 deletions.
21 changes: 21 additions & 0 deletions src/UserBot/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [

{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "Build UserBot.Cli",
"program": "${workspaceFolder}/UserBot.Cli/bin/Local/net8.0/UserBot.Cli.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole",
"env": {
"api_id": "24851338",
"api_hash": "82a9e257eb717d3a46f8479cdf307434",
"persist_to": "Memory"
}
}
]
}
24 changes: 24 additions & 0 deletions src/UserBot/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/azure-pipelines-task-lib/master/tasks.schema.json",
"version": {
"Major": 2,
"Minor": 0,
"Patch": 0
},
"tasks": [
{
"label": "Build UserBot.Cli",
"command": "dotnet",
"type": "process",
"args": [
"build",
"-c:Local",
"-p:BuildFromSource=false",
"${workspaceFolder}/UserBot.Cli/UserBot.Cli.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
}
]
}
33 changes: 18 additions & 15 deletions src/UserBot/Config/IUSerBotConfig.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Telegram.UserBot.Store.Abstractions;

namespace Telegram.UserBot.Config
{
public partial interface IUserBotConfig
{
IUserBotStore? GetSessionStore();
string? GetConfigVariable(string variable);
string Prompt(string variable);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Telegram.UserBot.Store.Abstractions;

namespace Telegram.UserBot.Config
{
public partial interface IUserBotConfig
{
// GetSessionStore GetSessionStore { get; set; }
// IUserBotStore? GetSessionStore();
string? GetConfigVariable(string variable);
string? Prompt(string variable);
}

public delegate IUserBotStore GetSessionStore(IServiceProvider services);
}
10 changes: 5 additions & 5 deletions src/UserBot/Config/Telegram.UserBot.Config.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Dgmjr.InterfaceGenerator" IncludeAssets="Analyzers;Build" ExcludeAssets="Native;BuildTransitive;BuildMultitargeting;ContentFiles;Compile;Runtime" PrivateAssets="All" />
<PackageReference Include="Dgmjr.Enumerations.CodeGenerator" IncludeAssets="Analyzers;Build" ExcludeAssets="Native;BuildTransitive;BuildMultitargeting;ContentFiles;Compile;Runtime" PrivateAssets="All" />
<PackageReference Include="System.Text.Json.Usings" />
<SourceGeneratorPackageReference Include="Dgmjr.InterfaceGenerator" />
<SourceGeneratorPackageReference Include="Dgmjr.Enumerations.CodeGenerator" />
<UsingsPackageReference Include="System.Text.Json.Usings" />
<PackageReference Include="Dgmjr.Abstractions" />
<PackageReference Include="Dgmjr.Enumerations.Abstractions" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Store.Abstractions/Telegram.UserBot.Store.Abstractions.csproj" />
<ProjectReference Include="../Store.EntityFrameworkCore/Telegram.UserBot.Store.EntityFrameworkCore.csproj" />
<!-- <ProjectReference Include="../Store.EntityFrameworkCore/Telegram.UserBot.Store.EntityFrameworkCore.csproj" />
<ProjectReference Include="../Store.FileSystem/Telegram.UserBot.Store.FileSystem.csproj" />
<ProjectReference Include="../Store.Redis/Telegram.UserBot.Store.Redis.csproj" />
<ProjectReference Include="../Store.Memory/Telegram.UserBot.Store.Memory.csproj" />
<ProjectReference Include="../Store.Memory/Telegram.UserBot.Store.Memory.csproj" /> -->
</ItemGroup>
<ItemGroup>
<Using Include="System.Console" Static="true" />
Expand Down
104 changes: 63 additions & 41 deletions src/UserBot/Config/UserBotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ namespace Telegram.UserBot.Config;

using Telegram.UserBot.Store.Abstractions;
using Telegram.UserBot.Store;
using Telegram.UserBot.Store.EntityFrameworkCore;
using Telegram.UserBot.Store.Redis;
using static Environment;
using Microsoft.EntityFrameworkCore;

public class UserBotConfig
public class UserBotConfig : IUserBotConfig
{
#pragma warning disable IDE1006
public const string api_hash = nameof(api_hash);
public const string api_id = nameof(api_id);
public const string verification_code = nameof(verification_code);
Expand All @@ -23,18 +22,25 @@ public class UserBotConfig
public const string session_pathname = nameof(session_pathname);
public const string session_key = nameof(session_key);
public const string server_address = nameof(server_address);

public virtual string Prompt(string variable)
public const string system_lang_code = nameof(system_lang_code);
public const string lang_code = nameof(lang_code);
public const string app_version = nameof(app_version);
public const string system_version = nameof(system_version);
public const string device_model = nameof(device_model);
#pragma warning restore IDE1006

public virtual string? Prompt(string variable)
{
Console.Write($"Enter {variable}: ");
return Console.ReadLine();
var value = Console.ReadLine();
return value.IsPresent() ? value : null;
}

public string? GetConfigVariable(string variable) =>
variable switch
{
#pragma warning disable S1121
api_id => (ApiId = long.Parse(ApiId.ToString() ?? Prompt(api_id))).ToString(),
api_id => ApiId ??= Prompt(api_id),
api_hash => ApiHash ??= Prompt(api_hash),
verification_code => VerificationCode ??= Prompt(verification_code),
first_name => FirstName ??= Prompt(first_name),
Expand All @@ -47,46 +53,47 @@ public virtual string Prompt(string variable)
_ => Prompt(variable)
};

public IUserBotStore? GetSessionStore()
{
return PersistTo switch
{
PersistTo.Memory => new MemoryUserBotStore(),
PersistTo.File => new FileUserBotStore(SessionPathname),
PersistTo.Database
=> new DbUserBotStore(
new UserBotDbContext(
new DbContextOptionsBuilder<UserBotDbContext>()
.UseSqlServer(DbConnectionString)
.Options
),
SessionPathname
),
PersistTo.Redis => new RedisUserBotStore(
new RedisUserBotOptions { Key = SessionKey, ConnectionString = RedisConnectionString }
),
_ => null,
};
}

[JProp((persist_to))]
public PersistTo PersistTo { get; set; } =
// public IUserBotStore? GetSessionStore()
// {
// return PersistTo switch
// {
// PersistTo.Memory => new MemoryUserBotStore(),
// PersistTo.File => new FileUserBotStore(SessionPathname),
// PersistTo.Database
// => new DbUserBotStore(
// new UserBotDbContext(
// new DbContextOptionsBuilder<UserBotDbContext>()
// .UseSqlServer(DbConnectionString)
// .Options
// ),
// SessionPathname
// ),
// PersistTo.Redis => new RedisUserBotStore(
// new RedisUserBotOptions { Key = SessionKey, ConnectionString = RedisConnectionString }
// ),
// _ => null,
// };
// }

// public GetSessionStore GetSessionStore { get; set; }

[JProp(persist_to)]
public virtual PersistTo PersistTo { get; set; } =
Enum.TryParse<PersistTo>(GetEnvironmentVariable(persist_to), out var @enum)
? @enum
: default;

[JProp(db_connection_string)]
public string DbConnectionString { get; set; } = GetEnvironmentVariable(db_connection_string);
public virtual string DbConnectionString { get; set; } = GetEnvironmentVariable(db_connection_string);

[JProp(api_id)]
public long ApiId { get; set; } =
long.TryParse(GetEnvironmentVariable(api_id), out var @long) ? @long : default;
public virtual string ApiId { get; set; } = GetEnvironmentVariable(api_id);

[JProp(api_hash)]
public string ApiHash { get; set; } = GetEnvironmentVariable(api_hash);
public virtual string ApiHash { get; set; } = GetEnvironmentVariable(api_hash);

[JProp(verification_code)]
public string VerificationCode { get; set; } = GetEnvironmentVariable(verification_code);
public virtual string VerificationCode { get; set; } = GetEnvironmentVariable(verification_code);

[JProp(first_name)]
public string FirstName { get; set; } = GetEnvironmentVariable(first_name);
Expand All @@ -95,17 +102,32 @@ public virtual string Prompt(string variable)
public string LastName { get; set; } = GetEnvironmentVariable(last_name);

[JProp(password)]
public string Password { get; set; } = GetEnvironmentVariable(password);
public virtual string Password { get; set; } = GetEnvironmentVariable(password);

[JProp(session_pathname)]
public string SessionPathname { get; set; } = GetEnvironmentVariable(session_pathname);
public virtual string SessionPathname { get; set; } = GetEnvironmentVariable(session_pathname);

[JProp(redis_connection_string)]
public string RedisConnectionString { get; set; } = GetEnvironmentVariable(redis_connection_string);
public virtual string RedisConnectionString { get; set; } = GetEnvironmentVariable(redis_connection_string);

[JProp(session_key)]
public string SessionKey { get; set; } = GetEnvironmentVariable(session_key);
public virtual string SessionKey { get; set; } = GetEnvironmentVariable(session_key);

[JProp(server_address)]
public string ServerAddress { get; set; } = GetEnvironmentVariable(server_address);
public virtual string ServerAddress { get; set; } = GetEnvironmentVariable(server_address);

[JProp(system_lang_code)]
public virtual string SystemLanguageCode { get; set; } = GetEnvironmentVariable(system_lang_code);

[JProp(lang_code)]
public virtual string LanguageCode { get; set; } = GetEnvironmentVariable(lang_code);

[JProp(app_version)]
public virtual string AppVersion { get; set; } = GetEnvironmentVariable(app_version);

[JProp(system_version)]
public virtual string SystemVersion { get; set; } = GetEnvironmentVariable(system_version);

[JProp(device_model)]
public virtual string DeviceModel { get; set; } = GetEnvironmentVariable(device_model);
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="../Models/Telegram.UserBot.Models.csproj" />
<ProjectReference Include="../Store.Abstractions/Telegram.UserBot.Store.Abstractions.csproj" />
<ProjectReference Include="../Config/Telegram.UserBot.Config.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Telegram.UserBot.Config, PublicKey=00240000048000009400000006020000002400005253413100040000010001000d802c8a5e6d2d2a7487bf92dd89a0689f004d27d8eaef54cc62aed0afd643dc33222f020003d3fc79c76c0a791eb7ae56d0d17bac639122f17a8f7f246ae6481045146327ebc0eab8878dbdc79aa6ad1bc26b56bbe0ce5eb443c7450a3496d8bc181135c01b98e974ef825ea9b247bd4bc95023b9394e7073e278cde1ca0c94" />
Expand Down
24 changes: 19 additions & 5 deletions src/UserBot/Store.FileSystem/FileUserBotStore.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
namespace Telegram.UserBot.Store.FileSystem;

using Telegram.UserBot.Store.Abstractions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;

internal class FileUserBotStore : IUserBotStore
public class FileUserBotStore : IUserBotStore, ILog
{
private readonly string _filePath;
public ILogger? Logger { get; }
private readonly FilePersistedUserBotConfig _options;

public FileUserBotStore(string? filePath = null)
public FileUserBotStore(IOptions<FilePersistedUserBotConfig> options, ILogger<FileUserBotStore>? logger = null)
{
_options = options.Value;

_filePath =
filePath
?? Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "session.dat");
_options.FilePath
?? Combine(GetDirectoryName(GetType().Assembly.Location), "session.dat");

if (!File.Exists(_filePath))
{
File.Create(_filePath).Close();
Create(_filePath).Close();
}

Logger = logger;
}

public Stream GetStream()
{
return new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
}
}

public class FilePersistedUserBotConfig : Telegram.UserBot.Config.UserBotConfig
{
public string? FilePath { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
<ItemGroup>
<ProjectReference Include="../Models/Telegram.UserBot.Models.csproj" />
<ProjectReference Include="../Store.Abstractions/Telegram.UserBot.Store.Abstractions.csproj" />
<ProjectReference Include="../Config/Telegram.UserBot.Config.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Telegram.UserBot.Config, PublicKey=00240000048000009400000006020000002400005253413100040000010001000d802c8a5e6d2d2a7487bf92dd89a0689f004d27d8eaef54cc62aed0afd643dc33222f020003d3fc79c76c0a791eb7ae56d0d17bac639122f17a8f7f246ae6481045146327ebc0eab8878dbdc79aa6ad1bc26b56bbe0ce5eb443c7450a3496d8bc181135c01b98e974ef825ea9b247bd4bc95023b9394e7073e278cde1ca0c94" />
</ItemGroup>
<ItemGroup>
<Using Include="System.IO.File" Static="true" />
<Using Include="System.IO.Path" Static="true" />
</ItemGroup>
</Project>
39 changes: 21 additions & 18 deletions src/UserBot/Store.Memory/MemoryUserBotStore.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
namespace Telegram.UserBot.Store;

using Telegram.UserBot.Store.Abstractions;

internal class MemoryUserBotStore : IUserBotStore
{
private readonly MemoryStream _stream;

public MemoryUserBotStore()
{
_stream = new MemoryStream();
}

public Stream GetStream()
{
return _stream;
}
}
namespace Telegram.UserBot.Store;
using Microsoft.Extensions.Logging;
using Telegram.UserBot.Store.Abstractions;

internal class MemoryUserBotStore : IUserBotStore, ILog
{
private readonly MemoryStream _stream;
public ILogger? Logger { get; }

public MemoryUserBotStore(ILogger<MemoryUserBotStore>? logger = null)
{
_stream = new MemoryStream();
Logger = logger;
}

public Stream GetStream()
{
Logger?.LogInformation("Getting memory stream for userbot session storage.");
return _stream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="../Models/Telegram.UserBot.Models.csproj" />
<ProjectReference Include="../Store.Abstractions/Telegram.UserBot.Store.Abstractions.csproj" />
<ProjectReference Include="../Config/Telegram.UserBot.Config.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Telegram.UserBot.Config, PublicKey=00240000048000009400000006020000002400005253413100040000010001000d802c8a5e6d2d2a7487bf92dd89a0689f004d27d8eaef54cc62aed0afd643dc33222f020003d3fc79c76c0a791eb7ae56d0d17bac639122f17a8f7f246ae6481045146327ebc0eab8878dbdc79aa6ad1bc26b56bbe0ce5eb443c7450a3496d8bc181135c01b98e974ef825ea9b247bd4bc95023b9394e7073e278cde1ca0c94" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ItemGroup>
<ProjectReference Include="../Models/Telegram.UserBot.Models.csproj" />
<ProjectReference Include="../Store.Abstractions/Telegram.UserBot.Store.Abstractions.csproj" />
<ProjectReference Include="../Config/Telegram.UserBot.Config.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Telegram.UserBot.Config, PublicKey=00240000048000009400000006020000002400005253413100040000010001000d802c8a5e6d2d2a7487bf92dd89a0689f004d27d8eaef54cc62aed0afd643dc33222f020003d3fc79c76c0a791eb7ae56d0d17bac639122f17a8f7f246ae6481045146327ebc0eab8878dbdc79aa6ad1bc26b56bbe0ce5eb443c7450a3496d8bc181135c01b98e974ef825ea9b247bd4bc95023b9394e7073e278cde1ca0c94" />
Expand Down
Loading

0 comments on commit b4e726c

Please sign in to comment.