Initial commit

This commit is contained in:
Vladimir Solodovnikov
2024-10-01 01:10:31 +03:00
commit 9b30c25670
13 changed files with 641 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
namespace SVBotApp.Configuration;
public sealed class BotSettings
{
public const string SectionName = "Bot";
public string Secret { get; set; } = null!;
}
+23
View File
@@ -0,0 +1,23 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER app
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["SVBotApp/SVBotApp.csproj", "SVBotApp/"]
RUN dotnet restore "./SVBotApp/SVBotApp.csproj"
COPY . .
WORKDIR "/src/SVBotApp"
RUN dotnet build "./SVBotApp.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./SVBotApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SVBotApp.dll"]
+11
View File
@@ -0,0 +1,11 @@
using SVBotApp;
using SVBotApp.Configuration;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.Configure<BotSettings>(builder.Configuration.GetSection(BotSettings.SectionName));
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();
+15
View File
@@ -0,0 +1,15 @@
{
"profiles": {
"SVBotApp": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
},
"Container (Dockerfile)": {
"commandName": "Docker"
}
},
"$schema": "http://json.schemastore.org/launchsettings.json"
}
+16
View File
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-SVBotApp-dbaa4807-7656-49a4-a49b-f451adab85b8</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Telegram.Bot" Version="21.11.0" />
</ItemGroup>
</Project>
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>SVBotApp</ActiveDebugProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
+76
View File
@@ -0,0 +1,76 @@
using Microsoft.Extensions.Options;
using SVBotApp.Configuration;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
namespace SVBotApp;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly BotSettings _botSettings;
private TelegramBotClient? _botClient = null;
public Worker(ILogger<Worker> logger, IOptions<BotSettings> botSettings)
{
_logger = logger;
_botSettings = botSettings.Value;
}
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
_botClient ??= new TelegramBotClient(_botSettings.Secret, cancellationToken: cancellationToken);
_botClient.OnMessage += OnMessageAsync;
_botClient.OnUpdate += OnUpdateAsync;
_logger.LogInformation("INITIALIZED");
}
private async Task OnUpdateAsync(Update update)
{
_logger.LogInformation("UPDATE_RECEIVED => '{update}'", update.Id);
if (update.Type == UpdateType.CallbackQuery && update.CallbackQuery != null)
{
_logger.LogInformation("PROCESS_CALLBACK_QUERY => {id}:{data}", update.CallbackQuery.Id, update.CallbackQuery.Data);
await _botClient!.EditMessageTextAsync(
update.CallbackQuery.Message!.Chat.Id,
update.CallbackQuery.Message.MessageId,
$"Button pressed: {update.CallbackQuery.Data}",
replyMarkup: new InlineKeyboardMarkup().AddButton("Step2", "step2"));
await _botClient!.AnswerCallbackQueryAsync(update.CallbackQuery.Id, string.Empty);
}
}
private async Task OnMessageAsync(Message message, UpdateType updateType)
{
if (_botClient == null)
{
return;
}
_logger.LogInformation("MESSAGE_RECEIVED => '{message}'", message.Text);
var processingMessage = await _botClient.SendTextMessageAsync(message.Chat.Id, "Processing...", protectContent: true, replyMarkup: new ReplyKeyboardRemove());
await _botClient.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);
await Task.Delay(2000);
var replyMarkup = new ReplyKeyboardMarkup(true)
.AddButton("Btn 01")
.AddButton("Btn 02");
var inlineKeyboard = new InlineKeyboardMarkup()
.AddButton("Some", "some");
_ = await _botClient.SendTextMessageAsync(message.Chat.Id, $"""You've wrote: "{message.Text}".""",
protectContent: true,
replyParameters: message,
replyMarkup: inlineKeyboard);
await _botClient.DeleteMessageAsync(processingMessage.Chat.Id, processingMessage.MessageId);
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Bot": {
"Secret": "6831614155:AAGJWCeDU0Js8Wj9cN4_R4j1MXA8LFUPL2Q"
},
}
+8
View File
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}