Skip to content

Commit bb53f3d

Browse files
committed
broadcasting:
- Received messages marking API
1 parent d01743b commit bb53f3d

File tree

19 files changed

+570
-35
lines changed

19 files changed

+570
-35
lines changed

Botticelli.Framework.Telegram/Builders/TelegramBotBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public TelegramBotBuilder<TBot> AddClient(TelegramClientDecoratorBuilder builder
8686
Services!.AddHttpClient<GetBroadCastMessagesService<TelegramBot>>()
8787
.AddServerCertificates(BotSettings);
8888
Services!.AddHostedService<GetBroadCastMessagesService<IBot<TelegramBot>>>();
89-
89+
Services!.AddHostedService<MarkAsReceivedService<IBot<TelegramBot>>>();
90+
9091
Services!.AddHostedService<TelegramBotHostedService>();
9192
var botId = BotDataUtils.GetBotId();
9293

Botticelli.Framework/Services/BotActualizationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual Task StopAsync(CancellationToken cancellationToken)
5656
/// <param name="funcName">Response</param>
5757
/// <param name="cancellationToken">Cancellation token</param>
5858
/// <returns></returns>
59-
protected virtual async Task<TResp?> InnerSend<TReq, TResp>(TReq request,
59+
protected virtual async Task<TResp?> InnerSendPost<TReq, TResp>(TReq request,
6060
string funcName,
6161
CancellationToken cancellationToken)
6262
{

Botticelli.Framework/Services/BotStatusService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private void GetRequiredStatus(CancellationToken cancellationToken)
5656
private Task<GetRequiredStatusFromServerResponse?> Process(GetRequiredStatusFromServerRequest request,
5757
CancellationToken cancellationToken)
5858
{
59-
var task = InnerSend<GetRequiredStatusFromServerRequest, GetRequiredStatusFromServerResponse>(request,
59+
var task = InnerSendPost<GetRequiredStatusFromServerRequest, GetRequiredStatusFromServerResponse>(request,
6060
"/bot/client/GetRequiredBotStatus",
6161
cancellationToken);
6262

Botticelli.Framework/Services/GetBroadCastMessagesService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class GetBroadCastMessagesService<TBot>(
2626
bot,
2727
logger)
2828
{
29-
private IBot _bot = bot;
29+
private readonly IBot _bot = bot;
3030

3131
protected override async Task InnerProcess(GetBroadCastMessagesResponse response, CancellationToken ct)
3232
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Botticelli.Framework.Options;
2+
using Botticelli.Interfaces;
3+
using Botticelli.Shared.API.Client.Requests;
4+
using Botticelli.Shared.API.Client.Responses;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Botticelli.Framework.Services;
8+
9+
/// <summary>
10+
/// Marks a broadcast message as received
11+
/// </summary>
12+
/// <param name="httpClientFactory"></param>
13+
/// <param name="serverSettings"></param>
14+
/// <param name="bot"></param>
15+
/// <param name="logger"></param>
16+
/// <typeparam name="TBot"></typeparam>
17+
public class MarkAsReceivedService<TBot>(
18+
IHttpClientFactory httpClientFactory,
19+
ServerSettings serverSettings,
20+
IBot bot,
21+
ILogger<BotActualizationService> logger)
22+
: PollActualizationService<MarkAsReceivedRequest, MarksAsReceivedResponse>(httpClientFactory,
23+
"broadcast",
24+
serverSettings,
25+
bot,
26+
logger)
27+
{
28+
private readonly IBot _bot = bot;
29+
30+
protected override async Task InnerProcess(MarksAsReceivedResponse response, CancellationToken ct)
31+
{
32+
}
33+
}

Botticelli.Framework/Services/PollActualizationService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override Task StartAsync(CancellationToken cancellationToken)
3737
/// <param name="funcName">Method on a server</param>
3838
/// <param name="cancellationToken">Cancellation token</param>
3939
/// <returns></returns>
40-
protected override async Task<TResp?> InnerSend<TReq, TResp>(TReq request,
40+
protected override async Task<TResp?> InnerSendPost<TReq, TResp>(TReq request,
4141
string funcName,
4242
CancellationToken cancellationToken) where TResp : default
4343
{
@@ -93,7 +93,7 @@ private void ProcessRequest(CancellationToken cancellationToken)
9393

9494
private async Task<TResponse> Process(TRequest request, CancellationToken ct)
9595
{
96-
var response = await InnerSend<TRequest, TResponse>(request,
96+
var response = await InnerSendPost<TRequest, TResponse>(request,
9797
$"/bot/client/{subPath}",
9898
ct);
9999

Botticelli.Server.Back/Controllers/AdminController.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using Botticelli.Server.Back.Services;
1+
using System.Collections;
2+
using Botticelli.Server.Back.Services;
3+
using Botticelli.Server.Back.Services.Broadcasting;
24
using Botticelli.Server.Data.Entities.Bot;
5+
using Botticelli.Server.Data.Entities.Bot.Broadcasting;
36
using Botticelli.Shared.API.Admin.Responses;
47
using Botticelli.Shared.API.Client.Requests;
58
using Botticelli.Shared.API.Client.Responses;
@@ -17,16 +20,19 @@ namespace Botticelli.Server.Back.Controllers;
1720
public class AdminController
1821
{
1922
private readonly IBotManagementService _botManagementService;
23+
private readonly IBroadcastService _broadcastService;
2024
private readonly IBotStatusDataService _botStatusDataService;
2125
private readonly ILogger<AdminController> _logger;
2226

2327
public AdminController(IBotManagementService botManagementService,
2428
IBotStatusDataService botStatusDataService,
25-
ILogger<AdminController> logger)
29+
ILogger<AdminController> logger,
30+
IBroadcastService broadcastService)
2631
{
2732
_botManagementService = botManagementService;
2833
_botStatusDataService = botStatusDataService;
2934
_logger = logger;
35+
_broadcastService = broadcastService;
3036
}
3137

3238
[HttpPost("[action]")]
@@ -64,6 +70,23 @@ public async Task<UpdateBotResponse> UpdateBot([FromBody] UpdateBotRequest reque
6470
};
6571
}
6672

73+
/// <summary>
74+
/// Sends a broadcast message
75+
/// </summary>
76+
/// <param name="botId"></param>
77+
/// <param name="message"></param>
78+
/// <returns></returns>
79+
[HttpGet("[action]")]
80+
public async Task SendBroadcast([FromQuery] string botId, [FromQuery] string message)
81+
{
82+
await _broadcastService.BroadcastMessage(new Broadcast
83+
{
84+
Id = Guid.NewGuid().ToString(),
85+
BotId = botId,
86+
Body = message
87+
});
88+
}
89+
6790
[HttpGet("[action]")]
6891
public async Task<ICollection<BotInfo>> GetBots()
6992
=> _botStatusDataService.GetBots();
@@ -76,7 +99,6 @@ public async Task ActivateBot([FromQuery] string botId)
7699
public async Task DeactivateBot([FromQuery] string botId)
77100
=> await _botManagementService.SetRequiredBotStatus(botId, BotStatus.Locked);
78101

79-
80102
[HttpGet("[action]")]
81103
public async Task RemoveBot([FromQuery] string botId)
82104
=> await _botManagementService.RemoveBot(botId);

Botticelli.Server.Back/Controllers/BotController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public async Task<GetBroadCastMessagesResponse> Broadcast([FromBody] GetBroadCas
139139
}
140140
}
141141

142-
143142
/// <summary>
144143
/// Gets broadcast messages received notifications
145144
/// </summary>
@@ -156,7 +155,7 @@ public async Task<BroadCastMessagesReceivedResponse> BroadcastReceived(
156155
request.BotId?.NotNullOrEmpty();
157156

158157
foreach (var messageId in request.MessageIds)
159-
await broadcastService.DeleteReceived(request.BotId!, messageId);
158+
await broadcastService.MarkReceived(request.BotId!, messageId);
160159

161160
return new BroadCastMessagesReceivedResponse
162161
{

Botticelli.Server.Back/Services/BotManagementService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Botticelli.Server.Data;
22
using Botticelli.Server.Data.Entities.Bot;
3+
using Botticelli.Server.Data.Entities.Bot.Broadcasting;
34
using Botticelli.Shared.API.Admin.Responses;
45
using Botticelli.Shared.Constants;
56

Botticelli.Server.Back/Services/Broadcasting/BroadcastService.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,38 @@ public async Task BroadcastMessage(Broadcast message)
1818
public async Task<IEnumerable<Broadcast>> GetMessages(string botId)
1919
=> await context.BroadcastMessages.Where(m => m.BotId.Equals(botId)).ToArrayAsync();
2020

21-
public async Task DeleteReceived(string botId, string messageId)
21+
public async Task MarkReceived(string botId, string messageId)
2222
{
23-
context.BroadcastMessages.RemoveRange(
24-
context.BroadcastMessages.Where(bm => bm.BotId == botId && bm.Id == messageId));
23+
var messages = context.BroadcastMessages.Where(bm => bm.BotId == botId && bm.Id == messageId)
24+
.ToList();
25+
26+
foreach (var message in messages)
27+
{
28+
message.Received = true;
29+
}
30+
31+
context.UpdateRange(messages);
32+
33+
await context.SaveChangesAsync();
34+
}
35+
36+
37+
public async Task<List<Broadcast>> GetBroadcasts(string botId)
38+
{
39+
var broadcasts = context.BroadcastMessages.Where(x => x.BotId == botId && !x.Sent && !x.Received).ToList();
40+
41+
return broadcasts;
42+
}
43+
44+
public async Task MarkAsReceived(string messageId)
45+
{
46+
var broadcast = context.BroadcastMessages.FirstOrDefault(x => x.Id == messageId);
47+
if (broadcast == null) return;
48+
49+
broadcast.Received = true;
50+
51+
context.Update(broadcast);
52+
2553
await context.SaveChangesAsync();
2654
}
2755
}

Botticelli.Server.Back/Services/Broadcasting/IBroadcastService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ public interface IBroadcastService
2828
/// <param name="botId"></param>
2929
/// <param name="messageId"></param>
3030
/// <returns></returns>
31-
public Task DeleteReceived(string botId, string messageId);
31+
public Task MarkReceived(string botId, string messageId);
32+
33+
public Task<List<Broadcast>> GetBroadcasts(string botId);
34+
public Task MarkAsReceived(string messageId);
3235
}

Botticelli.Server.Back/Services/IBotManagementService.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
using Botticelli.Shared.API.Admin.Responses;
1+
using Botticelli.Server.Data.Entities.Bot.Broadcasting;
2+
using Botticelli.Shared.API.Admin.Responses;
23
using Botticelli.Shared.Constants;
34

45
namespace Botticelli.Server.Back.Services;
56

67
public interface IBotManagementService
78
{
89
Task<bool> RegisterBot(string botId,
9-
string botKey,
10-
string botName,
11-
BotType botType,
12-
Dictionary<string, string> additionalParams = null);
10+
string botKey,
11+
string botName,
12+
BotType botType,
13+
Dictionary<string, string> additionalParams = null);
1314

1415
Task<bool> UpdateBot(string botId,
15-
string botKey,
16-
string botName,
17-
Dictionary<string, string> additionalParams = null);
16+
string botKey,
17+
string botName,
18+
Dictionary<string, string> additionalParams = null);
19+
1820

1921
Task SetRequiredBotStatus(string botId, BotStatus status);
2022
Task SetKeepAlive(string botId);

Botticelli.Server.Back/database.db

4 KB
Binary file not shown.

Botticelli.Server.Data.Entities/Bot/Broadcasting/Broadcast.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public class Broadcast
1313
public DateTime Timestamp { get; set; }
1414

1515
public BroadcastAttachment[]? Attachments { get; set; }
16-
// public bool ReceivedSuccessfully { get; set; } = false;
16+
public bool Sent { get; set; } = false;
17+
public bool Received { get; set; } = false;
1718
}

0 commit comments

Comments
 (0)