diff --git a/Configuration/XBotServiceConfiguration.cs b/Configuration/XBotServiceConfiguration.cs index 0d0966c..677c39b 100644 --- a/Configuration/XBotServiceConfiguration.cs +++ b/Configuration/XBotServiceConfiguration.cs @@ -13,15 +13,10 @@ namespace xBotService.Configuration public string BaseRoute { get; set; } /// - /// Verification Code Time to Live ... + /// Verification Code Time to Live in Minute ... /// public int VerificationCodeTTL { get; set; } = 300; - /// - /// Verification Code Length ... - /// - public int VerificationCodeLength { get; set; } = 6; - /// /// Default Timeout ... /// diff --git a/Constants/XBotMessageIds.cs b/Constants/XBotMessageIds.cs new file mode 100644 index 0000000..0a3bbfe --- /dev/null +++ b/Constants/XBotMessageIds.cs @@ -0,0 +1,14 @@ +namespace xBotService.Constants +{ + public struct XBotMessageIds + { + public const string XBotRequestVerificationCode = "request_verification_code"; + public const string XBotRequestVerificationCodeLabel = "🔐 دریافت کد اعتبارسنجی"; + public const string XBotVerificationCodeMessageText = """ + کد اعتبارسنجی شما: {xVerificationCode} + مدت اعتبار {xTTLInMinute} دقیقه + این کد را با ورود به سامانه در بخش پروفایل کاربری وارد کنید + """; + public const string XBotRequestVerificationCodeMessageText = "برای دریافت کد اعتبارسنجی روی دکمه زیر کلیک کنید:"; + } +} \ No newline at end of file diff --git a/Constants/XBotStyle.cs b/Constants/XBotStyle.cs new file mode 100644 index 0000000..9cbc567 --- /dev/null +++ b/Constants/XBotStyle.cs @@ -0,0 +1,16 @@ +using xExceptions.Attributes; + +namespace xBotService.Constants +{ + public enum XBotStyle + { + [StringValue("primary")] + Primary, + + [StringValue("success")] + Success, + + [StringValue("danger")] + Danger, + } +} \ No newline at end of file diff --git a/Extensions/XBotServiceExtensions.cs b/Extensions/XBotServiceExtensions.cs index fbe532d..794ea5e 100644 --- a/Extensions/XBotServiceExtensions.cs +++ b/Extensions/XBotServiceExtensions.cs @@ -1,6 +1,9 @@ using xBotService.Models; using xCommons.Extensions; using xExceptions.Constants; +using xBotService.Models.Dtos; +using xBotService.Configuration; +using xBotService.Models.Entities; namespace xBotService.Extensions { @@ -42,6 +45,94 @@ namespace xBotService.Extensions return result; } + /// + /// Check a User is Verified or not ... + /// + /// + /// + public static bool IsVerified( + this XBotUser source + ) + { + // + var result = + !source.IsNullOrDefault() && + source.VerifiedOn.HasValue; + + // + return result; + } + + /// + /// Check a User is Verified or not ... + /// + /// + /// + public static bool IsVerified( + this XBotUserDto source + ) + { + // + var result = + !source.IsNullOrDefault() && + source.VerifiedOn.HasValue; + + // + return result; + } + + /// + /// Check a User's Verification Code is Expired or not ... + /// + /// + /// + /// + public static bool IsExpired( + this XBotUser source, + XBotServiceConfiguration configuration + ) + { + // + // Validate ... + if (configuration.IsNull() || + source.IsNullOrDefault() || + !source.VerifiedOn.HasValue || + configuration.VerificationCodeTTL <= 0) + { + XException.InvalidArgs.Throw(); + } + + // + var result = source.VerifiedOn.Value.IsExpiredTTL(configuration.VerificationCodeTTL); + return result; + } + + /// + /// Check a User's Verification Code is Expired or not ... + /// + /// + /// + /// + public static bool IsExpired( + this XBotUserDto source, + XBotServiceConfiguration configuration + ) + { + // + // Validate ... + if (configuration.IsNull() || + source.IsNullOrDefault() || + !source.VerifiedOn.HasValue || + configuration.VerificationCodeTTL <= 0) + { + XException.InvalidArgs.Throw(); + } + + // + var result = source.VerifiedOn.Value.IsExpiredTTL(configuration.VerificationCodeTTL); + return result; + } + /// /// Normalize ot Update Request ... /// @@ -98,7 +189,7 @@ namespace xBotService.Extensions // result = new(); - result.UpdateData(source); + result = source.MapConvert(); // return result; diff --git a/Interfaces/IXBotProvider.cs b/Interfaces/IXBotProvider.cs index 8ecca6c..be7f22b 100644 --- a/Interfaces/IXBotProvider.cs +++ b/Interfaces/IXBotProvider.cs @@ -41,17 +41,17 @@ namespace xBotService.Interfaces ); /// - /// Sending Verification Request Menu ... + /// Show Verification Request Menu ... /// - /// + /// /// /// /// - // Task SendVerificationMenu( - // long chatId, - // string label = "🔐 دریافت کد وریفیکیشن", - // CancellationToken cancellationToken = default - // ); + Task ShowVerificationMenu( + long botUserId, + string label = XBotMessageIds.XBotRequestVerificationCodeLabel, + CancellationToken cancellationToken = default + ); /// /// Send Verification Code to Requests ... diff --git a/Models/Dtos/XBotUserDto.cs b/Models/Dtos/XBotUserDto.cs index 82bcffb..728d118 100644 --- a/Models/Dtos/XBotUserDto.cs +++ b/Models/Dtos/XBotUserDto.cs @@ -1,3 +1,4 @@ +using System; using xModels.Base; using xModels.Dtos; @@ -21,10 +22,16 @@ namespace xBotService.Models.Dtos public virtual XPersonDto User { get; set; } = null; /// - /// Check Bot User is Verified or not ... + /// Last Verification Code Sent ... /// /// - public bool Verified { get; set; } + public DateTime? LastVerificationSent { get; set; } = null; + + /// + /// Verified On ... + /// + /// + public DateTime? VerifiedOn { get; set; } = null; /// /// Bot User Id ... diff --git a/Models/Entities/XBotUser.cs b/Models/Entities/XBotUser.cs index b5bd194..801dcce 100644 --- a/Models/Entities/XBotUser.cs +++ b/Models/Entities/XBotUser.cs @@ -1,3 +1,4 @@ +using System; using xModels.Base; namespace xBotService.Models.Entities @@ -14,10 +15,16 @@ namespace xBotService.Models.Entities public string UserId { get; set; } /// - /// Check Bot User is Verified or not ... + /// Last Verification Code Sent ... /// /// - public bool Verified { get; set; } + public DateTime? LastVerificationSent { get; set; } = null; + + /// + /// Verified On ... + /// + /// + public DateTime? VerifiedOn { get; set; } = null; /// /// Bot User Id ... diff --git a/Models/XBotModels.cs b/Models/XBotModels.cs index 1bcd7f5..33918d5 100644 --- a/Models/XBotModels.cs +++ b/Models/XBotModels.cs @@ -2,6 +2,7 @@ using System; using Newtonsoft.Json; using xCommons.Extensions; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace xBotService.Models { @@ -346,5 +347,165 @@ namespace xBotService.Models public long ReplyToMessageId { get; set; } } + /// + /// a Bot Button Keyboard ... + /// + public class XBotKeyboardButton + { + [JsonProperty("text", + NullValueHandling = NullValueHandling.Ignore)] + public string Text { get; set; } + [JsonProperty("style", + NullValueHandling = NullValueHandling.Ignore)] + public string Style { get; set; } + + [JsonProperty("request_contact", + NullValueHandling = NullValueHandling.Ignore)] + public bool RequestContact { get; set; } + + [JsonProperty("request_location", + NullValueHandling = NullValueHandling.Ignore)] + public bool RequestLocation { get; set; } + + [JsonProperty("web_app", + NullValueHandling = NullValueHandling.Ignore)] + public XBotWebAppInfo WebApp { get; set; } = null; + } + + /// + /// XBot Web App Info ... + /// + public class XBotWebAppInfo + { + public string Url { get; set; } + } + + /// + /// a Bot Copy Text Button ... + /// + public class XBotCopyTextButton + { + [StringLength(256)] + public string Text { get; set; } + } + + /// + /// An Inline Keyboard Button ... + /// + public class XBotInlineKeyboardButton + { + [JsonProperty("url", + NullValueHandling = NullValueHandling.Ignore)] + public string Url { get; set; } + + [JsonProperty("text", + NullValueHandling = NullValueHandling.Ignore)] + public string Text { get; set; } + + [JsonProperty("style", + NullValueHandling = NullValueHandling.Ignore)] + public string Style { get; set; } + + [JsonProperty("callback_data", + NullValueHandling = NullValueHandling.Ignore)] + public string CallbackData { get; set; } + + [JsonProperty("disabled", + NullValueHandling = NullValueHandling.Ignore)] + public object Disabled { get; set; } = null; + + [JsonProperty("web_app", + NullValueHandling = NullValueHandling.Ignore)] + public XBotWebAppInfo WebApp { get; set; } = null; + + [JsonProperty("copy_text", + NullValueHandling = NullValueHandling.Ignore)] + public XBotCopyTextButton CopyText { get; set; } = null; + } + + /// + /// Bot Inline Keyboard ... + /// + public class XBotInlineKeyboardMarkup + { + [JsonProperty("inline_keyboard", + NullValueHandling = NullValueHandling.Ignore)] + public IList> InlineKeyboard { get; set; } = []; + } + + /// + /// Bot Reply Keyboard Markup ... + /// + public class XBotReplyKeyboardMarkup + { + [JsonProperty("keyboard", + NullValueHandling = NullValueHandling.Ignore)] + public IList Keyboard { get; set; } = []; + } + + /// + /// Reply Bot Keyboard Remove ... + /// + public class XBotReplyKeyboardRemoveMarkup + { + [JsonProperty("remove_keyboard", + NullValueHandling = NullValueHandling.Ignore)] + public bool RemoveKeyboard { get; set; } + } + + /// + /// Bot Reply Markup ... + /// + public class XBotReplyMarkup + { + [JsonProperty("reply_markup", + NullValueHandling = NullValueHandling.Ignore)] + public IList Keyboard { get; set; } = []; + public object ReplyMarkup { get; set; } + + public static XBotReplyMarkup InlineKeyboardMarkup( + XBotInlineKeyboardMarkup source + ) + { + // + var result = new XBotReplyMarkup + { + ReplyMarkup = source + }; + + // + return result; + } + + public static XBotReplyMarkup ReplyKeyboardMarkup( + XBotReplyKeyboardMarkup source + ) + { + // + var data = source.ToJSON(); + var result = new XBotReplyMarkup + { + ReplyMarkup = data + }; + + // + return result; + } + + public static XBotReplyMarkup ReplyKeyboardRemoveMarkup( + XBotReplyKeyboardRemoveMarkup source + ) + { + // + var data = source.ToJSON(); + var result = new XBotReplyMarkup + { + ReplyMarkup = data + }; + + // + return result; + } + } } \ No newline at end of file diff --git a/Providers/GraphQL/XBotUserGraphType.cs b/Providers/GraphQL/XBotUserGraphType.cs index 149f7a7..2358f75 100644 --- a/Providers/GraphQL/XBotUserGraphType.cs +++ b/Providers/GraphQL/XBotUserGraphType.cs @@ -8,11 +8,12 @@ namespace xBotService.Providers.GraphQL public XBotUserGraphType() : base() { Field(x => x.UserId); - Field(x => x.Verified); Field(x => x.BotUserId); + Field(x => x.VerifiedOn); Field(x => x.BotUserName); Field(x => x.BotLastname); Field(x => x.BotFirstname); + Field(x => x.LastVerificationSent); } } } \ No newline at end of file diff --git a/Providers/XBotProvider.cs b/Providers/XBotProvider.cs index 9e57737..9fe277b 100644 --- a/Providers/XBotProvider.cs +++ b/Providers/XBotProvider.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using xIdentityService.Interfaces; using Microsoft.Extensions.Logging; using xExceptions.Constants; +using System; namespace xBotService.Providers { @@ -55,6 +56,9 @@ namespace xBotService.Providers baseUrl: configuration.BaseRoute ); + // + result.Timeout = configuration.DefaultClientTimeout; + // return result; } @@ -225,55 +229,57 @@ namespace xBotService.Providers } /// - /// Sending Verification Request Menu ... + /// Show Verification Request Menu ... /// - /// + /// /// /// /// - // public virtual async Task SendVerificationMenu( - // long chatId, - // string label = "🔐 دریافت کد وریفیکیشن", - // CancellationToken cancellationToken = default - // ) - // { - // // - // // Validate ... - // if (chatId <= 0 || label.IsNullOrEmpty()) - // { - // XException.InvalidArgs.Throw(); - // } + public virtual async Task ShowVerificationMenu( + long botUserId, + string label = XBotMessageIds.XBotRequestVerificationCodeLabel, + CancellationToken cancellationToken = default + ) + { + // + // Validate ... + if (botUserId <= 0 || label.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); + } - // // - // // Markup for Keyboards ... - // var markup = new XBotReplyMarkup - // { - // // - // InlineKeyboard = - // [ - // // - // [ - // // - // new XBotInlineButton - // { - // Text = label, - // CallbackData = "request_verification_code" - // } - // ] - // ] - // }; + // + // Markup for Keyboards ... + var markup = XBotReplyMarkup + .InlineKeyboardMarkup(new XBotInlineKeyboardMarkup + { + InlineKeyboard = [ + [ + new XBotInlineKeyboardButton{ + Text = label, + CallbackData = XBotMessageIds.XBotRequestVerificationCode + } + ] + ] + }); - // // - // var response = await SendMessage( - // chatId: chatId, - // text: "برای دریافت کد وریفیکیشن روی دکمه زیر کلیک کنید:", - // replyMarkup: markup, - // cancellationToken: cancellationToken - // ); + // + var requestModel = new XBotSendMessageRequest + { + ChatId = botUserId, + ReplyMarkup = markup.ReplyMarkup, + Text = XBotMessageIds.XBotRequestVerificationCodeMessageText, + }; + + // + var result = await SendMessage( + requestModel: requestModel, + cancellationToken: cancellationToken + ); - // // - // return response; - // } + // + return result; + } /// /// Send Verification Code to Requests ... diff --git a/Reciever/XBotBotUpdateRecieverBackgroundService.cs b/Reciever/XBotBotUpdateRecieverBackgroundService.cs index 1838a70..9084b5c 100644 --- a/Reciever/XBotBotUpdateRecieverBackgroundService.cs +++ b/Reciever/XBotBotUpdateRecieverBackgroundService.cs @@ -11,6 +11,8 @@ using System.Collections.Generic; using xBotService.Interfaces.Dtos; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; +using xBotService.Constants; +using xBotService.Extensions; namespace xBotService.Reciever { @@ -73,7 +75,7 @@ namespace xBotService.Reciever // // Log ... - logger.LogInformation($"Updates: {Environment.NewLine}{updates.ToJSON()}"); + logger.LogInformation($"Updates: {Environment.NewLine}"); // // HandleRecieved Updates ... @@ -81,7 +83,7 @@ namespace xBotService.Reciever { // logger - .LogInformation($"Handle Bot Update: {Environment.NewLine} {update}"); + .LogInformation($"Handle Update: {Environment.NewLine} {update.ToJSON()}"); // // Check Item Exists in Db ... @@ -117,17 +119,65 @@ namespace xBotService.Reciever // // Check Verified ... - if (item.Verified) + if (item.IsVerified()) { // logger.LogInformation("User Verified ..."); continue; } + // + // TODO: Remove this ... + if (item.BotUserName != "llhajamir") + { + continue; + } + + // + // Handle Callback Commands ... + if (!update.CallbackQuery.IsNullOrDefault()) + { + // + 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 + ); + + // + logger.LogInformation($"Verification Menu Shown for User: {item.BotUserId}"); + } + } + + public async Task HandleBotAction(XBotUpdateResponse update) + { + // + // Validate ... + if (update.IsNullOrDefault() || + update.CallbackQuery.IsNullOrDefault() || + update.CallbackQuery.Data.IsNullOrEmpty()) + { + return; + } + + // + // Switch Betwen Data ... + switch (update.CallbackQuery.Data) + { + // + case XBotMessageIds.XBotRequestVerificationCode: + { + // + // Remove Inline Keyboard ... + // And Send Verification ... + break; + } } } }