using Grpc.Core;
using JOBot.Proto;
using JOBot.TClient.Infrastructure.Exceptions;
using Telegram.Bot;
using Telegram.Bot.Types;
using User = JOBot.Proto.User;
namespace JOBot.TClient.Services;
public class UserService(ITelegramBotClient bot, User.UserClient userClient)
{
///
/// Get user
///
/// Telegram Update object
/// Cancellation Token
/// RPC User Response or null if user not exists
/// If something in server logic went wrong
/// If update.Message is null
public async Task GetUser(Update update, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(update.Message?.From);
GetUserResponse? user;
try
{
user = await userClient.GetUserAsync(new GetUserRequest() //Получаем пользователя
{
UserId = update.Message.From.Id
});
}
catch (RpcException e) //Пользователь не найден?
{
if (e.StatusCode != StatusCode.NotFound)
throw new FallbackException(TextResource.FallbackMessage, update.Message, bot);
return null;
}
return user;
}
}