Add telegram sender

This commit is contained in:
Sergey Nazarov 2024-03-23 00:54:59 +04:00
parent 39876ebb54
commit 33074e99a0
11 changed files with 103 additions and 9 deletions

View File

@ -6,6 +6,15 @@
<MicrosoftVersion>8.0.0</MicrosoftVersion>
<InsightTelegramBotVersion>0.16.0</InsightTelegramBotVersion>
</PropertyGroup>
<ItemGroup Label="Nocr">
<PackageVersion Include="Nocr.TextMatcher.Async.Api.Contracts" Version="0.4.7" />
</ItemGroup>
<ItemGroup Label="Rebus">
<PackageVersion Include="Rebus" Version="8.2.2"/>
<PackageVersion Include="Rebus.ServiceProvider" Version="10.1.0"/>
<PackageVersion Include="Rebus.RabbitMq" Version="9.0.1"/>
<PackageVersion Include="Rebus.Serilog" Version="8.0.0"/>
</ItemGroup>
<ItemGroup Label="Insight.TelegramBot">
<PackageVersion Include="Insight.TelegramBot" Version="$(InsightTelegramBotVersion)"/>
<PackageVersion Include="Insight.TelegramBot.Hosting.DependencyInjection" Version="$(InsightTelegramBotVersion)"/>

View File

@ -0,0 +1,26 @@
using Insight.TelegramBot;
using Insight.TelegramBot.Models;
using Nocr.TextMatcher.Async.Api.Contracts;
using Rebus.Handlers;
namespace Nocr.TelegramClient.AppServices.Matches;
public class TextMatchMatchedHandler : IHandleMessages<TextMatchMatched>
{
private readonly IBot _bot;
public TextMatchMatchedHandler(IBot bot)
{
_bot = bot ?? throw new ArgumentNullException(nameof(bot));
}
public async Task Handle(TextMatchMatched message)
{
var textMessage = new TextMessage(58874635)
{
Text = $"[{message.PublishedDateTime:MM.dd.yyyy HH:mm:ss}] Найдено совпадение.\n @{message.ChatUsername} > {message.Text}"
};
await _bot.SendMessageAsync(textMessage);
}
}

View File

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

View File

@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Nocr.TelegramClient.AppServices.Matches;
using Rebus.Config;
namespace Nocr.TelegramClient.AppServices;
@ -10,6 +12,7 @@ public static class ServiceCollectionExtensions
throw new ArgumentNullException(nameof(services));
// Add registrations here
services.AddRebusHandler<TextMatchMatchedHandler>();
return services;
}

View File

@ -0,0 +1,12 @@
namespace Nocr.TelegramClient.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

@ -5,6 +5,11 @@ using Insight.TelegramBot.Hosting.Polling.ExceptionHandlers;
using Microsoft.Extensions.Options;
using Nocr.TelegramClient.AppServices;
using Nocr.TelegramClient.Core.Dates;
using Nocr.TelegramClient.Core.Options;
using Rebus.Bus;
using Rebus.Config;
using Rebus.Routing.TypeBased;
using Rebus.Serialization.Json;
namespace Nocr.TelegramClient.Host.Infrastructure;
@ -30,9 +35,24 @@ public class Startup
.WithPolling(polling => polling.WithExceptionHandler<LoggingPollingExceptionHandler>()));
services.AddTelegramBotHandling(typeof(BotClient).Assembly);
services.AddAppServices();
services.Configure<RebusRabbitMqOptions>(Configuration.GetSection(nameof(RebusRabbitMqOptions)));
services.AddRebus((builder, ctx) =>
builder.Transport(t =>
{
var rebusOptions = ctx.GetRequiredService<IOptions<RebusRabbitMqOptions>>().Value;
t.UseRabbitMq(rebusOptions.ConnectionString, rebusOptions.InputQueueName)
.DefaultQueueOptions(queue => queue.SetDurable(true))
.ExchangeNames(rebusOptions.DirectExchangeName, rebusOptions.TopicsExchangeName);
})
.Serialization(s => s.UseSystemTextJson())
.Logging(l => l.Serilog())
.Routing(r => r.TypeBased()));
}
public void Configure(IApplicationBuilder app)
{
var bus = app.ApplicationServices.GetRequiredService<IBus>();
bus.Advanced.Topics.Subscribe("nocr.text.matcher.matched").GetAwaiter().GetResult();
}
}

View File

@ -9,6 +9,11 @@
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>
<ItemGroup Label="Rebus">
<PackageReference Include="Rebus.RabbitMq" />
<PackageReference Include="Rebus.Serilog" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nocr.TelegramClient.AppServices\Nocr.TelegramClient.AppServices.csproj" />
<ProjectReference Include="..\Nocr.TelegramClient.Core\Nocr.TelegramClient.Core.csproj" />

View File

@ -8,5 +8,8 @@
}
}
]
},
"RebusRabbitMqOptions": {
"ConnectionString": "amqp://admin:admin@localhost:5672/"
}
}

View File

@ -0,0 +1,5 @@
{
"RebusRabbitMqOptions": {
"ConnectionString": "amqp://admin:admin@nocr-rabbitmq:5672/"
}
}

View File

@ -8,5 +8,10 @@
"System.Net.Http.HttpClient": "Warning"
}
}
},
"RebusRabbitMqOptions": {
"InputQueueName": "nocr.telegram.client.queue",
"DirectExchangeName": "nocr.direct",
"TopicsExchangeName": "nocr.topics"
}
}