Subscribes chat or channel on TextmatchCreated (#2)

Reviewed-on: #2
Co-authored-by: Sergey Nazarov <insight.appdev@gmail.com>
Co-committed-by: Sergey Nazarov <insight.appdev@gmail.com>
This commit is contained in:
Sergey Nazarov 2024-03-22 06:02:05 +00:00 committed by nazarovsa
parent eb27439cce
commit 88981faa31
9 changed files with 57 additions and 12 deletions

View File

@ -5,9 +5,10 @@
<Nullable>enable</Nullable>
<MicrosoftVersion>8.0.0</MicrosoftVersion>
</PropertyGroup>
<ItemGroup Label="Nocr">
<PackageVersion Include="Nocr.TextMatcher.Async.Api.Contracts" Version="0.4.1" />
</ItemGroup>
<ItemGroup Label="Telegram">
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageVersion Include="Nocr.TelegramListener.Async.Api.Contracts" Version="1.0.0" />
<PackageVersion Include="WTelegramClient" Version="3.7.1" />
</ItemGroup>
<ItemGroup Label="Rebus">
@ -23,6 +24,7 @@
</ItemGroup>
<ItemGroup Label="Microsoft">
<PackageVersion Include="Microsoft.Extensions.Options" Version="$(MicrosoftVersion)" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(MicrosoftVersion)" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftVersion)" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(MicrosoftVersion)" />

View File

@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
<PackageReference Include="Nocr.TextMatcher.Async.Api.Contracts" />
<PackageReference Include="WTelegramClient" />
<PackageReference Include="Rebus" />
<PackageReference Include="Rebus.ServiceProvider" />
</ItemGroup>
<ItemGroup>

View File

@ -1,7 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nocr.TelegramListener.AppServices.TextMatches;
using Nocr.TelegramListener.AppServices.UpdateListeners;
using Nocr.TelegramListener.AppServices.UpdateListeners.Handlers;
using Rebus.Config;
namespace Nocr.TelegramListener.AppServices;
@ -16,6 +18,8 @@ public static class ServiceCollectionExtensions
services.Configure<WTelegramClientOptions>(configuration.GetSection(nameof(WTelegramClientOptions)));
services.AddHostedService<UpdateListenerBackgroundService>();
services.AddRebusHandler<TextMatchCreatedHandler>();
services.AddScoped<IUpdateHandler, UpdateHandler>();
services.AddScoped<INewMessageHandler, NewMessageHandler>();
services.AddScoped<IEditMessageHandler, EditMessageHandler>();

View File

@ -0,0 +1,30 @@
using Nocr.TelegramListener.AppServices.UpdateListeners;
using Nocr.TextMatcher.Async.Api.Contracts;
using Rebus.Handlers;
using TL;
namespace Nocr.TelegramListener.AppServices.TextMatches;
public class TextMatchCreatedHandler : IHandleMessages<TextMatchCreated>
{
private readonly TelegramRegistry _telegramRegistry;
private readonly ITelegramClientContainer _clientContainer;
public TextMatchCreatedHandler(TelegramRegistry telegramRegistry,
ITelegramClientContainer clientContainer)
{
_telegramRegistry = telegramRegistry ?? throw new ArgumentNullException(nameof(telegramRegistry));
_clientContainer = clientContainer ?? throw new ArgumentNullException(nameof(clientContainer));
}
public async Task Handle(TextMatchCreated message)
{
var client = _clientContainer.Client;
var resolved = await client.Contacts_ResolveUsername(message.ChatUsername);
if (resolved.Chat is Channel channel && !_telegramRegistry.Chats.ContainsKey(resolved.Chat.ID))
{
await client.Channels_JoinChannel(channel);
await _telegramRegistry.Update(client);
}
}
}

View File

@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using TL;
using WTelegram;
namespace Nocr.TelegramListener.AppServices.UpdateListeners;
@ -11,6 +12,15 @@ public sealed class TelegramRegistry
private readonly object _syncRoot = new();
public async Task Update(Client client)
{
var dialogs = await client.Messages_GetAllDialogs();
lock (_syncRoot)
{
dialogs.CollectUsersChats(Users, Chats);
}
}
public void SetMy(User my)
{
lock (_syncRoot)

View File

@ -16,12 +16,10 @@ public sealed class UpdateListenerBackgroundService : BackgroundService
private readonly IServiceProvider _serviceProvider;
private readonly ITelegramClientContainer _telegramClientContainer;
private readonly TelegramRegistry _telegramRegistry;
private readonly WTelegramClientOptions _wTelegramClientOptions;
public UpdateListenerBackgroundService(
ILogger<UpdateListenerBackgroundService> logger,
IServiceProvider serviceProvider,
IOptions<WTelegramClientOptions> wTelegramClientOptions,
ITelegramClientContainer telegramClientContainer,
TelegramRegistry telegramRegistry)
{
@ -30,8 +28,6 @@ public sealed class UpdateListenerBackgroundService : BackgroundService
_telegramClientContainer =
telegramClientContainer ?? throw new ArgumentNullException(nameof(telegramClientContainer));
_telegramRegistry = telegramRegistry ?? throw new ArgumentNullException(nameof(telegramRegistry));
_wTelegramClientOptions = wTelegramClientOptions.Value ??
throw new ArgumentNullException(nameof(wTelegramClientOptions));
}
@ -57,8 +53,7 @@ public sealed class UpdateListenerBackgroundService : BackgroundService
_telegramRegistry.My.first_name + " " + _telegramRegistry.My.last_name,
_telegramRegistry.My.id);
var dialogs = await client.Messages_GetAllDialogs();
dialogs.CollectUsersChats(_telegramRegistry.Users, _telegramRegistry.Chats);
await _telegramRegistry.Update(client);
}
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<IsPackable>true</IsPackable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />

View File

@ -3,6 +3,7 @@ using Nocr.TelegramListener.AppServices;
using Nocr.TelegramListener.Async.Api.Contracts.Events;
using Nocr.TelegramListener.Core.Dates;
using Nocr.TelegramListener.Core.Options;
using Rebus.Bus;
using Rebus.Config;
using Rebus.Routing.TypeBased;
using Rebus.Serialization.Json;
@ -39,5 +40,7 @@ public class Startup
public void Configure(IApplicationBuilder app)
{
var bus = app.ApplicationServices.GetRequiredService<IBus>();
bus.Advanced.Topics.Subscribe("nocr.text.matcher").GetAwaiter().GetResult();
}
}