Compare commits
6
Commits
1e2eb030e8
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
30c75ba03d | ||
|
|
3e57ed6af9 | ||
|
|
2b7209c5fb | ||
|
|
fa59823008 | ||
|
|
d3b5ba622f | ||
|
|
ed1cf5e073 |
@@ -0,0 +1,25 @@
|
|||||||
|
namespace xBotService.Configuration
|
||||||
|
{
|
||||||
|
public class XBotServiceConfiguration
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Bot Required Token ...
|
||||||
|
/// </summary>
|
||||||
|
public string Token { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Base Route of Bot Api ...
|
||||||
|
/// </summary>
|
||||||
|
public string BaseRoute { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verification Code Time to Live in Minute ...
|
||||||
|
/// </summary>
|
||||||
|
public int VerificationCodeTTL { get; set; } = 5;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Timeout ...
|
||||||
|
/// </summary>
|
||||||
|
public int DefaultClientTimeout { get; set; } = 360;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using xExceptions.Attributes;
|
||||||
|
|
||||||
|
namespace xBotService.Constants
|
||||||
|
{
|
||||||
|
public enum XBotEndpoints
|
||||||
|
{
|
||||||
|
[StringValue("/bot{xBotToken}/getMe")]
|
||||||
|
GetMe,
|
||||||
|
|
||||||
|
[StringValue("/bot{xBotToken}/getChat")]
|
||||||
|
GetChat,
|
||||||
|
|
||||||
|
[StringValue("/bot{xBotToken}/sendMessage")]
|
||||||
|
SendMessage,
|
||||||
|
|
||||||
|
[StringValue("/bot{xBotToken}/getUpdates")]
|
||||||
|
GetUpdates,
|
||||||
|
|
||||||
|
[StringValue("/bot{xBotToken}/setWebhook")]
|
||||||
|
SetWebhook,
|
||||||
|
|
||||||
|
[StringValue("/bot{xBotToken}/deleteWebhook")]
|
||||||
|
DeleteWebhook,
|
||||||
|
|
||||||
|
[StringValue("/bot{xBotToken}/answerCallbackQuery")]
|
||||||
|
AnswerCallbackQuery,
|
||||||
|
|
||||||
|
[StringValue("/bot{xBotToken}/editMessageReplyMarkup")]
|
||||||
|
EditMessageReplyMarkup,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
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 = "برای دریافت کد اعتبارسنجی روی دکمه زیر کلیک کنید:";
|
||||||
|
public const string XBotUserVerificationSucceedMessageText = "کاربر گرامی، اعتبار سنجی شما با موفقیت انجام شد.";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using xExceptions.Attributes;
|
||||||
|
|
||||||
|
namespace xBotService.Constants
|
||||||
|
{
|
||||||
|
public enum XBotMessageParseMode
|
||||||
|
{
|
||||||
|
[StringValue("HTML")]
|
||||||
|
Html,
|
||||||
|
|
||||||
|
[StringValue("Markdown")]
|
||||||
|
Markdown
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace xBotService.Constants
|
||||||
|
{
|
||||||
|
public struct XBotParams
|
||||||
|
{
|
||||||
|
public const string XLimit = "limit";
|
||||||
|
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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using xExceptions.Attributes;
|
||||||
|
|
||||||
|
namespace xBotService.Constants
|
||||||
|
{
|
||||||
|
public enum XBotStyle
|
||||||
|
{
|
||||||
|
[StringValue("primary")]
|
||||||
|
Primary,
|
||||||
|
|
||||||
|
[StringValue("success")]
|
||||||
|
Success,
|
||||||
|
|
||||||
|
[StringValue("danger")]
|
||||||
|
Danger,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Providers;
|
||||||
|
using xBotService.Models;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xBotService.Interfaces;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using xIdentityService.Controllers;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xBotService.Reciever;
|
||||||
|
|
||||||
|
namespace xBotService.Controllers
|
||||||
|
{
|
||||||
|
public abstract class XBotProviderControllerBase : XBaseIdentityApiV1Controller
|
||||||
|
{
|
||||||
|
private readonly IXBotProvider botProvider;
|
||||||
|
private readonly XBotBotUpdateRecieverBackgroundService botHandler;
|
||||||
|
|
||||||
|
protected XBotProviderControllerBase(
|
||||||
|
IXBotProvider botProvider,
|
||||||
|
XAppConfiguration appConfiguration,
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
|
XValidationProvider validationProvider,
|
||||||
|
ILogger<XBotProviderControllerBase> logger,
|
||||||
|
XBotBotUpdateRecieverBackgroundService botHandler
|
||||||
|
) : base(
|
||||||
|
logger,
|
||||||
|
appConfiguration,
|
||||||
|
identityProvider,
|
||||||
|
validationProvider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
this.botProvider = botProvider;
|
||||||
|
this.botHandler = botHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Actions ...
|
||||||
|
[HttpGet("Me")]
|
||||||
|
public virtual async Task<ActionResult<XBotResUser>> GetMe(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await botProvider.GetMe(cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result.ToDynamicObject());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("Updates")]
|
||||||
|
public virtual async Task<ActionResult<IList<XBotUpdateResponse>>> GetUpdates(
|
||||||
|
[FromQuery] XBotUpdatesRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validation ...
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await botProvider.GetUpdates(
|
||||||
|
requestModel: request,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result.ToDynamicObject());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("Message")]
|
||||||
|
public virtual async Task<ActionResult<XBotMessage>> SendMessage(
|
||||||
|
[FromBody] XBotSendMessageRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validation ...
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
await ValidationProvider.GroupValidationBuilder()
|
||||||
|
.AddNotNull(request)
|
||||||
|
.AddNotEmpty(request.Text)
|
||||||
|
.AddNotZero(request.ChatId)
|
||||||
|
.ValidateGroupAsync();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await botProvider.SendMessage(
|
||||||
|
requestModel: request,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result.ToDynamicObject());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("{botUserId}/ShowVerificationMenu")]
|
||||||
|
public virtual async Task<ActionResult<XBotMessage>> ShowVerificationMenu(
|
||||||
|
[FromRoute] long botUserId,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validation ...
|
||||||
|
if (botUserId <= 0)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await botProvider.ShowVerificationMenu(
|
||||||
|
botUserId: botUserId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result.ToDynamicObject());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("{token}/VerifyUser")]
|
||||||
|
public virtual async Task<ActionResult<XBotMessage>> VerifyUser(
|
||||||
|
[FromRoute] string token,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validation ...
|
||||||
|
if (token.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await botProvider.VerifyUser(
|
||||||
|
token: token,
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result.ToDynamicObject());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("Refresh")]
|
||||||
|
public virtual async Task<ActionResult> Refresh(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
await botHandler.Refresh(cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
+210
-21
@@ -1,70 +1,230 @@
|
|||||||
using System;
|
using System;
|
||||||
using xDataService.Db;
|
using xDataService.Db;
|
||||||
using xDataService.DI;
|
using xDataService.DI;
|
||||||
|
using xPushService.DI;
|
||||||
using xCommons.Helpers;
|
using xCommons.Helpers;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xDataService.Models;
|
using xDataService.Models;
|
||||||
|
using xPushService.Helpers;
|
||||||
|
using xBotService.Reciever;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
using xBotService.Constants;
|
using xBotService.Constants;
|
||||||
|
using xBotService.Providers;
|
||||||
using xDataService.Constants;
|
using xDataService.Constants;
|
||||||
|
using xBotService.Interfaces;
|
||||||
using XBotUserService.Helpers;
|
using XBotUserService.Helpers;
|
||||||
using xHttpService.Interfaces;
|
using xHttpService.Interfaces;
|
||||||
|
using xBotService.Configuration;
|
||||||
using xBotService.Providers.Dtos;
|
using xBotService.Providers.Dtos;
|
||||||
|
using xBotService.Interfaces.Dtos;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using xBotService.Providers.Entities;
|
||||||
|
using xBotService.Interfaces.Entities;
|
||||||
|
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;
|
||||||
|
|
||||||
namespace xBotService.DI
|
namespace xBotService.DI
|
||||||
{
|
{
|
||||||
public static class XDIHelperExtension
|
public static class XDIHelperExtension
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Register XBotService
|
/// Retrieve Bot Service Configuration ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XBotServiceConfiguration GetXBotServiceConfiguration(
|
||||||
|
this IConfiguration source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var section = source
|
||||||
|
.GetSection(ConfigurationNodeNames.BOT_SERVICE_NODE);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = section.Get<XBotServiceConfiguration>();
|
||||||
|
if (result.IsNull())
|
||||||
|
{
|
||||||
|
result = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extract Service Configurations and Register it ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
public static void AddXBotServiceConfiguration(
|
||||||
|
this IServiceCollection source,
|
||||||
|
IConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var config = configuration.GetXBotServiceConfiguration();
|
||||||
|
source.AddXBotServiceConfiguration(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register Service Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
public static void AddXBotServiceConfiguration(
|
||||||
|
this IServiceCollection source,
|
||||||
|
XBotServiceConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (configuration.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Log("Service Registration faild, Invalid Configuration ...");
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
source.AddSingleton(configuration);
|
||||||
|
Log("Service Configuration, Registered ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register Service ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <param name="repositoryType"></param>
|
||||||
|
/// <param name="lifeTime"></param>
|
||||||
public static void AddXBotService(
|
public static void AddXBotService(
|
||||||
this IServiceCollection services,
|
this IServiceCollection services,
|
||||||
|
IConfiguration configuration,
|
||||||
XRepositoryType repositoryType,
|
XRepositoryType repositoryType,
|
||||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
var config = configuration.GetXBotServiceConfiguration();
|
||||||
|
|
||||||
|
//
|
||||||
AddBotService(
|
AddBotService(
|
||||||
contextType: null,
|
contextType: null,
|
||||||
services: services,
|
services: services,
|
||||||
lifeTime: lifeTime,
|
lifeTime: lifeTime,
|
||||||
|
configuration: config,
|
||||||
repositoryType: repositoryType
|
repositoryType: repositoryType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Register XBotService with Context
|
/// Register Service with Context ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <param name="repositoryType"></param>
|
||||||
|
/// <param name="lifeTime"></param>
|
||||||
|
/// <typeparam name="TContext"></typeparam>
|
||||||
public static void AddXBotService<TContext>(
|
public static void AddXBotService<TContext>(
|
||||||
this IServiceCollection services,
|
this IServiceCollection services,
|
||||||
|
IConfiguration configuration,
|
||||||
XRepositoryType repositoryType,
|
XRepositoryType repositoryType,
|
||||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||||
) where TContext : XDbContext
|
) where TContext : XDbContext
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
var config = configuration.GetXBotServiceConfiguration();
|
||||||
|
|
||||||
|
//
|
||||||
AddBotService(
|
AddBotService(
|
||||||
services: services,
|
services: services,
|
||||||
lifeTime: lifeTime,
|
lifeTime: lifeTime,
|
||||||
|
configuration: config,
|
||||||
contextType: typeof(TContext),
|
contextType: typeof(TContext),
|
||||||
repositoryType: repositoryType
|
repositoryType: repositoryType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Use XBotService Middleware
|
/// Register Service ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <param name="repositoryType"></param>
|
||||||
|
/// <param name="lifeTime"></param>
|
||||||
|
public static void AddXBotService(
|
||||||
|
this IServiceCollection services,
|
||||||
|
XBotServiceConfiguration configuration,
|
||||||
|
XRepositoryType repositoryType,
|
||||||
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
AddBotService(
|
||||||
|
contextType: null,
|
||||||
|
services: services,
|
||||||
|
lifeTime: lifeTime,
|
||||||
|
configuration: configuration,
|
||||||
|
repositoryType: repositoryType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register Service with Context ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <param name="repositoryType"></param>
|
||||||
|
/// <param name="lifeTime"></param>
|
||||||
|
/// <typeparam name="TContext"></typeparam>
|
||||||
|
public static void AddXBotService<TContext>(
|
||||||
|
this IServiceCollection services,
|
||||||
|
XBotServiceConfiguration configuration,
|
||||||
|
XRepositoryType repositoryType,
|
||||||
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||||
|
) where TContext : XDbContext
|
||||||
|
{
|
||||||
|
//
|
||||||
|
AddBotService(
|
||||||
|
services: services,
|
||||||
|
lifeTime: lifeTime,
|
||||||
|
configuration: configuration,
|
||||||
|
contextType: typeof(TContext),
|
||||||
|
repositoryType: repositoryType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Use XBotService Middleware ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="app"></param>
|
||||||
|
/// <param name="isDevelopmentEnvironment"></param>
|
||||||
public static void UseXBotService(
|
public static void UseXBotService(
|
||||||
this IApplicationBuilder app,
|
this IApplicationBuilder app,
|
||||||
bool isDevelopmentEnvironment = false
|
bool isDevelopmentEnvironment = false
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
// Using XBot Repository Descriptor
|
//
|
||||||
if (!descriptor.IsNull())
|
#region Using XTag Hubs ...
|
||||||
|
//
|
||||||
|
var pushHelper = new XPushServiceHelper();
|
||||||
|
|
||||||
|
//
|
||||||
|
pushHelper.AddHub<XBotUserDtoHub>(XBotServiceConstants.XBotUserDtoHub);
|
||||||
|
pushHelper.AddHub<XBotUserEntityHub>(XBotServiceConstants.XBotUserEntityHub);
|
||||||
|
|
||||||
|
//
|
||||||
|
app.UseXPushService(pushHelper);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
// Using XBotUser Repository Descriptor ...
|
||||||
|
if (!botUserDescriptor.IsNull())
|
||||||
{
|
{
|
||||||
|
//
|
||||||
app.UseXRepository(
|
app.UseXRepository(
|
||||||
descriptor: descriptor,
|
descriptor: botUserDescriptor,
|
||||||
isDevelopmentEnvironment: isDevelopmentEnvironment
|
isDevelopmentEnvironment: isDevelopmentEnvironment
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -72,7 +232,7 @@ namespace xBotService.DI
|
|||||||
|
|
||||||
//
|
//
|
||||||
#region Private ...
|
#region Private ...
|
||||||
private static XRepositoryDescriptor descriptor = null;
|
private static XRepositoryDescriptor botUserDescriptor = null;
|
||||||
private static string XLogTag = XBotServiceConstants.XBotServiceDILogTag;
|
private static string XLogTag = XBotServiceConstants.XBotServiceDILogTag;
|
||||||
|
|
||||||
private static void Log(string message)
|
private static void Log(string message)
|
||||||
@@ -82,6 +242,7 @@ namespace xBotService.DI
|
|||||||
|
|
||||||
private static void AddBotService(
|
private static void AddBotService(
|
||||||
IServiceCollection services,
|
IServiceCollection services,
|
||||||
|
XBotServiceConfiguration configuration,
|
||||||
XRepositoryType repositoryType,
|
XRepositoryType repositoryType,
|
||||||
Type contextType = null,
|
Type contextType = null,
|
||||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||||
@@ -109,6 +270,10 @@ namespace xBotService.DI
|
|||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Configuration ...
|
||||||
|
services.AddXBotServiceConfiguration(configuration);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Validate Repository Type
|
// Validate Repository Type
|
||||||
var isRepositoryTypeValid =
|
var isRepositoryTypeValid =
|
||||||
@@ -116,43 +281,67 @@ namespace xBotService.DI
|
|||||||
((repositoryType == XRepositoryType.Mongo || repositoryType == XRepositoryType.InMemory) && contextType.IsNull());
|
((repositoryType == XRepositoryType.Mongo || repositoryType == XRepositoryType.InMemory) && contextType.IsNull());
|
||||||
if (!isRepositoryTypeValid)
|
if (!isRepositoryTypeValid)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
Log("Service Registration failed, Invalid Repository Type and Context Type ...");
|
Log("Service Registration failed, Invalid Repository Type and Context Type ...");
|
||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Make Instance of Repository Descriptor
|
// Make Instance of Repository Descriptor ...
|
||||||
switch (repositoryType)
|
switch (repositoryType)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
case XRepositoryType.EF:
|
case XRepositoryType.EF:
|
||||||
descriptor = typeof(XBotServiceHelper)
|
botUserDescriptor = typeof(XBotServiceHelper)
|
||||||
.InvokeGenericMethod<XRepositoryDescriptor>(
|
.InvokeGenericMethod<XRepositoryDescriptor>(
|
||||||
args: null,
|
args: null,
|
||||||
runtimeType: contextType,
|
runtimeType: contextType,
|
||||||
methodName: XBotServiceConstants.GetXBotUserEFRepositoryDescriptor
|
methodName: XBotServiceConstants.GetXBotUserEFRepositoryDescriptor
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
case XRepositoryType.Mongo:
|
case XRepositoryType.Mongo:
|
||||||
// descriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor();
|
botUserDescriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor();
|
||||||
Log("MongoDB support not implemented yet for XBotService.");
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
case XRepositoryType.InMemory:
|
case XRepositoryType.InMemory:
|
||||||
// descriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor();
|
botUserDescriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor();
|
||||||
Log("InMemory support not implemented yet for XBotService.");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Register XBotUser Repository Descriptor
|
// Register XBotUser Repository Descriptor ...
|
||||||
if (!descriptor.IsNull())
|
services.AddXRepository(
|
||||||
{
|
lifeTime: lifeTime,
|
||||||
services.AddXRepository(lifeTime: lifeTime, descriptor: descriptor);
|
descriptor: botUserDescriptor
|
||||||
}
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Register Dto Services
|
// Register Dto Services ...
|
||||||
services.Add(new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime));
|
services.Add(
|
||||||
Log("XBotService Registration Succeed ...");
|
new ServiceDescriptor(typeof(IXBotUserServiceProvider), typeof(XBotUserServiceProvider), lifeTime)
|
||||||
|
);
|
||||||
|
services.Add(
|
||||||
|
new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime)
|
||||||
|
);
|
||||||
|
services.Add(
|
||||||
|
new ServiceDescriptor(typeof(IXBotUserRepositoryProvider), typeof(XBotUserRepositoryProvider), lifeTime)
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Bot Provider Registration ...
|
||||||
|
services.Add(
|
||||||
|
new ServiceDescriptor(typeof(IXBotProvider), typeof(XBotProvider), lifeTime)
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Using Bot Update Reciever ...
|
||||||
|
services.AddXBackgroundService<XBotBotUpdateRecieverBackgroundService>();
|
||||||
|
|
||||||
|
//
|
||||||
|
Log("Service Registration Succeed ...");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,198 @@
|
|||||||
|
using xBotService.Models;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xBotService.Models.Dtos;
|
||||||
|
using xBotService.Configuration;
|
||||||
|
using xBotService.Models.Entities;
|
||||||
|
|
||||||
|
namespace xBotService.Extensions
|
||||||
|
{
|
||||||
|
public static class XBotServiceExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Make Sure a Response Message is Valid ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
public static void EnsureBotResponseSucceed<T>(
|
||||||
|
this XBotResponse<T> source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var isValid =
|
||||||
|
!source.IsNull() &&
|
||||||
|
source.Ok;
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate a Message Send Request ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValid(
|
||||||
|
this XBotSendMessageRequest source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNull() &&
|
||||||
|
source.ChatId > 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
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>
|
||||||
|
/// Normalize ot Update Request ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XBotUpdatesRequest Normalize(
|
||||||
|
this XBotUpdatesRequest source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (source.IsNull())
|
||||||
|
{
|
||||||
|
source = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Limit ...
|
||||||
|
if (source.Limit <= 0 || source.Limit > 100)
|
||||||
|
{
|
||||||
|
source.Limit = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (source.Offset < 0)
|
||||||
|
{
|
||||||
|
source.Offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (source.Timeout <= 30)
|
||||||
|
{
|
||||||
|
source.Timeout = 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts To Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XBotSendMessageRequestDto ToDto(
|
||||||
|
this XBotSendMessageRequest source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XBotSendMessageRequestDto result = null;
|
||||||
|
if (source.IsNull())
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result = new();
|
||||||
|
result = source.MapConvert<XBotSendMessageRequestDto, XBotSendMessageRequest>();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using xBotService.Models;
|
||||||
|
using xBotService.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xBotService.Models.Dtos;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
|
||||||
|
namespace xBotService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXBotProvider
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Bot Info ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XBotResUser> GetMe(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Messages ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="requestModel"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IList<XBotUpdateResponse>> GetUpdates(
|
||||||
|
XBotUpdatesRequest requestModel,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending Simple String Message to Bot ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="requestModel"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XBotMessage> SendMessage(
|
||||||
|
XBotSendMessageRequest requestModel,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Show Verification Request Menu ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="botUserId"></param>
|
||||||
|
/// <param name="label"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XBotMessage> ShowVerificationMenu(
|
||||||
|
long botUserId,
|
||||||
|
string label = XBotMessageIds.XBotRequestVerificationCodeLabel,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send Verification Code to Requests ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="botUserId"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XBotMessage> SendVerificationCode(
|
||||||
|
long botUserId,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify User Subscription ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XBotMessage> VerifyUser(
|
||||||
|
string token,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using xModels.Base;
|
using xModels.Base;
|
||||||
using xModels.Dtos;
|
using xModels.Dtos;
|
||||||
|
|
||||||
@@ -20,10 +21,51 @@ namespace xBotService.Models.Dtos
|
|||||||
/// <value></value>
|
/// <value></value>
|
||||||
public virtual XPersonDto User { get; set; } = null;
|
public virtual XPersonDto User { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Last Verification Code Sent ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public DateTime? LastVerificationSent { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verification Menu Shown or not ...
|
||||||
|
/// </summary>
|
||||||
|
public bool ShownVirificationMenu { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verification Code ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public string VerificationCode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verified On ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public DateTime? VerifiedOn { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot User Id ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public long BotUserId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Chat Bot User Name ...
|
/// Chat Bot User Name ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value></value>
|
/// <value></value>
|
||||||
public string BotUserName { get; set; }
|
public string BotUserName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot Last Name ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public string BotLastname { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot First Name ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public string BotFirstname { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using xModels.Base;
|
using xModels.Base;
|
||||||
|
|
||||||
namespace xBotService.Models.Entities
|
namespace xBotService.Models.Entities
|
||||||
@@ -13,10 +14,51 @@ namespace xBotService.Models.Entities
|
|||||||
/// <value></value>
|
/// <value></value>
|
||||||
public string UserId { get; set; }
|
public string UserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Last Verification Code Sent ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public DateTime? LastVerificationSent { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verification Menu Shown or not ...
|
||||||
|
/// </summary>
|
||||||
|
public bool ShownVirificationMenu { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verification Code ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public string VerificationCode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verified On ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public DateTime? VerifiedOn { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot User Id ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public long BotUserId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Chat Bot User Name ...
|
/// Chat Bot User Name ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value></value>
|
/// <value></value>
|
||||||
public string BotUserName { get; set; }
|
public string BotUserName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot Last Name ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public string BotLastname { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot First Name ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public string BotFirstname { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,511 @@
|
|||||||
|
using System;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace xBotService.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represent XBot Response User ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotResUser
|
||||||
|
{
|
||||||
|
[JsonProperty("id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long Id { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("is_bot",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public bool IsBot { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("last_name",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string LastName { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("username",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Username { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("first_name",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("language_code",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string LanguageCode { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("has_main_web_app",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string HasMainWebApp { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("has_topics_enabled",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public bool HasTopicsEnabled { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("allows_users_to_create_topics",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public bool AllowsUsersToCreateTopics { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Base Bot Response ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotResponse : XBotResponse<object>
|
||||||
|
{ }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Base Bot Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
public class XBotResponse<T>
|
||||||
|
{
|
||||||
|
public bool Ok { get; set; }
|
||||||
|
public T Result { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot Update Request ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotUpdatesRequest
|
||||||
|
{
|
||||||
|
[JsonProperty("limit")]
|
||||||
|
public int Limit { get; set; } = 100;
|
||||||
|
|
||||||
|
[JsonProperty("offset")]
|
||||||
|
public long Offset { get; set; } = 0;
|
||||||
|
|
||||||
|
[JsonProperty("timeout")]
|
||||||
|
public int Timeout { get; set; } = 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represent an Event in Bot ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotUpdateResponse
|
||||||
|
{
|
||||||
|
[JsonProperty("update_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long UpdateId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("message",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotMessage Message { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("edited_message",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotMessage EditedMessage { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("callback_query",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotCallbackQuery CallbackQuery { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("pre_checkout_query",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object PreCheckoutQuery { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = string.Empty;
|
||||||
|
if (this.IsNull())
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result = $"{nameof(XBotUpdateResponse)}:{Environment.NewLine}";
|
||||||
|
result += $"{nameof(UpdateId)}: {UpdateId}{Environment.NewLine}";
|
||||||
|
result += $"{nameof(Message)}: {Message}{Environment.NewLine}";
|
||||||
|
result += $"{nameof(EditedMessage)}: {EditedMessage}{Environment.NewLine}";
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represent a Bot MEssage ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotMessage
|
||||||
|
{
|
||||||
|
[JsonProperty("message_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long MessageId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("message_thread_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long MessageThreadId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("from",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotResUser From { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("date",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long Date { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("chat",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Chat { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("sender_chat",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object SenderChat { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("forward_from",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotResUser ForwardFrom { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("forward_from_chat",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object ForwardFromChat { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("forward_from_message_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long ForwardFromMessageId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("forward_date",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long ForwardDate { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("reply_to_message",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotMessage ReplyToMessage { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("edit_date",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long EditDate { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("media_group_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string MediaGroupId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("text",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Text { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("entities",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public IList<object> Entities { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("animation",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Animation { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("audio",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Audio { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("document",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Document { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("photo",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public IList<object> Photo { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("sticker",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Sticker { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("video",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Video { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("voice",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Voice { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("caption",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Caption { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("caption_entities",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public IList<object> CaptionEntities { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("contact",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Contact { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("location",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Location { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("new_chat_members",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public IList<XBotResUser> NewChatMembers { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("left_chat_member",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotResUser LeftChatMember { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("invoice",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object Invoice { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("successful_payment",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object SuccessfulPayment { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("web_app_data",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object WebAppData { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("reply_markup",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object ReplyMarkup { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("forum_topic_created",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object ForumTopicCreated { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (this.IsNull())
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var from = !From.IsNull()
|
||||||
|
? nameof(From) + ":" + From.Id + "," + From.Username + From.FirstName + From.LastName
|
||||||
|
: string.Empty;
|
||||||
|
var result = $"""
|
||||||
|
{nameof(MessageId)}: {MessageId}, {Environment.NewLine}
|
||||||
|
{nameof(Text)}: {Text}, {Environment.NewLine}
|
||||||
|
""";
|
||||||
|
if (!from.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
result += from + Environment.NewLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// a Callbacked From a Inline Keyboard ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotCallbackQuery
|
||||||
|
{
|
||||||
|
[JsonProperty("id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("data",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Data { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("from",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotResUser From { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("message",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public XBotMessage Message { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot Message Send Request ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotSendMessageRequest
|
||||||
|
{
|
||||||
|
public long ChatId { get; set; }
|
||||||
|
public string Text { get; set; }
|
||||||
|
public object ReplyMarkup { get; set; }
|
||||||
|
public long MessageThreadId { get; set; }
|
||||||
|
public long ReplyToMessageId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bot Message Send Request ...
|
||||||
|
/// </summary>
|
||||||
|
public class XBotSendMessageRequestDto
|
||||||
|
{
|
||||||
|
[JsonProperty("chat_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long ChatId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("text",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Text { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("reply_markup",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public object ReplyMarkup { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("message_thread_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public long MessageThreadId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("reply_to_message_id",
|
||||||
|
NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using xPushService.Base;
|
||||||
|
using xDataService.Configuration;
|
||||||
|
using xBotService.Models.Entities;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using xBotService.Interfaces.Entities;
|
||||||
|
|
||||||
|
namespace xBotService.Providers.Entities
|
||||||
|
{
|
||||||
|
public class XBotUserRepositoryProvider : XBaseEntityProvider<XBotUser, int, XBotUserEntityHub>, IXBotUserRepositoryProvider
|
||||||
|
{
|
||||||
|
public XBotUserRepositoryProvider(
|
||||||
|
IHubContext<XBotUserEntityHub> hub,
|
||||||
|
IXBotUserRepository repository,
|
||||||
|
XDataServiceConfiguration dataConfiguration,
|
||||||
|
IXIdentityProvider identityProvider = null
|
||||||
|
) : base(
|
||||||
|
hub,
|
||||||
|
repository,
|
||||||
|
dataConfiguration,
|
||||||
|
identityProvider
|
||||||
|
)
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,14 @@ namespace xBotService.Providers.GraphQL
|
|||||||
public XBotUserGraphType() : base()
|
public XBotUserGraphType() : base()
|
||||||
{
|
{
|
||||||
Field(x => x.UserId);
|
Field(x => x.UserId);
|
||||||
|
Field(x => x.BotUserId);
|
||||||
|
Field(x => x.VerifiedOn);
|
||||||
Field(x => x.BotUserName);
|
Field(x => x.BotUserName);
|
||||||
|
Field(x => x.BotLastname);
|
||||||
|
Field(x => x.BotFirstname);
|
||||||
|
Field(x => x.VerificationCode);
|
||||||
|
Field(x => x.LastVerificationSent);
|
||||||
|
Field(x => x.ShownVirificationMenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,512 @@
|
|||||||
|
using RestSharp;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Providers;
|
||||||
|
using xBotService.Models;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xBotService.Constants;
|
||||||
|
using xBotService.Extensions;
|
||||||
|
using xBotService.Interfaces;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xHttpService.Constants;
|
||||||
|
using xBotService.Configuration;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System;
|
||||||
|
using xBotService.Models.Dtos;
|
||||||
|
using xIdentityService.Security;
|
||||||
|
using xBotService.Interfaces.Dtos;
|
||||||
|
|
||||||
|
namespace xBotService.Providers
|
||||||
|
{
|
||||||
|
public class XBotProvider : IXBotProvider
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private readonly ILogger<XBotProvider> logger;
|
||||||
|
private readonly IXSecurityProvider securityProvider;
|
||||||
|
private readonly IXIdentityHttpProvider httpProvider;
|
||||||
|
private readonly XValidationProvider validationProvider;
|
||||||
|
private readonly XBotServiceConfiguration configuration;
|
||||||
|
private readonly IXBotUserServiceProvider botUserServiceProvider;
|
||||||
|
|
||||||
|
public XBotProvider(
|
||||||
|
ILogger<XBotProvider> logger,
|
||||||
|
IXSecurityProvider securityProvider,
|
||||||
|
IXIdentityHttpProvider httpProvider,
|
||||||
|
XValidationProvider validationProvider,
|
||||||
|
XBotServiceConfiguration configuration,
|
||||||
|
IXBotUserServiceProvider botUserServiceProvider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.logger = logger;
|
||||||
|
this.httpProvider = httpProvider;
|
||||||
|
this.configuration = configuration;
|
||||||
|
this.securityProvider = securityProvider;
|
||||||
|
this.validationProvider = validationProvider;
|
||||||
|
this.botUserServiceProvider = botUserServiceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Tools ...
|
||||||
|
/// <summary>
|
||||||
|
/// Get an Instance of RestClient ...
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public RestClient GetRestClient(
|
||||||
|
XTokenResponse tokens = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = httpProvider.GetRestClient(
|
||||||
|
tokens: tokens,
|
||||||
|
baseUrl: configuration.BaseRoute
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
result.Timeout = configuration.DefaultClientTimeout;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get an Instance of RestRequest ...
|
||||||
|
/// </summary>
|
||||||
|
public RestRequest GetRestRequest(
|
||||||
|
XBotEndpoints endpoint,
|
||||||
|
bool addXPoweredValue = true,
|
||||||
|
IDictionary<string, string> @params = null,
|
||||||
|
IDictionary<string, string> headers = null,
|
||||||
|
IDictionary<string, string> queryStrings = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = httpProvider.GetRestRequest(
|
||||||
|
@params: @params,
|
||||||
|
headers: headers,
|
||||||
|
queryStrings: queryStrings,
|
||||||
|
addXPoweredValue: addXPoweredValue,
|
||||||
|
endpoint: endpoint.GetStringValue()
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Service ...
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Bot Info ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<XBotResUser> GetMe(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var client = GetRestClient();
|
||||||
|
var request = GetRestRequest(
|
||||||
|
endpoint: XBotEndpoints.GetMe,
|
||||||
|
@params: new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ XBotParams.XTokenParam, configuration.Token }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var response = await httpProvider.RunRestRequest<XBotResponse<XBotResUser>>(
|
||||||
|
client: client,
|
||||||
|
request: request,
|
||||||
|
method: XHttpMethod.GET,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Ensure Success ...
|
||||||
|
response.EnsureBotResponseSucceed();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = response.Result;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Messages ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="requestModel"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<IList<XBotUpdateResponse>> GetUpdates(
|
||||||
|
XBotUpdatesRequest requestModel,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
requestModel = requestModel.Normalize();
|
||||||
|
|
||||||
|
//
|
||||||
|
var client = GetRestClient();
|
||||||
|
var request = GetRestRequest(
|
||||||
|
endpoint: XBotEndpoints.GetUpdates,
|
||||||
|
@params: new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ XBotParams.XTokenParam, configuration.Token }
|
||||||
|
},
|
||||||
|
queryStrings: new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ XBotParams.XLimit, requestModel.Limit.ToString() },
|
||||||
|
{ XBotParams.XOffest, requestModel.Offset.ToString() },
|
||||||
|
{ XBotParams.XTimeout, requestModel.Timeout.ToString() }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var response = await httpProvider.RunRestRequest<XBotResponse<IList<XBotUpdateResponse>>>(
|
||||||
|
client: client,
|
||||||
|
request: request,
|
||||||
|
method: XHttpMethod.GET,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Ensure Success ...
|
||||||
|
response.EnsureBotResponseSucceed();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = response.Result;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending Simple String Message to Bot ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="requestModel"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<XBotMessage> SendMessage(
|
||||||
|
XBotSendMessageRequest requestModel,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!requestModel.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var client = GetRestClient();
|
||||||
|
var request = GetRestRequest(
|
||||||
|
endpoint: XBotEndpoints.SendMessage,
|
||||||
|
@params: new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ XBotParams.XTokenParam, configuration.Token }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var req = requestModel.ToDto();
|
||||||
|
request.AddJsonBody(req.ToJSON());
|
||||||
|
|
||||||
|
//
|
||||||
|
var response = await httpProvider.RunRestRequest<XBotResponse<XBotMessage>>(
|
||||||
|
client: client,
|
||||||
|
request: request,
|
||||||
|
method: XHttpMethod.POST,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Ensure Success ...
|
||||||
|
response.EnsureBotResponseSucceed();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = response.Result;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Show Verification Request Menu ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="botUserId"></param>
|
||||||
|
/// <param name="label"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<XBotMessage> ShowVerificationMenu(
|
||||||
|
long botUserId,
|
||||||
|
string label = XBotMessageIds.XBotRequestVerificationCodeLabel,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (botUserId <= 0 || label.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize ...
|
||||||
|
if (label.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
label = XBotMessageIds.XBotRequestVerificationCodeLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Markup for Keyboards ...
|
||||||
|
var markup = XBotReplyMarkup
|
||||||
|
.InlineKeyboardMarkup(new XBotInlineKeyboardMarkup
|
||||||
|
{
|
||||||
|
InlineKeyboard = [
|
||||||
|
[
|
||||||
|
new XBotInlineKeyboardButton{
|
||||||
|
Text = label,
|
||||||
|
CallbackData = XBotMessageIds.XBotRequestVerificationCode
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
var requestModel = new XBotSendMessageRequest
|
||||||
|
{
|
||||||
|
ChatId = botUserId,
|
||||||
|
ReplyMarkup = markup.ReplyMarkup,
|
||||||
|
Text = XBotMessageIds.XBotRequestVerificationCodeMessageText,
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await SendMessage(
|
||||||
|
requestModel: requestModel,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send Verification Code to Requests ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="botUserId"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<XBotMessage> SendVerificationCode(
|
||||||
|
long botUserId,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (botUserId <= 0)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Bot User Exists ...
|
||||||
|
var botUser = await botUserServiceProvider.FindOneAsync(
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
predicate: x => x.BotUserId.Equals(botUserId)
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
botUser.IsNullOrDefault() ||
|
||||||
|
botUser.BotUserId <= 0)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check User not Verified ...
|
||||||
|
if (botUser.VerifiedOn.HasValue)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var verifiedTime = DateTime.UtcNow;
|
||||||
|
var code = CalculateVerificationCode(
|
||||||
|
user: botUser,
|
||||||
|
verifiedTime: verifiedTime
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var message = XBotMessageIds.XBotVerificationCodeMessageText
|
||||||
|
.ApplyTemplates(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ XBotParams.XVerificationTokenParam, code },
|
||||||
|
{ XBotParams.XTTLInMinuteParam, configuration.VerificationCodeTTL.ToString() },
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await SendMessage(
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
requestModel: new XBotSendMessageRequest
|
||||||
|
{
|
||||||
|
Text = message,
|
||||||
|
ChatId = botUser.BotUserId,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (!result.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Update Bot User ...
|
||||||
|
botUser.VerificationCode = code;
|
||||||
|
botUser.LastVerificationSent = verifiedTime;
|
||||||
|
botUser = await botUserServiceProvider.UpdateAsync(
|
||||||
|
item: botUser,
|
||||||
|
id: botUser.Id,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: null,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify User Subscription ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<XBotMessage> VerifyUser(
|
||||||
|
string token,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validation ...
|
||||||
|
if (!token.IsMD5String() ||
|
||||||
|
token.IsNullOrEmpty() ||
|
||||||
|
userInfo.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Detect Verified User ...
|
||||||
|
var botUser = await botUserServiceProvider.FindOneAsync(
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
predicate: x =>
|
||||||
|
x.LastVerificationSent.HasValue &&
|
||||||
|
x.VerificationCode.Equals(token)
|
||||||
|
);
|
||||||
|
if (botUser.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Verification Code is Valid ...
|
||||||
|
var userCode = CalculateVerificationCode(
|
||||||
|
user: botUser,
|
||||||
|
verifiedTime: botUser.LastVerificationSent.Value
|
||||||
|
);
|
||||||
|
if (!token.Equals(userCode))
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Toke Life Time ...
|
||||||
|
var isExpired = botUser.LastVerificationSent.Value
|
||||||
|
.IsExpiredTTL(configuration.VerificationCodeTTL * 60);
|
||||||
|
if (isExpired)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Cleanup User Verification Info ...
|
||||||
|
botUser.VerificationCode = null;
|
||||||
|
botUser.LastVerificationSent = null;
|
||||||
|
botUser.ShownVirificationMenu = false;
|
||||||
|
|
||||||
|
//
|
||||||
|
botUser = await botUserServiceProvider.UpdateAsync(
|
||||||
|
item: botUser,
|
||||||
|
id: botUser.Id,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: null,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
XException.ExpiredDate.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Make User Valid ...
|
||||||
|
botUser.VerificationCode = null;
|
||||||
|
botUser.UserId = userInfo.UserId;
|
||||||
|
botUser.LastVerificationSent = null;
|
||||||
|
botUser.VerifiedOn = DateTime.UtcNow;
|
||||||
|
botUser.ShownVirificationMenu = false;
|
||||||
|
|
||||||
|
//
|
||||||
|
botUser = await botUserServiceProvider.UpdateAsync(
|
||||||
|
item: botUser,
|
||||||
|
id: botUser.Id,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: null,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Send User Verification Message ...
|
||||||
|
var result = await SendMessage(
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
requestModel: new XBotSendMessageRequest
|
||||||
|
{
|
||||||
|
ChatId = botUser.BotUserId,
|
||||||
|
Text = XBotMessageIds.XBotUserVerificationSucceedMessageText
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
/// <summary>
|
||||||
|
/// Calculate Verification Code ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="user"></param>
|
||||||
|
/// <param name="verifiedTime"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private string CalculateVerificationCode(
|
||||||
|
XBotUserDto user,
|
||||||
|
DateTime verifiedTime
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = securityProvider
|
||||||
|
.Encrypt($"{user.BotUserId}{verifiedTime}")
|
||||||
|
.ToMd5String();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,277 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Providers;
|
||||||
|
using xBotService.Models;
|
||||||
|
using xModels.Extensions;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xBotService.Interfaces;
|
||||||
|
using xBotService.Models.Dtos;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xBotService.Interfaces.Dtos;
|
||||||
|
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
|
||||||
|
{
|
||||||
|
public class XBotBotUpdateRecieverBackgroundService : XTimeBasedBackgroundService
|
||||||
|
{
|
||||||
|
//
|
||||||
|
public XBotBotUpdateRecieverBackgroundService(
|
||||||
|
IServiceProvider serviceProvider,
|
||||||
|
ILogger<XBotBotUpdateRecieverBackgroundService> logger,
|
||||||
|
int intervalInMinutes = 5
|
||||||
|
) : base(serviceProvider, logger, intervalInMinutes)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public override async Task DoWorkAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Inject Provider ...
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var botProvider = scope.ServiceProvider.GetRequiredService<IXBotProvider>();
|
||||||
|
var botUserProvider = scope.ServiceProvider.GetRequiredService<IXBotUserServiceProvider>();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Provider Service Registered Successfully ...
|
||||||
|
if (botProvider.IsNull() ||
|
||||||
|
botUserProvider.IsNull())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogInformation("xBotProvider Service not Provided, ignore ...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Try to Retrieve Updates ...
|
||||||
|
IList<XBotUpdateResponse> updates = [];
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
updates = await botProvider.GetUpdates(
|
||||||
|
requestModel: new(),
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError(ex, "Retrieve Updates failed ...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Updates Exists ...
|
||||||
|
if (!updates.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogInformation("there is not any new Updates ...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Log ...
|
||||||
|
logger.LogInformation($"Updates: {Environment.NewLine}");
|
||||||
|
|
||||||
|
//
|
||||||
|
// HandleRecieved Updates ...
|
||||||
|
foreach (var update in updates)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger
|
||||||
|
.LogInformation($"Handle Update: {Environment.NewLine} {update.ToJSON()}");
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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: predicator,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
if (item.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Not Exists ...
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
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
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Exists ...
|
||||||
|
logger.LogInformation("BotUser exists ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Verified ...
|
||||||
|
if (item.IsVerified())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogInformation("User Verified ...");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// TODO: Remove this ...
|
||||||
|
if (item.BotUserName != "saherelm_it_center")
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Callback Commands ...
|
||||||
|
if (!update.CallbackQuery.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
await HandleBotAction(
|
||||||
|
botUser: item,
|
||||||
|
update: update,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Try to Show Verification Menu on User ...
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle Bot Sent Commands ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="update"></param>
|
||||||
|
/// <param name="botUser"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task HandleBotAction(
|
||||||
|
XBotUpdateResponse update,
|
||||||
|
XBotUserDto botUser,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
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 ...
|
||||||
|
await botProvider.SendVerificationCode(
|
||||||
|
botUserId: botUser.BotUserId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user