467 lines
14 KiB
C#
467 lines
14 KiB
C#
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;
|
|
|
|
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
|
|
);
|
|
|
|
//
|
|
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();
|
|
}
|
|
|
|
//
|
|
// 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="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
|
|
}
|
|
} |