JOBot/JOBot.TClient/Services/UserService.cs
2025-07-25 16:37:42 +03:00

42 lines
1.4 KiB
C#

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)
{
/// <summary>
/// Get user
/// </summary>
/// <param name="update">Telegram Update object</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>RPC User Response or null if user not exists</returns>
/// <exception cref="FallbackException">If something in server logic went wrong</exception>
/// <exception cref="ArgumentNullException">If update.Message is null</exception>
public async Task<GetUserResponse?> 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;
}
}