diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/NewMessageHandler.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/NewMessageHandler.cs index 6547406..30b2490 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/NewMessageHandler.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/NewMessageHandler.cs @@ -38,7 +38,8 @@ public sealed class NewMessageHandler : INewMessageHandler m.From?.Peer(_telegramRegistry.Users, _telegramRegistry.Chats), m.Peer.Peer(_telegramRegistry.Users, _telegramRegistry.Chats), m.message); - var chatUserName = m.peer_id.Peer(_telegramRegistry.Users, _telegramRegistry.Chats).Split("@").Last(); + var chatUserName = m.peer_id.Peer(_telegramRegistry.Users, _telegramRegistry.Chats)?.Split("@").Last(); + if (string.IsNullOrWhiteSpace(chatUserName)) { _logger.LogWarning("Failed to get chat user name for chat {ChatId}", m.peer_id.ID); diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramObjectExtensions.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramObjectExtensions.cs index 659a0c1..7898152 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramObjectExtensions.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramObjectExtensions.cs @@ -6,11 +6,15 @@ public static class TelegramObjectExtensions { public static string User(this IDictionary dictionary, long id) => dictionary.TryGetValue(id, out var user) ? user.ToString() : $"User {id}"; - + public static string Chat(this IDictionary dictionary, long id) => - dictionary.TryGetValue(id, out var chat) ? chat.ToString() : $"Chat {id}"; + dictionary.TryGetValue(id, out var chat) ? chat.ToString() ?? $"Null chat {id}" : $"Chat {id}"; - public static string Peer(this Peer peer, IDictionary users, IDictionary chats) => peer is null ? null - : peer is PeerUser user ? users.User(user.user_id) - : peer is PeerChat or PeerChannel ? chats.Chat(peer.ID) : $"Peer {peer.ID}"; + public static string? Peer(this Peer? peer, IDictionary users, IDictionary chats) => peer switch + { + null => null, + PeerUser user => users.User(user.user_id), + PeerChat or PeerChannel => chats.Chat(peer.ID), + _ => $"Peer {peer.ID}" + }; } \ No newline at end of file diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs index 1d6d3f5..dc01030 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs @@ -6,7 +6,7 @@ namespace Nocr.TelegramListener.AppServices.UpdateListeners; public sealed class TelegramRegistry { - public User My { get; private set; } + public User? My { get; private set; } public ConcurrentDictionary Users = new(); public ConcurrentDictionary Chats = new(); diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs index dc01f2d..066f29a 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs @@ -57,9 +57,9 @@ public sealed class UpdateListenerBackgroundService : BackgroundService _telegramRegistry.SetMy(my); _logger.LogInformation("Telegram client is logged-in as {Username} (id {Id})", - _telegramRegistry.My.username ?? - _telegramRegistry.My.first_name + " " + _telegramRegistry.My.last_name, - _telegramRegistry.My.id); + _telegramRegistry.My?.username ?? + _telegramRegistry.My?.first_name + " " + _telegramRegistry.My?.last_name, + _telegramRegistry.My?.id); } await _telegramRegistry.Update(client); diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/WTelegramClientOptions.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/WTelegramClientOptions.cs index dad08a5..5f72a35 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/WTelegramClientOptions.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/WTelegramClientOptions.cs @@ -5,11 +5,11 @@ namespace Nocr.TelegramListener.AppServices.UpdateListeners; /// public sealed class WTelegramClientOptions { - public string ApiId { get; set; } + public required string ApiId { get; set; } - public string ApiHash { get; set; } + public required string ApiHash { get; set; } - public string PhoneNumber { get; set; } + public required string PhoneNumber { get; set; } public string? FirstName { get; set; } diff --git a/src/Nocr.TelegramListener.Core/Options/RebusRabbitMqOptions.cs b/src/Nocr.TelegramListener.Core/Options/RebusRabbitMqOptions.cs index 973d15a..7878722 100644 --- a/src/Nocr.TelegramListener.Core/Options/RebusRabbitMqOptions.cs +++ b/src/Nocr.TelegramListener.Core/Options/RebusRabbitMqOptions.cs @@ -2,11 +2,11 @@ namespace Nocr.TelegramListener.Core.Options; public sealed class RebusRabbitMqOptions { - public string ConnectionString { get; set; } + public required string ConnectionString { get; set; } - public string InputQueueName { get; set; } + public required string InputQueueName { get; set; } - public string DirectExchangeName { get; set; } + public required string DirectExchangeName { get; set; } - public string TopicsExchangeName { get; set; } + public required string TopicsExchangeName { get; set; } } \ No newline at end of file