This commit is contained in:
2026-11-13 13:47:53 +03:30
parent 2b7209c5fb
commit 3e57ed6af9
9 changed files with 185 additions and 64 deletions
@@ -13,6 +13,10 @@ 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
{
@@ -86,29 +90,76 @@ namespace xBotService.Reciever
.LogInformation($"Handle Update: {Environment.NewLine} {update.ToJSON()}");
//
// Check Item Exists in Db ...
// 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: x => x.BotUserId.Equals(update.Message.From.Id)
predicate: predicator,
cancellationToken: cancellationToken
);
if (item.IsNullOrDefault())
{
//
// Not Exists ...
item = await botUserProvider.AddAsync(
saveChanges: true,
connectionId: null,
item: new XBotUserDto
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,
},
cancellationToken: cancellationToken
);
};
}
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,
};
}
//
logger.LogInformation("Add new BotUser ...");
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
{
@@ -128,7 +179,7 @@ namespace xBotService.Reciever
//
// TODO: Remove this ...
if (item.BotUserName != "llhajamir")
if (item.BotUserName != "saherelm_it_center")
{
continue;
}
@@ -138,46 +189,81 @@ namespace xBotService.Reciever
if (!update.CallbackQuery.IsNullOrDefault())
{
//
await HandleBotAction(
update: update,
botUserDto: item
);
return;
}
//
// Try to Show Verification Menu on User ...
logger
.LogInformation($"Show Verification Request Method to User: {item.User.GetFullname()}");
var response = await botProvider.ShowVerificationMenu(
botUserId: item.BotUserId,
cancellationToken: cancellationToken
);
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}");
}
}
public async Task HandleBotAction(XBotUpdateResponse update)
/// <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() ||
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;
}
{
//
// Remove Inline Keyboard ...
// And Send Verification ...
break;
}
}
}
}