refactor uncompleted works exists in Bot Service ...
This commit is contained in:
@@ -2,30 +2,30 @@ using xExceptions.Attributes;
|
||||
|
||||
namespace xBotService.Constants
|
||||
{
|
||||
public enum XBaleBotEndpoints
|
||||
public enum XBotEndpoints
|
||||
{
|
||||
[StringValue("getMe")]
|
||||
[StringValue("/bot{xBotToken}/getMe")]
|
||||
GetMe,
|
||||
|
||||
[StringValue("getChat")]
|
||||
[StringValue("/bot{xBotToken}/getChat")]
|
||||
GetChat,
|
||||
|
||||
[StringValue("sendMessage")]
|
||||
[StringValue("/bot{xBotToken}/sendMessage")]
|
||||
SendMessage,
|
||||
|
||||
[StringValue("getUpdates")]
|
||||
[StringValue("/bot{xBotToken}/getUpdates")]
|
||||
GetUpdates,
|
||||
|
||||
[StringValue("setWebhook")]
|
||||
[StringValue("/bot{xBotToken}/setWebhook")]
|
||||
SetWebhook,
|
||||
|
||||
[StringValue("deleteWebhook")]
|
||||
[StringValue("/bot{xBotToken}/deleteWebhook")]
|
||||
DeleteWebhook,
|
||||
|
||||
[StringValue("answerCallbackQuery")]
|
||||
[StringValue("/bot{xBotToken}/answerCallbackQuery")]
|
||||
AnswerCallbackQuery,
|
||||
|
||||
[StringValue("editMessageReplyMarkup")]
|
||||
[StringValue("/bot{xBotToken}/editMessageReplyMarkup")]
|
||||
EditMessageReplyMarkup,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using xExceptions.Attributes;
|
||||
|
||||
namespace xBotService.Constants
|
||||
{
|
||||
public enum XBotMessageParseMode
|
||||
{
|
||||
[StringValue("HTML")]
|
||||
Html,
|
||||
|
||||
[StringValue("Markdown")]
|
||||
Markdown
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
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}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
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;
|
||||
|
||||
namespace xBotService.Controllers
|
||||
{
|
||||
public abstract class XBotProviderControllerBase : XBaseIdentityApiV1Controller
|
||||
{
|
||||
private readonly IXBotProvider botProvider;
|
||||
|
||||
protected XBotProviderControllerBase(
|
||||
IXBotProvider botProvider,
|
||||
ILogger<XBotProviderControllerBase> logger,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider
|
||||
) : base(
|
||||
logger,
|
||||
appConfiguration,
|
||||
identityProvider,
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
this.botProvider = botProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#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("{chatId}/ShowVerificationMenu")]
|
||||
// public virtual async Task<ActionResult<XBotMessageResponse>> ShowVerificationMenu(
|
||||
// [FromRoute] long chatId,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// try
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// if (!ModelState.IsValid)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
// if (chatId <= 0)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
// //
|
||||
// var result = await botProvider.SendVerificationMenu(
|
||||
// chatId: chatId,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return Ok(result.ToDynamicObject());
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// //
|
||||
// var result = GetExceptionActionResult(ex);
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
|
||||
// [HttpPost("{chatId}/SendVerificationCode")]
|
||||
// public virtual async Task<ActionResult<XBotMessageResponse>> SendVerificationCode(
|
||||
// [FromRoute] long chatId,
|
||||
// [FromQuery] string code,
|
||||
// [FromQuery] int ttlSeconds,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// try
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// if (!ModelState.IsValid)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
// if (chatId <= 0 || code.IsNullOrEmpty() || ttlSeconds < 60)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
// //
|
||||
// var result = await botProvider.SendVerificationCode(
|
||||
// code: code,
|
||||
// chatId: chatId,
|
||||
// ttlSeconds: ttlSeconds,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return Ok(result.ToDynamicObject());
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// //
|
||||
// var result = GetExceptionActionResult(ex);
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
|
||||
// [HttpPost("{id}/AnswerCallback")]
|
||||
// public virtual async Task<ActionResult<XBotMessageResponse>> AnswerCallbackQuery(
|
||||
// [FromRoute] string callbackQueryId,
|
||||
// [FromQuery] string text = null,
|
||||
// [FromQuery] bool showAlert = false,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// try
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// if (!ModelState.IsValid ||
|
||||
// callbackQueryId.IsNullOrEmpty())
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
// //
|
||||
// var result = await botProvider.AnswerCallbackQuery(
|
||||
// text: text,
|
||||
// showAlert: showAlert,
|
||||
// callbackQueryId: callbackQueryId,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return Ok(result.ToDynamicObject());
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// //
|
||||
// var result = GetExceptionActionResult(ex);
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
|
||||
// [HttpPost("{chatId}/RemoveInlineKeyboard")]
|
||||
// public virtual async Task<ActionResult<XBotMessageResponse>> RemoveInlineKeyboard(
|
||||
// [FromRoute] long chatId,
|
||||
// [FromQuery] long messageId,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// try
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// if (!ModelState.IsValid)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
// if (chatId <= 0 || messageId <= 0)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
// //
|
||||
// var result = await botProvider.RemoveInlineKeyboard(
|
||||
// chatId: chatId,
|
||||
// messageId: messageId,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return Ok(result.ToDynamicObject());
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// //
|
||||
// var result = GetExceptionActionResult(ex);
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
|
||||
// [HttpPost("Webhook")]
|
||||
// public virtual async Task<ActionResult<bool>> SetWebhook(
|
||||
// [FromQuery] string webhookUrl,
|
||||
// [FromQuery] string secretToken = null,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// try
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// if (!ModelState.IsValid)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
// await ValidationProvider
|
||||
// .GroupValidationBuilder()
|
||||
// .AddNotEmpty(webhookUrl)
|
||||
// .AddUrl(webhookUrl)
|
||||
// .ValidateGroupAsync();
|
||||
|
||||
// //
|
||||
// var result = await botProvider.SetWebhook(
|
||||
// webhookUrl: webhookUrl,
|
||||
// secretToken: secretToken,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return Ok(result);
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// //
|
||||
// var result = GetExceptionActionResult(ex);
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ using Microsoft.Extensions.Configuration;
|
||||
using xBotService.Interfaces.Entities.Dtos;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ConfigurationNodeNames = xBotService.Constants.ConfigurationNodeNames;
|
||||
using xBotService.Reciever;
|
||||
|
||||
namespace xBotService.DI
|
||||
{
|
||||
@@ -330,10 +331,15 @@ namespace xBotService.DI
|
||||
);
|
||||
|
||||
//
|
||||
// Bot Provider Registration ...
|
||||
services.Add(
|
||||
new ServiceDescriptor(typeof(IXBotProvider), typeof(XBotProvider), lifeTime)
|
||||
);
|
||||
|
||||
//
|
||||
// Using Bot Update Reciever ...
|
||||
services.AddXBackgroundService<XBotBotUpdateRecieverBackgroundService>();
|
||||
|
||||
//
|
||||
Log("Service Registration Succeed ...");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
using xBotService.Models;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
|
||||
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>
|
||||
/// 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.UpdateData(source);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace xBotService.Interfaces
|
||||
{
|
||||
public interface IXBaleBotProvider
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
+104
-3
@@ -1,11 +1,112 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using xBotService.Models;
|
||||
using xBotService.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using xCommons.Extensions;
|
||||
|
||||
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>
|
||||
/// Sending Verification Request Menu ...
|
||||
/// </summary>
|
||||
/// <param name="chatId"></param>
|
||||
/// <param name="label"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// Task<XBotMessageResponse> SendVerificationMenu(
|
||||
// long chatId,
|
||||
// string label = "🔐 دریافت کد وریفیکیشن",
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
|
||||
/// <summary>
|
||||
/// Send Verification Code to Requests ...
|
||||
/// </summary>
|
||||
/// <param name="chatId"></param>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="ttlSeconds"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// Task<XBotMessageResponse> SendVerificationCode(
|
||||
// long chatId,
|
||||
// string code,
|
||||
// int ttlSeconds,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
|
||||
/// <summary>
|
||||
/// an Accept for Exit Loading on Bot ...
|
||||
/// </summary>
|
||||
/// <param name="callbackQueryId"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="showAlert"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// Task<XBotMessageResponse> AnswerCallbackQuery(
|
||||
// string callbackQueryId,
|
||||
// string text = null,
|
||||
// bool showAlert = false,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
|
||||
/// <summary>
|
||||
/// Remove Inline Keyboard After Click (Optional) ...
|
||||
/// </summary>
|
||||
/// <param name="chatId"></param>
|
||||
/// <param name="messageId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// Task<XBotMessageResponse> RemoveInlineKeyboard(
|
||||
// long chatId,
|
||||
// long messageId,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
|
||||
/// <summary>
|
||||
/// Set Webhook for Retrieve Messages on Recieved ...
|
||||
/// </summary>
|
||||
/// <param name="webhookUrl"></param>
|
||||
/// <param name="secretToken"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// Task<bool> SetWebhook(
|
||||
// string webhookUrl,
|
||||
// string secretToken = null,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,34 @@ namespace xBotService.Models.Dtos
|
||||
/// <value></value>
|
||||
public virtual XPersonDto User { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// Check Bot User is Verified or not ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public bool Verified { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Bot User Id ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public long BotUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Chat Bot User Name ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,34 @@ namespace xBotService.Models.Entities
|
||||
/// <value></value>
|
||||
public string UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Check Bot User is Verified or not ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public bool Verified { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Bot User Id ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public long BotUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Chat Bot User Name ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
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,73 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xBotService.Models
|
||||
{
|
||||
public class XBotMessageRequest
|
||||
{
|
||||
public long ChatId { get; set; }
|
||||
public string Text { get; set; }
|
||||
public string ParseMode { get; set; }
|
||||
public XBotReplyMarkup ReplyMarkup { get; set; }
|
||||
}
|
||||
|
||||
public class XBotReplyMarkup
|
||||
{
|
||||
public List<List<XBotInlineButton>> InlineKeyboard { get; set; }
|
||||
}
|
||||
|
||||
public class XBotInlineButton
|
||||
{
|
||||
public string Text { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string CallbackData { get; set; }
|
||||
}
|
||||
|
||||
public class XBotMessageResponse
|
||||
{
|
||||
public bool Ok { get; set; }
|
||||
public string Description { get; set; }
|
||||
public XBotResMessage Result { get; set; }
|
||||
}
|
||||
|
||||
public class XBotResMessage
|
||||
{
|
||||
public long Date { get; set; }
|
||||
public string Text { get; set; }
|
||||
public long MessageId { get; set; }
|
||||
public XBotResUser From { get; set; }
|
||||
public XBotResChat Chat { get; set; }
|
||||
}
|
||||
|
||||
public class XBotResUser
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public bool IsBot { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
}
|
||||
|
||||
public class XBotResChat
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string Type { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
}
|
||||
|
||||
public class XBotUpdate
|
||||
{
|
||||
public long UpdateId { get; set; }
|
||||
public XBotResMessage Message { get; set; }
|
||||
public XBotCallbackQuery CallbackQuery { get; set; }
|
||||
}
|
||||
|
||||
public class XBotCallbackQuery
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Data { get; set; }
|
||||
public XBotResUser From { get; set; }
|
||||
public XBotResMessage Message { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using xCommons.Extensions;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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 User { 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; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -8,7 +8,11 @@ namespace xBotService.Providers.GraphQL
|
||||
public XBotUserGraphType() : base()
|
||||
{
|
||||
Field(x => x.UserId);
|
||||
Field(x => x.Verified);
|
||||
Field(x => x.BotUserId);
|
||||
Field(x => x.BotUserName);
|
||||
Field(x => x.BotLastname);
|
||||
Field(x => x.BotFirstname);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using xBotService.Interfaces;
|
||||
|
||||
namespace xBotService.Providers
|
||||
{
|
||||
public class XBaleBotProvider : IXBaleBotProvider
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
+453
-1
@@ -1,9 +1,461 @@
|
||||
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;
|
||||
|
||||
namespace xBotService.Providers
|
||||
{
|
||||
public class XBotProvider : IXBotProvider
|
||||
{
|
||||
|
||||
//
|
||||
private readonly ILogger<XBotProvider> logger;
|
||||
private readonly IXIdentityHttpProvider httpProvider;
|
||||
private readonly XValidationProvider validationProvider;
|
||||
private readonly XBotServiceConfiguration configuration;
|
||||
|
||||
public XBotProvider(
|
||||
ILogger<XBotProvider> logger,
|
||||
IXIdentityHttpProvider httpProvider,
|
||||
XValidationProvider validationProvider,
|
||||
XBotServiceConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
this.logger = logger;
|
||||
this.httpProvider = httpProvider;
|
||||
this.configuration = configuration;
|
||||
this.validationProvider = validationProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#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
|
||||
);
|
||||
|
||||
//
|
||||
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>
|
||||
/// Sending Verification Request Menu ...
|
||||
/// </summary>
|
||||
/// <param name="chatId"></param>
|
||||
/// <param name="label"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<XBotMessageResponse> SendVerificationMenu(
|
||||
// long chatId,
|
||||
// string label = "🔐 دریافت کد وریفیکیشن",
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validate ...
|
||||
// if (chatId <= 0 || label.IsNullOrEmpty())
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
// //
|
||||
// // Markup for Keyboards ...
|
||||
// var markup = new XBotReplyMarkup
|
||||
// {
|
||||
// //
|
||||
// InlineKeyboard =
|
||||
// [
|
||||
// //
|
||||
// [
|
||||
// //
|
||||
// new XBotInlineButton
|
||||
// {
|
||||
// Text = label,
|
||||
// CallbackData = "request_verification_code"
|
||||
// }
|
||||
// ]
|
||||
// ]
|
||||
// };
|
||||
|
||||
// //
|
||||
// var response = await SendMessage(
|
||||
// chatId: chatId,
|
||||
// text: "برای دریافت کد وریفیکیشن روی دکمه زیر کلیک کنید:",
|
||||
// replyMarkup: markup,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Send Verification Code to Requests ...
|
||||
/// </summary>
|
||||
/// <param name="chatId"></param>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="ttlSeconds"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<XBotMessageResponse> SendVerificationCode(
|
||||
// long chatId,
|
||||
// string code,
|
||||
// int ttlSeconds,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validate ...
|
||||
// if (chatId <= 0 || ttlSeconds < 60 || code.IsNullOrEmpty())
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
// //
|
||||
// var minutes = ttlSeconds / 60;
|
||||
// var response = await SendMessage(
|
||||
// chatId: chatId,
|
||||
// cancellationToken: cancellationToken,
|
||||
// text: $"✅ کد وریفیکیشن شما:\n\n<code>{code}</code>\n\n⏱ این کد تا {minutes} دقیقه معتبر است."
|
||||
// );
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// an Accept for Exit Loading on Bot ...
|
||||
/// </summary>
|
||||
/// <param name="callbackQueryId"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="showAlert"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<XBotMessageResponse> AnswerCallbackQuery(
|
||||
// string callbackQueryId,
|
||||
// string text = null,
|
||||
// bool showAlert = false,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// validationProvider.GroupValidationBuilder()
|
||||
// .AddNotEmpty(callbackQueryId)
|
||||
// .ValidateGroup();
|
||||
|
||||
// //
|
||||
// var client = GetRestClient();
|
||||
// var request = GetRestRequest(
|
||||
// endpoint: XBotEndpoints.AnswerCallbackQuery,
|
||||
// @params: new Dictionary<string, string>
|
||||
// {
|
||||
// { XBotParams.XTokenParam, configuration.Token }
|
||||
// }
|
||||
// );
|
||||
|
||||
// //
|
||||
// request.AddJsonBody(new
|
||||
// {
|
||||
// text = text,
|
||||
// show_alert = showAlert,
|
||||
// callback_query_id = callbackQueryId,
|
||||
// });
|
||||
|
||||
// //
|
||||
// var response = await httpProvider.RunRestRequest<XBotMessageResponse>(
|
||||
// client: client,
|
||||
// request: request,
|
||||
// method: XHttpMethod.POST,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Remove Inline Keyboard After Click (Optional) ...
|
||||
/// </summary>
|
||||
/// <param name="chatId"></param>
|
||||
/// <param name="messageId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<XBotMessageResponse> RemoveInlineKeyboard(
|
||||
// long chatId,
|
||||
// long messageId,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// if (chatId <= 0 || messageId <= 0)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
// //
|
||||
// var client = GetRestClient();
|
||||
// var request = GetRestRequest(
|
||||
// endpoint: XBotEndpoints.EditMessageReplyMarkup,
|
||||
// @params: new Dictionary<string, string>
|
||||
// {
|
||||
// { XBotParams.XTokenParam, configuration.Token }
|
||||
// }
|
||||
// );
|
||||
|
||||
// //
|
||||
// request.AddJsonBody(new
|
||||
// {
|
||||
// chat_id = chatId,
|
||||
// message_id = messageId,
|
||||
// reply_markup = new { inline_keyboard = new List<List<object>>() }
|
||||
// });
|
||||
|
||||
// //
|
||||
// var response = await httpProvider.RunRestRequest<XBotMessageResponse>(
|
||||
// client: client,
|
||||
// request: request,
|
||||
// method: XHttpMethod.POST,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Set Webhook for Retrieve Messages on Recieved ...
|
||||
/// </summary>
|
||||
/// <param name="webhookUrl"></param>
|
||||
/// <param name="secretToken"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<bool> SetWebhook(
|
||||
// string webhookUrl,
|
||||
// string secretToken = null,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// var client = GetRestClient();
|
||||
// var request = GetRestRequest(
|
||||
// endpoint: XBotEndpoints.SetWebhook,
|
||||
// @params: new Dictionary<string, string>
|
||||
// {
|
||||
// { XBotParams.XTokenParam, configuration.Token }
|
||||
// }
|
||||
// );
|
||||
|
||||
// //
|
||||
// request.AddJsonBody(new
|
||||
// {
|
||||
// url = webhookUrl,
|
||||
// secret_token = secretToken
|
||||
// });
|
||||
|
||||
// //
|
||||
// var iResponse = await httpProvider.RunRestRequest<XBotMessageResponse>(
|
||||
// client: client,
|
||||
// request: request,
|
||||
// method: XHttpMethod.POST,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
// var response = iResponse?.Ok ?? false;
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
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;
|
||||
|
||||
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}{updates.ToJSON()}");
|
||||
|
||||
//
|
||||
// HandleRecieved Updates ...
|
||||
foreach (var update in updates)
|
||||
{
|
||||
//
|
||||
logger
|
||||
.LogInformation($"Handle Bot Update: {Environment.NewLine} {update}");
|
||||
|
||||
//
|
||||
// Check Item Exists in Db ...
|
||||
var item = await botUserProvider.FindOneAsync(
|
||||
predicate: x => x.BotUserId.Equals(update.Message.From.Id)
|
||||
);
|
||||
if (item.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
// Not Exists ...
|
||||
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.Verified)
|
||||
{
|
||||
//
|
||||
logger.LogInformation("User Verified ...");
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
// Try to Show Verification Menu on User ...
|
||||
logger
|
||||
.LogInformation($"Show Verification Request Method to User: {item.User.GetFullname()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user