230 lines
6.4 KiB
C#
230 lines
6.4 KiB
C#
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
|
|
}
|
|
} |