From 3e57ed6af955e9ef88ea329afce81bea7c4af128 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 13 Nov 2026 13:47:53 +0330 Subject: [PATCH] last ... --- Constants/XBotParams.cs | 2 + DI/XDIHelperExtension.cs | 2 +- Interfaces/IXBotProvider.cs | 15 +- Models/Dtos/XBotUserDto.cs | 5 + Models/Entities/XBotUser.cs | 5 + Models/XBotModels.cs | 2 +- Providers/GraphQL/XBotUserGraphType.cs | 1 + Providers/XBotProvider.cs | 79 ++++++---- .../XBotBotUpdateRecieverBackgroundService.cs | 138 ++++++++++++++---- 9 files changed, 185 insertions(+), 64 deletions(-) diff --git a/Constants/XBotParams.cs b/Constants/XBotParams.cs index 062f3dd..f841a52 100644 --- a/Constants/XBotParams.cs +++ b/Constants/XBotParams.cs @@ -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}"; } } \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index 7ecf7d3..afe1042 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -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 { diff --git a/Interfaces/IXBotProvider.cs b/Interfaces/IXBotProvider.cs index be7f22b..fa66f97 100644 --- a/Interfaces/IXBotProvider.cs +++ b/Interfaces/IXBotProvider.cs @@ -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 /// /// Send Verification Code to Requests ... /// - /// - /// - /// + /// /// /// - // Task SendVerificationCode( - // long chatId, - // string code, - // int ttlSeconds, - // CancellationToken cancellationToken = default - // ); + Task SendVerificationCode( + XBotUserDto botUser, + CancellationToken cancellationToken = default + ); /// /// an Accept for Exit Loading on Bot ... diff --git a/Models/Dtos/XBotUserDto.cs b/Models/Dtos/XBotUserDto.cs index 728d118..bee0a81 100644 --- a/Models/Dtos/XBotUserDto.cs +++ b/Models/Dtos/XBotUserDto.cs @@ -27,6 +27,11 @@ namespace xBotService.Models.Dtos /// public DateTime? LastVerificationSent { get; set; } = null; + /// + /// Verification Menu Shown or not ... + /// + public bool ShownVirificationMenu { get; set; } + /// /// Verified On ... /// diff --git a/Models/Entities/XBotUser.cs b/Models/Entities/XBotUser.cs index 801dcce..78c2105 100644 --- a/Models/Entities/XBotUser.cs +++ b/Models/Entities/XBotUser.cs @@ -20,6 +20,11 @@ namespace xBotService.Models.Entities /// public DateTime? LastVerificationSent { get; set; } = null; + /// + /// Verification Menu Shown or not ... + /// + public bool ShownVirificationMenu { get; set; } + /// /// Verified On ... /// diff --git a/Models/XBotModels.cs b/Models/XBotModels.cs index 33918d5..353d7d7 100644 --- a/Models/XBotModels.cs +++ b/Models/XBotModels.cs @@ -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)] diff --git a/Providers/GraphQL/XBotUserGraphType.cs b/Providers/GraphQL/XBotUserGraphType.cs index 2358f75..92ef7fc 100644 --- a/Providers/GraphQL/XBotUserGraphType.cs +++ b/Providers/GraphQL/XBotUserGraphType.cs @@ -14,6 +14,7 @@ namespace xBotService.Providers.GraphQL Field(x => x.BotLastname); Field(x => x.BotFirstname); Field(x => x.LastVerificationSent); + Field(x => x.ShownVirificationMenu); } } } \ No newline at end of file diff --git a/Providers/XBotProvider.cs b/Providers/XBotProvider.cs index 9fe277b..4ea9b88 100644 --- a/Providers/XBotProvider.cs +++ b/Providers/XBotProvider.cs @@ -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 logger; + private readonly IXSecurityProvider securityProvider; private readonly IXIdentityHttpProvider httpProvider; private readonly XValidationProvider validationProvider; private readonly XBotServiceConfiguration configuration; public XBotProvider( ILogger 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 /// /// Send Verification Code to Requests ... /// - /// - /// - /// + /// /// /// - // public virtual async Task 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 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}\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 + { + { XBotParams.XVerificationTokenParam, code }, + { XBotParams.XTTLInMinuteParam, configuration.VerificationCodeTTL.ToString() }, + }); + + // + var response = await SendMessage( + cancellationToken: cancellationToken, + requestModel: new XBotSendMessageRequest + { + Text = message, + ChatId = botUser.BotUserId, + } + ); + + // + return response; + } /// /// an Accept for Exit Loading on Bot ... diff --git a/Reciever/XBotBotUpdateRecieverBackgroundService.cs b/Reciever/XBotBotUpdateRecieverBackgroundService.cs index 9084b5c..21c8933 100644 --- a/Reciever/XBotBotUpdateRecieverBackgroundService.cs +++ b/Reciever/XBotBotUpdateRecieverBackgroundService.cs @@ -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> 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) + /// + /// Handle Bot Sent Commands ... + /// + /// + /// + /// + 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(); + var botUserProvider = scope.ServiceProvider.GetRequiredService(); + // // Switch Betwen Data ... switch (update.CallbackQuery.Data) { // case XBotMessageIds.XBotRequestVerificationCode: - { - // - // Remove Inline Keyboard ... - // And Send Verification ... - break; - } + { + // + // Remove Inline Keyboard ... + // And Send Verification ... + break; + } } } }