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 XOffest = "offset";
public const string XTimeout = "timeout"; public const string XTimeout = "timeout";
public const string XTokenParam = "{xBotToken}"; 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 xCommons.Extensions;
using xDataService.Models; using xDataService.Models;
using xPushService.Helpers; using xPushService.Helpers;
using xBotService.Reciever;
using xExceptions.Constants; using xExceptions.Constants;
using xBotService.Constants; using xBotService.Constants;
using xBotService.Providers; using xBotService.Providers;
@@ -23,7 +24,6 @@ using Microsoft.Extensions.Configuration;
using xBotService.Interfaces.Entities.Dtos; using xBotService.Interfaces.Entities.Dtos;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ConfigurationNodeNames = xBotService.Constants.ConfigurationNodeNames; using ConfigurationNodeNames = xBotService.Constants.ConfigurationNodeNames;
using xBotService.Reciever;
namespace xBotService.DI namespace xBotService.DI
{ {
+6 -9
View File
@@ -4,6 +4,7 @@ using xBotService.Constants;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using xCommons.Extensions; using xCommons.Extensions;
using xBotService.Models.Dtos;
namespace xBotService.Interfaces namespace xBotService.Interfaces
{ {
@@ -56,17 +57,13 @@ namespace xBotService.Interfaces
/// <summary> /// <summary>
/// Send Verification Code to Requests ... /// Send Verification Code to Requests ...
/// </summary> /// </summary>
/// <param name="chatId"></param> /// <param name="botUser"></param>
/// <param name="code"></param>
/// <param name="ttlSeconds"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
// Task<XBotMessageResponse> SendVerificationCode( Task<XBotMessage> SendVerificationCode(
// long chatId, XBotUserDto botUser,
// string code, CancellationToken cancellationToken = default
// int ttlSeconds, );
// CancellationToken cancellationToken = default
// );
/// <summary> /// <summary>
/// an Accept for Exit Loading on Bot ... /// an Accept for Exit Loading on Bot ...
+5
View File
@@ -27,6 +27,11 @@ namespace xBotService.Models.Dtos
/// <value></value> /// <value></value>
public DateTime? LastVerificationSent { get; set; } = null; public DateTime? LastVerificationSent { get; set; } = null;
/// <summary>
/// Verification Menu Shown or not ...
/// </summary>
public bool ShownVirificationMenu { get; set; }
/// <summary> /// <summary>
/// Verified On ... /// Verified On ...
/// </summary> /// </summary>
+5
View File
@@ -20,6 +20,11 @@ namespace xBotService.Models.Entities
/// <value></value> /// <value></value>
public DateTime? LastVerificationSent { get; set; } = null; public DateTime? LastVerificationSent { get; set; } = null;
/// <summary>
/// Verification Menu Shown or not ...
/// </summary>
public bool ShownVirificationMenu { get; set; }
/// <summary> /// <summary>
/// Verified On ... /// Verified On ...
/// </summary> /// </summary>
+1 -1
View File
@@ -302,7 +302,7 @@ namespace xBotService.Models
[JsonProperty("from", [JsonProperty("from",
NullValueHandling = NullValueHandling.Ignore)] NullValueHandling = NullValueHandling.Ignore)]
public XBotResUser User { get; set; } public XBotResUser From { get; set; }
[JsonProperty("message", [JsonProperty("message",
NullValueHandling = NullValueHandling.Ignore)] NullValueHandling = NullValueHandling.Ignore)]
+1
View File
@@ -14,6 +14,7 @@ namespace xBotService.Providers.GraphQL
Field(x => x.BotLastname); Field(x => x.BotLastname);
Field(x => x.BotFirstname); Field(x => x.BotFirstname);
Field(x => x.LastVerificationSent); Field(x => x.LastVerificationSent);
Field(x => x.ShownVirificationMenu);
} }
} }
} }
+52 -27
View File
@@ -15,6 +15,8 @@ using xIdentityService.Interfaces;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using xExceptions.Constants; using xExceptions.Constants;
using System; using System;
using xBotService.Models.Dtos;
using xIdentityService.Security;
namespace xBotService.Providers namespace xBotService.Providers
{ {
@@ -22,12 +24,14 @@ namespace xBotService.Providers
{ {
// //
private readonly ILogger<XBotProvider> logger; private readonly ILogger<XBotProvider> logger;
private readonly IXSecurityProvider securityProvider;
private readonly IXIdentityHttpProvider httpProvider; private readonly IXIdentityHttpProvider httpProvider;
private readonly XValidationProvider validationProvider; private readonly XValidationProvider validationProvider;
private readonly XBotServiceConfiguration configuration; private readonly XBotServiceConfiguration configuration;
public XBotProvider( public XBotProvider(
ILogger<XBotProvider> logger, ILogger<XBotProvider> logger,
IXSecurityProvider securityProvider,
IXIdentityHttpProvider httpProvider, IXIdentityHttpProvider httpProvider,
XValidationProvider validationProvider, XValidationProvider validationProvider,
XBotServiceConfiguration configuration XBotServiceConfiguration configuration
@@ -35,6 +39,7 @@ namespace xBotService.Providers
{ {
// //
this.logger = logger; this.logger = logger;
this.securityProvider = securityProvider;
this.httpProvider = httpProvider; this.httpProvider = httpProvider;
this.configuration = configuration; this.configuration = configuration;
this.validationProvider = validationProvider; this.validationProvider = validationProvider;
@@ -270,7 +275,7 @@ namespace xBotService.Providers
ReplyMarkup = markup.ReplyMarkup, ReplyMarkup = markup.ReplyMarkup,
Text = XBotMessageIds.XBotRequestVerificationCodeMessageText, Text = XBotMessageIds.XBotRequestVerificationCodeMessageText,
}; };
// //
var result = await SendMessage( var result = await SendMessage(
requestModel: requestModel, requestModel: requestModel,
@@ -284,36 +289,56 @@ namespace xBotService.Providers
/// <summary> /// <summary>
/// Send Verification Code to Requests ... /// Send Verification Code to Requests ...
/// </summary> /// </summary>
/// <param name="chatId"></param> /// <param name="botUser"></param>
/// <param name="code"></param>
/// <param name="ttlSeconds"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
// public virtual async Task<XBotMessageResponse> SendVerificationCode( public virtual async Task<XBotMessage> SendVerificationCode(
// long chatId, XBotUserDto botUser,
// string code, CancellationToken cancellationToken = default
// int ttlSeconds, )
// CancellationToken cancellationToken = default {
// ) //
// { // Validate ...
// // if (
// // Validate ... botUser.IsNullOrDefault() ||
// if (chatId <= 0 || ttlSeconds < 60 || code.IsNullOrEmpty()) botUser.BotUserId <= 0)
// { {
// XException.InvalidArgs.Throw(); XException.InvalidArgs.Throw();
// } }
// // //
// var minutes = ttlSeconds / 60; // Check User not Verified ...
// var response = await SendMessage( if (botUser.VerifiedOn.HasValue)
// chatId: chatId, {
// cancellationToken: cancellationToken, XException.NotAllowed.Throw();
// text: $"✅ کد وریفیکیشن شما:\n\n<code>{code}</code>\n\n⏱ این کد تا {minutes} دقیقه معتبر است." }
// );
// // //
// 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> /// <summary>
/// an Accept for Exit Loading on Bot ... /// an Accept for Exit Loading on Bot ...
@@ -13,6 +13,10 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using xBotService.Constants; using xBotService.Constants;
using xBotService.Extensions; using xBotService.Extensions;
using System.Linq.Expressions;
using UglyToad.PdfPig.Graphics.Operations.PathConstruction;
using xBotService.Models.Entities;
using xExceptions.Constants;
namespace xBotService.Reciever namespace xBotService.Reciever
{ {
@@ -86,29 +90,76 @@ namespace xBotService.Reciever
.LogInformation($"Handle Update: {Environment.NewLine} {update.ToJSON()}"); .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( var item = await botUserProvider.FindOneAsync(
predicate: x => x.BotUserId.Equals(update.Message.From.Id) predicate: predicator,
cancellationToken: cancellationToken
); );
if (item.IsNullOrDefault()) if (item.IsNullOrDefault())
{ {
// //
// Not Exists ... // Not Exists ...
item = await botUserProvider.AddAsync( var itemForAdd = new XBotUserDto();
saveChanges: true, if (!update.Message.IsNullOrDefault())
connectionId: null, {
item: new XBotUserDto //
itemForAdd = new XBotUserDto
{ {
BotUserId = update.Message.From.Id, BotUserId = update.Message.From.Id,
BotUserName = update.Message.From.Username, BotUserName = update.Message.From.Username,
BotLastname = update.Message.From.LastName, BotLastname = update.Message.From.LastName,
BotFirstname = update.Message.From.FirstName, 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 else
{ {
@@ -128,7 +179,7 @@ namespace xBotService.Reciever
// //
// TODO: Remove this ... // TODO: Remove this ...
if (item.BotUserName != "llhajamir") if (item.BotUserName != "saherelm_it_center")
{ {
continue; continue;
} }
@@ -138,46 +189,81 @@ namespace xBotService.Reciever
if (!update.CallbackQuery.IsNullOrDefault()) if (!update.CallbackQuery.IsNullOrDefault())
{ {
// //
await HandleBotAction(
update: update,
botUserDto: item
);
return; return;
} }
// //
// Try to Show Verification Menu on User ... // Try to Show Verification Menu on User ...
logger if (!item.ShownVirificationMenu)
.LogInformation($"Show Verification Request Method to User: {item.User.GetFullname()}"); {
var response = await botProvider.ShowVerificationMenu( //
botUserId: item.BotUserId, logger
cancellationToken: cancellationToken .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}"); 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 ... // Validate ...
if (update.IsNullOrDefault() || if (update.IsNullOrDefault() ||
update.CallbackQuery.IsNullOrDefault() || update.CallbackQuery.IsNullOrDefault() ||
update.CallbackQuery.Data.IsNullOrEmpty()) update.CallbackQuery.Data.IsNullOrEmpty())
{ {
return; return;
} }
//
// Inject Provider ...
using var scope = serviceProvider.CreateScope();
var botProvider = scope.ServiceProvider.GetRequiredService<IXBotProvider>();
var botUserProvider = scope.ServiceProvider.GetRequiredService<IXBotUserServiceProvider>();
// //
// Switch Betwen Data ... // Switch Betwen Data ...
switch (update.CallbackQuery.Data) switch (update.CallbackQuery.Data)
{ {
// //
case XBotMessageIds.XBotRequestVerificationCode: case XBotMessageIds.XBotRequestVerificationCode:
{ {
// //
// Remove Inline Keyboard ... // Remove Inline Keyboard ...
// And Send Verification ... // And Send Verification ...
break; break;
} }
} }
} }
} }