Add routing

This commit is contained in:
Sergey Nazarov 2024-03-21 00:48:33 +04:00
parent f8024b7205
commit aef4be1ef9
8 changed files with 29 additions and 18 deletions

View File

@ -7,6 +7,7 @@
</PropertyGroup>
<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">

View File

@ -28,9 +28,9 @@ public sealed class NewMessageHandler : INewMessageHandler
switch (messageBase)
{
case Message m:
if (!string.IsNullOrWhiteSpace(m.message))
if (string.IsNullOrWhiteSpace(m.message))
break;
_logger.LogInformation("{From} in {Chat} > {MessageText}",
m.from_id.Peer(_telegramRegistry.Users, _telegramRegistry.Chats) ?? m.post_author,
m.peer_id.Peer(_telegramRegistry.Users, _telegramRegistry.Chats),
@ -42,7 +42,7 @@ public sealed class NewMessageHandler : INewMessageHandler
Text = m.message,
OccuredDateTime = _dateProvider.UtcNow
};
await _bus.Publish(@event);
await _bus.Advanced.Topics.Publish("nocr.telegram.listener", @event);
break;
case MessageService ms:
_logger.LogInformation("{From} in {Chat} > [{Action}]",

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Nocr.TelegramListener.Core\Nocr.TelegramListener.Core.csproj" />
<ProjectReference Include="..\Nocr.TelegramListener.Core\Nocr.TelegramListener.Core.csproj" PrivateAssets="All"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,12 @@
namespace Nocr.TelegramListener.Core.Options;
public sealed class RebusRabbitMqOptions
{
public string ConnectionString { get; set; }
public string InputQueueName { get; set; }
public string DirectExchangeName { get; set; }
public string TopicsExchangeName { get; set; }
}

View File

@ -12,7 +12,7 @@ public class HostBuilderFactory<TStartup> where TStartup : class
if (!string.IsNullOrWhiteSpace(baseDirectory))
configurationBuilder.SetBasePath(baseDirectory);
configurationBuilder.AddJsonFile("appsettings.protected.json", false);
configurationBuilder.AddJsonFile("appsettings.protected.json", true);
})
.ConfigureWebHostDefaults(host => { host.UseStartup<TStartup>(); })
.UseSerilog((ctx, logBuilder) =>

View File

@ -2,6 +2,7 @@ using Microsoft.Extensions.Options;
using Nocr.TelegramListener.AppServices;
using Nocr.TelegramListener.Async.Api.Contracts.Events;
using Nocr.TelegramListener.Core.Dates;
using Nocr.TelegramListener.Core.Options;
using Rebus.Config;
using Rebus.Routing.TypeBased;
using Rebus.Serialization.Json;
@ -25,22 +26,18 @@ public class Startup
services.Configure<RebusRabbitMqOptions>(Configuration.GetSection(nameof(RebusRabbitMqOptions)));
services.AddRebus((builder, ctx) =>
builder.Transport(t =>
t.UseRabbitMq(ctx.GetRequiredService<IOptions<RebusRabbitMqOptions>>().Value.ConnectionString,
ctx.GetRequiredService<IOptions<RebusRabbitMqOptions>>().Value.InputQueueName)
{
var rebusOptions = ctx.GetRequiredService<IOptions<RebusRabbitMqOptions>>().Value;
t.UseRabbitMq(rebusOptions.ConnectionString, rebusOptions.InputQueueName)
.DefaultQueueOptions(queue => queue.SetDurable(true))
.ExchangeNames("nocr.direct", "nocr.topics"))
.ExchangeNames(rebusOptions.DirectExchangeName, rebusOptions.TopicsExchangeName);
})
.Serialization(s => s.UseSystemTextJson())
.Logging(l => l.Serilog()));
.Logging(l => l.Serilog())
.Routing(r => r.TypeBased()));
}
public void Configure(IApplicationBuilder app)
{
}
public sealed class RebusRabbitMqOptions
{
public string ConnectionString { get; set; }
public string InputQueueName { get; set; }
}
}

View File

@ -9,7 +9,6 @@
</ItemGroup>
<ItemGroup Label="Rebus">
<PackageReference Include="Rebus" />
<PackageReference Include="Rebus.ServiceProvider" />
<PackageReference Include="Rebus.RabbitMq" />
<PackageReference Include="Rebus.Serilog" />

View File

@ -10,6 +10,8 @@
}
},
"RebusRabbitMqOptions": {
"InputQueueName": "nocr.telegram.listener.queue"
"InputQueueName": "nocr.telegram.listener.queue",
"DirectExchangeName": "nocr.direct",
"TopicsExchangeName": "nocr.topics"
}
}