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
+2
View File
@@ -6,5 +6,7 @@ namespace xBotService.Constants
public const string XOffest = "offset";
public const string XTimeout = "timeout";
public const string XTokenParam = "{xBotToken}";
public const string XTTLInMinuteParam = "{xTTLInMinute}";
public const string XVerificationTokenParam = "{xVerificationCode}";
}
}
+1 -1
View File
@@ -6,6 +6,7 @@ using xCommons.Helpers;
using xCommons.Extensions;
using xDataService.Models;
using xPushService.Helpers;
using xBotService.Reciever;
using xExceptions.Constants;
using xBotService.Constants;
using xBotService.Providers;
@@ -23,7 +24,6 @@ using Microsoft.Extensions.Configuration;
using xBotService.Interfaces.Entities.Dtos;
using Microsoft.Extensions.DependencyInjection;
using ConfigurationNodeNames = xBotService.Constants.ConfigurationNodeNames;
using xBotService.Reciever;
namespace xBotService.DI
{
+6 -9
View File
@@ -4,6 +4,7 @@ using xBotService.Constants;
using System.Threading.Tasks;
using System.Collections.Generic;
using xCommons.Extensions;
using xBotService.Models.Dtos;
namespace xBotService.Interfaces
{
@@ -56,17 +57,13 @@ namespace xBotService.Interfaces
/// <summary>
/// Send Verification Code to Requests ...
/// </summary>
/// <param name="chatId"></param>
/// <param name="code"></param>
/// <param name="ttlSeconds"></param>
/// <param name="botUser"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
// Task<XBotMessageResponse> SendVerificationCode(
// long chatId,
// string code,
// int ttlSeconds,
// CancellationToken cancellationToken = default
// );
Task<XBotMessage> SendVerificationCode(
XBotUserDto botUser,
CancellationToken cancellationToken = default
);
/// <summary>
/// an Accept for Exit Loading on Bot ...
+5
View File
@@ -27,6 +27,11 @@ namespace xBotService.Models.Dtos
/// <value></value>
public DateTime? LastVerificationSent { get; set; } = null;
/// <summary>
/// Verification Menu Shown or not ...
/// </summary>
public bool ShownVirificationMenu { get; set; }
/// <summary>
/// Verified On ...
/// </summary>
+5
View File
@@ -20,6 +20,11 @@ namespace xBotService.Models.Entities
/// <value></value>
public DateTime? LastVerificationSent { get; set; } = null;
/// <summary>
/// Verification Menu Shown or not ...
/// </summary>
public bool ShownVirificationMenu { get; set; }
/// <summary>
/// Verified On ...
/// </summary>
+1 -1
View File
@@ -302,7 +302,7 @@ namespace xBotService.Models
[JsonProperty("from",
NullValueHandling = NullValueHandling.Ignore)]
public XBotResUser User { get; set; }
public XBotResUser From { get; set; }
[JsonProperty("message",
NullValueHandling = NullValueHandling.Ignore)]
+1
View File
@@ -14,6 +14,7 @@ namespace xBotService.Providers.GraphQL
Field(x => x.BotLastname);
Field(x => x.BotFirstname);
Field(x => x.LastVerificationSent);
Field(x => x.ShownVirificationMenu);
}
}
}
+52 -27
View File
@@ -15,6 +15,8 @@ using xIdentityService.Interfaces;
using Microsoft.Extensions.Logging;
using xExceptions.Constants;
using System;
using xBotService.Models.Dtos;
using xIdentityService.Security;
namespace xBotService.Providers
{
@@ -22,12 +24,14 @@ namespace xBotService.Providers
{
//
private readonly ILogger<XBotProvider> logger;
private readonly IXSecurityProvider securityProvider;
private readonly IXIdentityHttpProvider httpProvider;
private readonly XValidationProvider validationProvider;
private readonly XBotServiceConfiguration configuration;
public XBotProvider(
ILogger<XBotProvider> logger,
IXSecurityProvider securityProvider,
IXIdentityHttpProvider httpProvider,
XValidationProvider validationProvider,
XBotServiceConfiguration configuration
@@ -35,6 +39,7 @@ namespace xBotService.Providers
{
//
this.logger = logger;
this.securityProvider = securityProvider;
this.httpProvider = httpProvider;
this.configuration = configuration;
this.validationProvider = validationProvider;
@@ -270,7 +275,7 @@ namespace xBotService.Providers
ReplyMarkup = markup.ReplyMarkup,
Text = XBotMessageIds.XBotRequestVerificationCodeMessageText,
};
//
var result = await SendMessage(
requestModel: requestModel,
@@ -284,36 +289,56 @@ namespace xBotService.Providers
/// <summary>
/// Send Verification Code to Requests ...
/// </summary>
/// <param name="chatId"></param>
/// <param name="code"></param>
/// <param name="ttlSeconds"></param>
/// <param name="botUser"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
// public virtual async Task<XBotMessageResponse> SendVerificationCode(
// long chatId,
// string code,
// int ttlSeconds,
// CancellationToken cancellationToken = default
// )
// {
// //
// // Validate ...
// if (chatId <= 0 || ttlSeconds < 60 || code.IsNullOrEmpty())
// {
// XException.InvalidArgs.Throw();
// }
public virtual async Task<XBotMessage> SendVerificationCode(
XBotUserDto botUser,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (
botUser.IsNullOrDefault() ||
botUser.BotUserId <= 0)
{
XException.InvalidArgs.Throw();
}
// //
// var minutes = ttlSeconds / 60;
// var response = await SendMessage(
// chatId: chatId,
// cancellationToken: cancellationToken,
// text: $"✅ کد وریفیکیشن شما:\n\n<code>{code}</code>\n\n⏱ این کد تا {minutes} دقیقه معتبر است."
// );
//
// Check User not Verified ...
if (botUser.VerifiedOn.HasValue)
{
XException.NotAllowed.Throw();
}
// //
// return response;
// }
//
var code = securityProvider
.Encrypt(botUser.BotUserId.ToString())
.ToMd5String();
//
var message = XBotMessageIds.XBotVerificationCodeMessageText
.ApplyTemplates(new Dictionary<string, string>
{
{ XBotParams.XVerificationTokenParam, code },
{ XBotParams.XTTLInMinuteParam, configuration.VerificationCodeTTL.ToString() },
});
//
var response = await SendMessage(
cancellationToken: cancellationToken,
requestModel: new XBotSendMessageRequest
{
Text = message,
ChatId = botUser.BotUserId,
}
);
//
return response;
}
/// <summary>
/// an Accept for Exit Loading on Bot ...
@@ -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;
}
}
}
}