refactor uncompleted works exists in Bot Service ...
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using xCommons.Providers;
|
||||
using xBotService.Models;
|
||||
using xModels.Extensions;
|
||||
using xCommons.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using xBotService.Interfaces;
|
||||
using xBotService.Models.Dtos;
|
||||
using System.Collections.Generic;
|
||||
using xBotService.Interfaces.Dtos;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace xBotService.Reciever
|
||||
{
|
||||
public class XBotBotUpdateRecieverBackgroundService : XTimeBasedBackgroundService
|
||||
{
|
||||
//
|
||||
public XBotBotUpdateRecieverBackgroundService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<XBotBotUpdateRecieverBackgroundService> logger,
|
||||
int intervalInMinutes = 5
|
||||
) : base(serviceProvider, logger, intervalInMinutes)
|
||||
{ }
|
||||
|
||||
public override async Task DoWorkAsync(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Inject Provider ...
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var botProvider = scope.ServiceProvider.GetRequiredService<IXBotProvider>();
|
||||
var botUserProvider = scope.ServiceProvider.GetRequiredService<IXBotUserServiceProvider>();
|
||||
|
||||
//
|
||||
// Check Provider Service Registered Successfully ...
|
||||
if (botProvider.IsNull() ||
|
||||
botUserProvider.IsNull())
|
||||
{
|
||||
//
|
||||
logger.LogInformation("xBotProvider Service not Provided, ignore ...");
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Try to Retrieve Updates ...
|
||||
IList<XBotUpdateResponse> updates = [];
|
||||
try
|
||||
{
|
||||
//
|
||||
updates = await botProvider.GetUpdates(
|
||||
requestModel: new(),
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
logger.LogError(ex, "Retrieve Updates failed ...");
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Updates Exists ...
|
||||
if (!updates.HasChild())
|
||||
{
|
||||
//
|
||||
logger.LogInformation("there is not any new Updates ...");
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Log ...
|
||||
logger.LogInformation($"Updates: {Environment.NewLine}{updates.ToJSON()}");
|
||||
|
||||
//
|
||||
// HandleRecieved Updates ...
|
||||
foreach (var update in updates)
|
||||
{
|
||||
//
|
||||
logger
|
||||
.LogInformation($"Handle Bot Update: {Environment.NewLine} {update}");
|
||||
|
||||
//
|
||||
// Check Item Exists in Db ...
|
||||
var item = await botUserProvider.FindOneAsync(
|
||||
predicate: x => x.BotUserId.Equals(update.Message.From.Id)
|
||||
);
|
||||
if (item.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
// Not Exists ...
|
||||
item = await botUserProvider.AddAsync(
|
||||
saveChanges: true,
|
||||
connectionId: null,
|
||||
item: new XBotUserDto
|
||||
{
|
||||
BotUserId = update.Message.From.Id,
|
||||
BotUserName = update.Message.From.Username,
|
||||
BotLastname = update.Message.From.LastName,
|
||||
BotFirstname = update.Message.From.FirstName,
|
||||
},
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
logger.LogInformation("Add new BotUser ...");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Exists ...
|
||||
logger.LogInformation("BotUser exists ...");
|
||||
}
|
||||
|
||||
//
|
||||
// Check Verified ...
|
||||
if (item.Verified)
|
||||
{
|
||||
//
|
||||
logger.LogInformation("User Verified ...");
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
// Try to Show Verification Menu on User ...
|
||||
logger
|
||||
.LogInformation($"Show Verification Request Method to User: {item.User.GetFullname()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user