Skip to content

Commit fd48525

Browse files
author
Matt Robold
committed
Create a console/background worker host for chat that still uses the tasks api
1 parent a1d18ef commit fd48525

File tree

10 files changed

+379
-3
lines changed

10 files changed

+379
-3
lines changed

.github/workflows/workflow.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v2
18+
19+
- name: Setup .NET
20+
uses: actions/setup-dotnet@v2
21+
with:
22+
dotnet-version: '9.0.x'
23+
24+
- name: Restore dependencies
25+
run: dotnet restore
26+
27+
- name: Build solution
28+
run: dotnet build --no-restore --configuration Release
29+
30+
- name: Run tests
31+
run: dotnet test --no-build --verbosity normal --configuration Release

Moneo.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moneo.Hosts.Chat.Api", "src
4141
EndProject
4242
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moneo.Web.Common", "src\Moneo.Web.Common\Moneo.Web.Common.csproj", "{AC612371-092B-4A01-903A-393F3EC3B12D}"
4343
EndProject
44+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moneo.Hosts.Telegram.Console", "src\Hosts\Moneo.Hosts.Telegram.Console\Moneo.Hosts.Telegram.Console.csproj", "{645D2325-A422-46C2-9863-85965BD20059}"
45+
EndProject
4446
Global
4547
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4648
Debug|Any CPU = Debug|Any CPU
@@ -103,6 +105,10 @@ Global
103105
{AC612371-092B-4A01-903A-393F3EC3B12D}.Debug|Any CPU.Build.0 = Debug|Any CPU
104106
{AC612371-092B-4A01-903A-393F3EC3B12D}.Release|Any CPU.ActiveCfg = Release|Any CPU
105107
{AC612371-092B-4A01-903A-393F3EC3B12D}.Release|Any CPU.Build.0 = Release|Any CPU
108+
{645D2325-A422-46C2-9863-85965BD20059}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
109+
{645D2325-A422-46C2-9863-85965BD20059}.Debug|Any CPU.Build.0 = Debug|Any CPU
110+
{645D2325-A422-46C2-9863-85965BD20059}.Release|Any CPU.ActiveCfg = Release|Any CPU
111+
{645D2325-A422-46C2-9863-85965BD20059}.Release|Any CPU.Build.0 = Release|Any CPU
106112
EndGlobalSection
107113
GlobalSection(SolutionProperties) = preSolution
108114
HideSolutionNode = FALSE
@@ -125,6 +131,7 @@ Global
125131
{2D782615-ECEE-440C-AFB8-515990FCACE2} = {C80150B3-C997-47FF-BE06-ED0709EA6DCB}
126132
{F7DB7921-AD36-4A26-988B-C01318AE8045} = {C80150B3-C997-47FF-BE06-ED0709EA6DCB}
127133
{AC612371-092B-4A01-903A-393F3EC3B12D} = {BAC297B4-CBE2-403F-AD70-110B317BBE28}
134+
{645D2325-A422-46C2-9863-85965BD20059} = {C80150B3-C997-47FF-BE06-ED0709EA6DCB}
128135
EndGlobalSection
129136
GlobalSection(ExtensibilityGlobals) = postSolution
130137
SolutionGuid = {9A69730A-25AE-421F-A76A-39B03C0A0207}

src/Chat/Moneo.Chat.Telegram/ServiceCollectionExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public static IServiceCollection AddTelegramChatAdapter(this IServiceCollection
2929
// https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
3030
services.AddHttpClient("tgclient")
3131
.AddTypedClient<ITelegramBotClient>(httpClient => new TelegramBotClient(options.BotToken, httpClient));
32+
33+
if (options.HostedServiceFlag)
34+
{
35+
services.AddHostedService<TelegramChatBackgroundService>();
36+
}
3237

3338
return services;
3439
}

src/Chat/Moneo.Chat.Telegram/TelegramChatAdapter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ await _botClient.SendMessage(_options.MasterConversationId, apiRequestException.
9696
}
9797
}
9898

99-
public Task StartReceivingAsync(CancellationToken cancellationToken = default)
99+
public async Task StartReceivingAsync(CancellationToken cancellationToken = default)
100100
{
101101
_isUsingWebhook = false;
102102

@@ -107,10 +107,10 @@ public Task StartReceivingAsync(CancellationToken cancellationToken = default)
107107
AllowedUpdates = [UpdateType.Message, UpdateType.CallbackQuery],
108108
DropPendingUpdates = true
109109
};
110+
await _botClient.DeleteWebhook(cancellationToken: cancellationToken);
111+
110112
_botClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, options, cancellationToken);
111113
IsActive = true;
112-
113-
return Task.CompletedTask;
114114
}
115115

116116
public async Task StartReceivingAsync(string callbackUrl, CancellationToken cancellationToken = default)

src/Chat/Moneo.Chat.Telegram/TelegramChatAdapterOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ namespace Moneo.Chat.Telegram;
44

55
public class TelegramChatAdapterOptions : ChatAdapterOptions
66
{
7+
public bool HostedServiceFlag { get; private set; }
78
public string BotToken { get; set; } = string.Empty;
89
public long MasterConversationId { get; set; }
910
public string CallbackToken { get; set; } = string.Empty;
11+
public void RegisterAsHostedService() => HostedServiceFlag = true;
1012
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Moneo.Chat.BotRequests;
3+
using Telegram.Bot.Types;
4+
5+
namespace Moneo.Chat.Telegram;
6+
7+
public class TelegramChatBackgroundService : IHostedService
8+
{
9+
private readonly IChatAdapter _chatAdapter;
10+
11+
public Task StartAsync(CancellationToken cancellationToken) => _chatAdapter.StartReceivingAsync(cancellationToken);
12+
13+
public Task StopAsync(CancellationToken cancellationToken) => _chatAdapter.StopReceivingAsync(cancellationToken);
14+
15+
public TelegramChatBackgroundService(IChatAdapter chatAdapter)
16+
{
17+
_chatAdapter = chatAdapter;
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\Chat\Moneo.Chat.Contracts\Moneo.Chat.Contracts.csproj" />
12+
<ProjectReference Include="..\..\Chat\Moneo.Chat.Telegram\Moneo.Chat.Telegram.csproj" />
13+
<ProjectReference Include="..\..\Chat\Moneo.Chat\Moneo.Chat.csproj" />
14+
<ProjectReference Include="..\..\Moneo.Common\Moneo.Common.csproj" />
15+
<ProjectReference Include="..\..\Moneo.Web.Common\Moneo.Web.Common.csproj" />
16+
<ProjectReference Include="..\..\TaskManagement\Moneo.TaskManagement.Contracts\Moneo.TaskManagement.Contracts.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

0 commit comments

Comments
 (0)