Try to Handle Commands ...

This commit is contained in:
2026-11-12 23:48:17 +03:30
parent fa59823008
commit 2b7209c5fb
11 changed files with 412 additions and 64 deletions
+1 -6
View File
@@ -13,15 +13,10 @@ namespace xBotService.Configuration
public string BaseRoute { get; set; } public string BaseRoute { get; set; }
/// <summary> /// <summary>
/// Verification Code Time to Live ... /// Verification Code Time to Live in Minute ...
/// </summary> /// </summary>
public int VerificationCodeTTL { get; set; } = 300; public int VerificationCodeTTL { get; set; } = 300;
/// <summary>
/// Verification Code Length ...
/// </summary>
public int VerificationCodeLength { get; set; } = 6;
/// <summary> /// <summary>
/// Default Timeout ... /// Default Timeout ...
/// </summary> /// </summary>
+14
View File
@@ -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 = "برای دریافت کد اعتبارسنجی روی دکمه زیر کلیک کنید:";
}
}
+16
View File
@@ -0,0 +1,16 @@
using xExceptions.Attributes;
namespace xBotService.Constants
{
public enum XBotStyle
{
[StringValue("primary")]
Primary,
[StringValue("success")]
Success,
[StringValue("danger")]
Danger,
}
}
+92 -1
View File
@@ -1,6 +1,9 @@
using xBotService.Models; using xBotService.Models;
using xCommons.Extensions; using xCommons.Extensions;
using xExceptions.Constants; using xExceptions.Constants;
using xBotService.Models.Dtos;
using xBotService.Configuration;
using xBotService.Models.Entities;
namespace xBotService.Extensions namespace xBotService.Extensions
{ {
@@ -42,6 +45,94 @@ namespace xBotService.Extensions
return result; return result;
} }
/// <summary>
/// Check a User is Verified or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsVerified(
this XBotUser source
)
{
//
var result =
!source.IsNullOrDefault() &&
source.VerifiedOn.HasValue;
//
return result;
}
/// <summary>
/// Check a User is Verified or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsVerified(
this XBotUserDto source
)
{
//
var result =
!source.IsNullOrDefault() &&
source.VerifiedOn.HasValue;
//
return result;
}
/// <summary>
/// Check a User's Verification Code is Expired or not ...
/// </summary>
/// <param name="source"></param>
/// <param name="configuration"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Check a User's Verification Code is Expired or not ...
/// </summary>
/// <param name="source"></param>
/// <param name="configuration"></param>
/// <returns></returns>
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;
}
/// <summary> /// <summary>
/// Normalize ot Update Request ... /// Normalize ot Update Request ...
/// </summary> /// </summary>
@@ -98,7 +189,7 @@ namespace xBotService.Extensions
// //
result = new(); result = new();
result.UpdateData(source); result = source.MapConvert<XBotSendMessageRequestDto, XBotSendMessageRequest>();
// //
return result; return result;
+7 -7
View File
@@ -41,17 +41,17 @@ namespace xBotService.Interfaces
); );
/// <summary> /// <summary>
/// Sending Verification Request Menu ... /// Show Verification Request Menu ...
/// </summary> /// </summary>
/// <param name="chatId"></param> /// <param name="botUserId"></param>
/// <param name="label"></param> /// <param name="label"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
// Task<XBotMessageResponse> SendVerificationMenu( Task<XBotMessage> ShowVerificationMenu(
// long chatId, long botUserId,
// string label = "🔐 دریافت کد وریفیکیشن", string label = XBotMessageIds.XBotRequestVerificationCodeLabel,
// CancellationToken cancellationToken = default CancellationToken cancellationToken = default
// ); );
/// <summary> /// <summary>
/// Send Verification Code to Requests ... /// Send Verification Code to Requests ...
+9 -2
View File
@@ -1,3 +1,4 @@
using System;
using xModels.Base; using xModels.Base;
using xModels.Dtos; using xModels.Dtos;
@@ -21,10 +22,16 @@ namespace xBotService.Models.Dtos
public virtual XPersonDto User { get; set; } = null; public virtual XPersonDto User { get; set; } = null;
/// <summary> /// <summary>
/// Check Bot User is Verified or not ... /// Last Verification Code Sent ...
/// </summary> /// </summary>
/// <value></value> /// <value></value>
public bool Verified { get; set; } public DateTime? LastVerificationSent { get; set; } = null;
/// <summary>
/// Verified On ...
/// </summary>
/// <value></value>
public DateTime? VerifiedOn { get; set; } = null;
/// <summary> /// <summary>
/// Bot User Id ... /// Bot User Id ...
+9 -2
View File
@@ -1,3 +1,4 @@
using System;
using xModels.Base; using xModels.Base;
namespace xBotService.Models.Entities namespace xBotService.Models.Entities
@@ -14,10 +15,16 @@ namespace xBotService.Models.Entities
public string UserId { get; set; } public string UserId { get; set; }
/// <summary> /// <summary>
/// Check Bot User is Verified or not ... /// Last Verification Code Sent ...
/// </summary> /// </summary>
/// <value></value> /// <value></value>
public bool Verified { get; set; } public DateTime? LastVerificationSent { get; set; } = null;
/// <summary>
/// Verified On ...
/// </summary>
/// <value></value>
public DateTime? VerifiedOn { get; set; } = null;
/// <summary> /// <summary>
/// Bot User Id ... /// Bot User Id ...
+161
View File
@@ -2,6 +2,7 @@ using System;
using Newtonsoft.Json; using Newtonsoft.Json;
using xCommons.Extensions; using xCommons.Extensions;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace xBotService.Models namespace xBotService.Models
{ {
@@ -346,5 +347,165 @@ namespace xBotService.Models
public long ReplyToMessageId { get; set; } public long ReplyToMessageId { get; set; }
} }
/// <summary>
/// a Bot Button Keyboard ...
/// </summary>
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;
}
/// <summary>
/// XBot Web App Info ...
/// </summary>
public class XBotWebAppInfo
{
public string Url { get; set; }
}
/// <summary>
/// a Bot Copy Text Button ...
/// </summary>
public class XBotCopyTextButton
{
[StringLength(256)]
public string Text { get; set; }
}
/// <summary>
/// An Inline Keyboard Button ...
/// </summary>
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;
}
/// <summary>
/// Bot Inline Keyboard ...
/// </summary>
public class XBotInlineKeyboardMarkup
{
[JsonProperty("inline_keyboard",
NullValueHandling = NullValueHandling.Ignore)]
public IList<IList<XBotInlineKeyboardButton>> InlineKeyboard { get; set; } = [];
}
/// <summary>
/// Bot Reply Keyboard Markup ...
/// </summary>
public class XBotReplyKeyboardMarkup
{
[JsonProperty("keyboard",
NullValueHandling = NullValueHandling.Ignore)]
public IList<XBotKeyboardButton> Keyboard { get; set; } = [];
}
/// <summary>
/// Reply Bot Keyboard Remove ...
/// </summary>
public class XBotReplyKeyboardRemoveMarkup
{
[JsonProperty("remove_keyboard",
NullValueHandling = NullValueHandling.Ignore)]
public bool RemoveKeyboard { get; set; }
}
/// <summary>
/// Bot Reply Markup ...
/// </summary>
public class XBotReplyMarkup
{
[JsonProperty("reply_markup",
NullValueHandling = NullValueHandling.Ignore)]
public IList<XBotKeyboardButton> 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;
}
}
} }
+2 -1
View File
@@ -8,11 +8,12 @@ namespace xBotService.Providers.GraphQL
public XBotUserGraphType() : base() public XBotUserGraphType() : base()
{ {
Field(x => x.UserId); Field(x => x.UserId);
Field(x => x.Verified);
Field(x => x.BotUserId); Field(x => x.BotUserId);
Field(x => x.VerifiedOn);
Field(x => x.BotUserName); Field(x => x.BotUserName);
Field(x => x.BotLastname); Field(x => x.BotLastname);
Field(x => x.BotFirstname); Field(x => x.BotFirstname);
Field(x => x.LastVerificationSent);
} }
} }
} }
+48 -42
View File
@@ -14,6 +14,7 @@ using System.Collections.Generic;
using xIdentityService.Interfaces; using xIdentityService.Interfaces;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using xExceptions.Constants; using xExceptions.Constants;
using System;
namespace xBotService.Providers namespace xBotService.Providers
{ {
@@ -55,6 +56,9 @@ namespace xBotService.Providers
baseUrl: configuration.BaseRoute baseUrl: configuration.BaseRoute
); );
//
result.Timeout = configuration.DefaultClientTimeout;
// //
return result; return result;
} }
@@ -225,55 +229,57 @@ namespace xBotService.Providers
} }
/// <summary> /// <summary>
/// Sending Verification Request Menu ... /// Show Verification Request Menu ...
/// </summary> /// </summary>
/// <param name="chatId"></param> /// <param name="botUserId"></param>
/// <param name="label"></param> /// <param name="label"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
// public virtual async Task<XBotMessageResponse> SendVerificationMenu( public virtual async Task<XBotMessage> ShowVerificationMenu(
// long chatId, long botUserId,
// string label = "🔐 دریافت کد وریفیکیشن", string label = XBotMessageIds.XBotRequestVerificationCodeLabel,
// CancellationToken cancellationToken = default CancellationToken cancellationToken = default
// ) )
// { {
// // //
// // Validate ... // Validate ...
// if (chatId <= 0 || label.IsNullOrEmpty()) if (botUserId <= 0 || label.IsNullOrEmpty())
// { {
// XException.InvalidArgs.Throw(); XException.InvalidArgs.Throw();
// } }
// // //
// // Markup for Keyboards ... // Markup for Keyboards ...
// var markup = new XBotReplyMarkup var markup = XBotReplyMarkup
// { .InlineKeyboardMarkup(new XBotInlineKeyboardMarkup
// // {
// InlineKeyboard = InlineKeyboard = [
// [ [
// // new XBotInlineKeyboardButton{
// [ Text = label,
// // CallbackData = XBotMessageIds.XBotRequestVerificationCode
// new XBotInlineButton }
// { ]
// Text = label, ]
// CallbackData = "request_verification_code" });
// }
// ]
// ]
// };
// // //
// var response = await SendMessage( var requestModel = new XBotSendMessageRequest
// chatId: chatId, {
// text: "برای دریافت کد وریفیکیشن روی دکمه زیر کلیک کنید:", ChatId = botUserId,
// replyMarkup: markup, ReplyMarkup = markup.ReplyMarkup,
// cancellationToken: cancellationToken Text = XBotMessageIds.XBotRequestVerificationCodeMessageText,
// ); };
//
var result = await SendMessage(
requestModel: requestModel,
cancellationToken: cancellationToken
);
// // //
// return response; return result;
// } }
/// <summary> /// <summary>
/// Send Verification Code to Requests ... /// Send Verification Code to Requests ...
@@ -11,6 +11,8 @@ using System.Collections.Generic;
using xBotService.Interfaces.Dtos; using xBotService.Interfaces.Dtos;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using xBotService.Constants;
using xBotService.Extensions;
namespace xBotService.Reciever namespace xBotService.Reciever
{ {
@@ -73,7 +75,7 @@ namespace xBotService.Reciever
// //
// Log ... // Log ...
logger.LogInformation($"Updates: {Environment.NewLine}{updates.ToJSON()}"); logger.LogInformation($"Updates: {Environment.NewLine}");
// //
// HandleRecieved Updates ... // HandleRecieved Updates ...
@@ -81,7 +83,7 @@ namespace xBotService.Reciever
{ {
// //
logger logger
.LogInformation($"Handle Bot Update: {Environment.NewLine} {update}"); .LogInformation($"Handle Update: {Environment.NewLine} {update.ToJSON()}");
// //
// Check Item Exists in Db ... // Check Item Exists in Db ...
@@ -117,17 +119,65 @@ namespace xBotService.Reciever
// //
// Check Verified ... // Check Verified ...
if (item.Verified) if (item.IsVerified())
{ {
// //
logger.LogInformation("User Verified ..."); logger.LogInformation("User Verified ...");
continue; continue;
} }
//
// TODO: Remove this ...
if (item.BotUserName != "llhajamir")
{
continue;
}
//
// Handle Callback Commands ...
if (!update.CallbackQuery.IsNullOrDefault())
{
//
return;
}
// //
// Try to Show Verification Menu on User ... // Try to Show Verification Menu on User ...
logger logger
.LogInformation($"Show Verification Request Method to User: {item.User.GetFullname()}"); .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;
}
} }
} }
} }