fixed modern style for ensuring props are not null and extracting values.
This commit is contained in:
parent
66910ae6fe
commit
65af3ded91
@ -28,24 +28,12 @@ public abstract class StartHandlerBase
|
|||||||
|
|
||||||
public async Task Handle(Update update, CancellationToken cancellationToken = default)
|
public async Task Handle(Update update, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
if (update.Message?.From is null || update.CallbackQuery?.Message is null)
|
long telegramId = update.Type switch
|
||||||
{
|
{
|
||||||
_logger.LogError("Не удалось выделить пользовательский колбэк.\n{@Update}", update);
|
UpdateType.Message when update.Message?.From is { } from => from.Id,
|
||||||
return;
|
UpdateType.CallbackQuery when update.CallbackQuery?.From is { } from => from.Id,
|
||||||
}
|
_ => throw new ArgumentOutOfRangeException(nameof(update.Type), "Unsupported update type or missing From.")
|
||||||
|
};
|
||||||
long telegramId;
|
|
||||||
switch (update.Type)
|
|
||||||
{
|
|
||||||
case UpdateType.Message:
|
|
||||||
telegramId = update.Message.From.Id;
|
|
||||||
break;
|
|
||||||
case UpdateType.CallbackQuery:
|
|
||||||
telegramId = update.CallbackQuery.From.Id;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(update.Type), "Unsupported update type");
|
|
||||||
}
|
|
||||||
|
|
||||||
var message = new TextMessage(telegramId)
|
var message = new TextMessage(telegramId)
|
||||||
{
|
{
|
||||||
@ -54,16 +42,18 @@ public abstract class StartHandlerBase
|
|||||||
ReplyMarkup = GetKeyboard()
|
ReplyMarkup = GetKeyboard()
|
||||||
};
|
};
|
||||||
|
|
||||||
if (update.Type == UpdateType.CallbackQuery)
|
if (update is { Type: UpdateType.CallbackQuery, CallbackQuery.Message: { } m })
|
||||||
{
|
{
|
||||||
var messageId = update.CallbackQuery.Message.MessageId;
|
var messageId = m.MessageId;
|
||||||
if (update.CallbackQuery.Message.Type == MessageType.Text)
|
|
||||||
|
if (m.Type == MessageType.Text)
|
||||||
{
|
{
|
||||||
await Bot.EditMessageTextAsync(messageId, message, cancellationToken);
|
await Bot.EditMessageTextAsync(messageId, message, cancellationToken);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var chatId = update.CallbackQuery.Message.Chat.Id;
|
var chatId = m.Chat.Id;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Bot.DeleteMessageAsync(chatId, messageId, cancellationToken);
|
await Bot.DeleteMessageAsync(chatId, messageId, cancellationToken);
|
||||||
|
|||||||
@ -28,7 +28,8 @@ public abstract class SubscribeHandlerBase : ViewSubscriptionHandlerBase
|
|||||||
{
|
{
|
||||||
if (update.Message?.Text is null || update.Message?.From is null)
|
if (update.Message?.Text is null || update.Message?.From is null)
|
||||||
{
|
{
|
||||||
_logger.LogError("Не удалось выделить пользовательское сообщение.\n{@Update}", update);
|
_logger.LogError("Cannot parse update message or from/text user unknown. {@Update}", update.Id);
|
||||||
|
_logger.LogDebug(update.ToString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user