Skip to content

Commit

Permalink
Merge branch 'master' into LogAndDeploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohirjon-Odilov authored Apr 20, 2024
2 parents 0ec0dff + 6977044 commit 175b042
Show file tree
Hide file tree
Showing 10 changed files with 599 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/BookStore.Application/BookStore.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="MediatR" Version="11.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 13 additions & 2 deletions src/BookStore.Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
using MediatR;
using BookStore.Application.useCases.Extensions.TelegramBot;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using Telegram.Bot;

namespace BookStore.Application
{
public static class DependencyInjection
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddMediatR(Assembly.GetExecutingAssembly());

services.AddSingleton<TelegramBotClient>(provider =>
{
var botToken = $"7040566448:AAGuJLoJsg8xqAAW4yWGPkZDR6PIxBlU0ns";
return new TelegramBotClient(botToken);
});

services.AddSingleton<IWriteToTelegramBotService, WriteToTelegramBotService>();

return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Application.useCases.Extensions.TelegramBot
{
public interface IWriteToTelegramBotService
{
public Task LogError(Exception ex, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.Configuration;
using Telegram.Bot;

namespace BookStore.Application.useCases.Extensions.TelegramBot
{

public class WriteToTelegramBotService : IWriteToTelegramBotService
{
private readonly long _groupId = -1002007183401;
private readonly TelegramBotClient _botClient;

public WriteToTelegramBotService(TelegramBotClient botClient)
{
_botClient = botClient;
}

public async Task LogError(Exception ex, CancellationToken cancellationToken)
{
string errorMessage = $"An error occurred:\n\n{ex.Message}\n\nStack Trace:\n{ex.StackTrace}";
await _botClient.SendTextMessageAsync(chatId: _groupId, text: ex.ToString(), cancellationToken: cancellationToken);
}

}
}
Loading

0 comments on commit 175b042

Please sign in to comment.