270 lines
9.3 KiB
C#
270 lines
9.3 KiB
C#
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;
|
|
using xBotService.Constants;
|
|
using xBotService.Extensions;
|
|
using System.Linq.Expressions;
|
|
using UglyToad.PdfPig.Graphics.Operations.PathConstruction;
|
|
using xBotService.Models.Entities;
|
|
using xExceptions.Constants;
|
|
|
|
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}");
|
|
|
|
//
|
|
// HandleRecieved Updates ...
|
|
foreach (var update in updates)
|
|
{
|
|
//
|
|
logger
|
|
.LogInformation($"Handle Update: {Environment.NewLine} {update.ToJSON()}");
|
|
|
|
//
|
|
// Check Item Exists in Db ...
|
|
Expression<Func<XBotUser, bool>> predicator = null;
|
|
if (!update.Message.IsNullOrDefault())
|
|
{
|
|
predicator = x => x.BotUserId.Equals(update.Message.From.Id);
|
|
}
|
|
else if (!update.CallbackQuery.IsNullOrDefault() &&
|
|
!update.CallbackQuery.From.IsNullOrDefault())
|
|
{
|
|
predicator = x => x.BotUserId.Equals(update.CallbackQuery.From.Id);
|
|
}
|
|
else
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var item = await botUserProvider.FindOneAsync(
|
|
predicate: predicator,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (item.IsNullOrDefault())
|
|
{
|
|
//
|
|
// Not Exists ...
|
|
var itemForAdd = new XBotUserDto();
|
|
if (!update.Message.IsNullOrDefault())
|
|
{
|
|
//
|
|
itemForAdd = new XBotUserDto
|
|
{
|
|
BotUserId = update.Message.From.Id,
|
|
BotUserName = update.Message.From.Username,
|
|
BotLastname = update.Message.From.LastName,
|
|
BotFirstname = update.Message.From.FirstName,
|
|
};
|
|
}
|
|
else if (!update.CallbackQuery.IsNullOrDefault() &&
|
|
!update.CallbackQuery.From.IsNullOrDefault())
|
|
{
|
|
//
|
|
itemForAdd = new XBotUserDto
|
|
{
|
|
BotUserId = update.CallbackQuery.From.Id,
|
|
BotUserName = update.CallbackQuery.From.Username,
|
|
BotLastname = update.CallbackQuery.From.LastName,
|
|
BotFirstname = update.CallbackQuery.From.FirstName,
|
|
};
|
|
}
|
|
|
|
//
|
|
if (!itemForAdd.IsNullOrDefault())
|
|
{
|
|
//
|
|
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.IsVerified())
|
|
{
|
|
//
|
|
logger.LogInformation("User Verified ...");
|
|
continue;
|
|
}
|
|
|
|
//
|
|
// TODO: Remove this ...
|
|
if (item.BotUserName != "saherelm_it_center")
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
// Handle Callback Commands ...
|
|
if (!update.CallbackQuery.IsNullOrDefault())
|
|
{
|
|
//
|
|
await HandleBotAction(
|
|
update: update,
|
|
botUserDto: item
|
|
);
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Try to Show Verification Menu on User ...
|
|
if (!item.ShownVirificationMenu)
|
|
{
|
|
//
|
|
logger
|
|
.LogInformation($"Show Verification Request Method to User: {item.User.GetFullname()}");
|
|
var response = await botProvider.ShowVerificationMenu(
|
|
botUserId: item.BotUserId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!response.IsNullOrDefault())
|
|
{
|
|
//
|
|
item.ShownVirificationMenu = true;
|
|
item = await botUserProvider.UpdateAsync(
|
|
item: item,
|
|
id: item.Id,
|
|
saveChanges: true,
|
|
connectionId: null,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
logger.LogInformation($"Verification Menu Shown for User: {item.BotUserId}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handle Bot Sent Commands ...
|
|
/// </summary>
|
|
/// <param name="update"></param>
|
|
/// <param name="botUserDto"></param>
|
|
/// <returns></returns>
|
|
public async Task HandleBotAction(
|
|
XBotUpdateResponse update,
|
|
XBotUserDto botUserDto
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (update.IsNullOrDefault() ||
|
|
update.CallbackQuery.IsNullOrDefault() ||
|
|
update.CallbackQuery.Data.IsNullOrEmpty())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Inject Provider ...
|
|
using var scope = serviceProvider.CreateScope();
|
|
var botProvider = scope.ServiceProvider.GetRequiredService<IXBotProvider>();
|
|
var botUserProvider = scope.ServiceProvider.GetRequiredService<IXBotUserServiceProvider>();
|
|
|
|
//
|
|
// Switch Betwen Data ...
|
|
switch (update.CallbackQuery.Data)
|
|
{
|
|
//
|
|
case XBotMessageIds.XBotRequestVerificationCode:
|
|
{
|
|
//
|
|
// Remove Inline Keyboard ...
|
|
// And Send Verification ...
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |