Skip to content

Commit

Permalink
refactor: fix the inaccuracies and improve the logic of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenonEl committed Jan 12, 2025
1 parent 58b0a89 commit 54b0197
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Resources/texts.resx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ An invitation link so that a person can start using by bot:</value>
</data>

<!-- Working with contact selection for distribution -->
<data name="videoDistributionQuestion" xml:space="preserve">
<data name="VideoDistributionQuestion" xml:space="preserve">
<value>Who should we send the video to?</value>
</data>
<data name="SendToAllContactsButtonText" xml:space="preserve">
Expand Down
2 changes: 1 addition & 1 deletion Resources/texts.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</data>

<!-- Работа с выбором контактов для рассылки-->
<data name="videoDistributionQuestion" xml:space="preserve">
<data name="VideoDistributionQuestion" xml:space="preserve">
<value>Кому будем рассылать видео?</value>
</data>
<data name="SendToAllContactsButtonText" xml:space="preserve">
Expand Down
10 changes: 7 additions & 3 deletions TelegramBot/MediaDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ await botClient.EditMessageText(statusMessage.Chat.Id, statusMessage.MessageId,
chatId,
InputFile.FromStream(progressStream, "video.mp4"),
caption: Config.GetResourceString("HereIsYourVideo") + text,
replyParameters: new ReplyParameters { MessageId = statusMessage.MessageId },
parseMode: ParseMode.Html
);

Expand Down Expand Up @@ -251,14 +252,17 @@ await botClient.EditMessageText(statusMessage.Chat.Id, statusMessage.MessageId,
{
await botClient.SendMessage(telegramId,
string.Format(Config.GetResourceString("VideoSentToContacts"),
filteredContactUserTGIds.Count, now.ToString("yyyy_MM_dd_HH_mm_ss"),
MyRegex().Replace(name, "_")), parseMode: ParseMode.Html);
filteredContactUserTGIds.Count, now.ToString("yyyy_MM_dd_HH_mm_ss"),
MyRegex().Replace(name, "_")),
replyParameters: new ReplyParameters { MessageId = statusMessage.MessageId },
parseMode: ParseMode.Html);
}

if (mutedByUserIds.Count > 0)
{
await botClient.SendMessage(telegramId,
string.Format(Config.GetResourceString("MutedByContacts"), mutedByUserIds.Count));
string.Format(Config.GetResourceString("MutedByContacts"), mutedByUserIds.Count),
replyParameters: new ReplyParameters { MessageId = statusMessage.MessageId });
}
}

Expand Down
38 changes: 17 additions & 21 deletions TelegramBot/States/VideoDistributionConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ public string GetCurrentState()
public async Task ProcessState(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
long chatId = Utils.Utils.GetIDfromUpdate(update);
if (!TelegramBot.userStates.TryGetValue(chatId, out IUserState? value))
return;

var userState = (ProcessVideoDC)value;

switch (userState.currentState)
switch (currentState)
{
case UsersGroupState.ProcessAction:
if (update.CallbackQuery != null)
Expand All @@ -46,28 +42,28 @@ public async Task ProcessState(ITelegramBotClient botClient, Update update, Canc
switch (callbackData)
{
case "send_to_all_contacts":
userState.action = "send_to_all_contacts";
await PrepareTargetUserIds(chatId, userState);
action = "send_to_all_contacts";
await PrepareTargetUserIds(chatId);
await botClient.EditMessageText(statusMessage.Chat.Id, statusMessage.MessageId, Config.GetResourceString("ConfirmDecision"), replyMarkup: KeyboardUtils.GetConfirmForActionKeyboardMarkup(), cancellationToken: cancellationToken);
break;
case "send_to_default_groups":
userState.action = "send_to_default_groups";
await PrepareTargetUserIds(chatId, userState);
action = "send_to_default_groups";
await PrepareTargetUserIds(chatId);
break;
case "send_to_specified_groups":
userState.action = "send_to_specified_groups";
action = "send_to_specified_groups";
await botClient.SendMessage(chatId, "Please enter group IDs separated by spaces:", cancellationToken: cancellationToken);
userState.currentState = UsersGroupState.ProcessData;
currentState = UsersGroupState.ProcessData;
break;
case "send_to_specified_users":
userState.action = "send_to_specified_users";
action = "send_to_specified_users";
await botClient.SendMessage(chatId, "Please enter user IDs separated by spaces:", cancellationToken: cancellationToken);
userState.currentState = UsersGroupState.ProcessData;
currentState = UsersGroupState.ProcessData;
break;
case "send_only_to_me":
userState.action = "send_only_to_me";
action = "send_only_to_me";
await botClient.EditMessageText(statusMessage.Chat.Id, statusMessage.MessageId, Config.GetResourceString("ConfirmDecision"), replyMarkup: KeyboardUtils.GetConfirmForActionKeyboardMarkup(), cancellationToken: cancellationToken);
userState.currentState = UsersGroupState.Finish;
currentState = UsersGroupState.Finish;
break;
case "main_menu":
await Utils.Utils.HandleStateBreakCommand(botClient, update, chatId, removeReplyMarkup: false);
Expand All @@ -86,7 +82,7 @@ public async Task ProcessState(ITelegramBotClient botClient, Update update, Canc
if (ids.All(id => long.TryParse(id, out _)))
{
targetUserIds = ids.Select(long.Parse).ToList();
await PrepareTargetUserIds(chatId, userState);
await PrepareTargetUserIds(chatId);
}
else
{
Expand All @@ -98,7 +94,7 @@ public async Task ProcessState(ITelegramBotClient botClient, Update update, Canc
if (long.TryParse(input, out long id))
{
targetUserIds.Add(id);
await PrepareTargetUserIds(chatId, userState);
await PrepareTargetUserIds(chatId);
}
else
{
Expand All @@ -113,7 +109,7 @@ public async Task ProcessState(ITelegramBotClient botClient, Update update, Canc
update.Message != null)
{
await botClient.EditMessageText(statusMessage.Chat.Id, statusMessage.MessageId, Config.GetResourceString("VideoDistributionQuestion"), replyMarkup: KeyboardUtils.GetVideoDistributionKeyboardMarkup(), cancellationToken: cancellationToken);
userState.currentState = UsersGroupState.ProcessAction;
currentState = UsersGroupState.ProcessAction;
return;
}
await botClient.EditMessageText(statusMessage.Chat.Id, statusMessage.MessageId, Config.GetResourceString("WaitDownloadingVideo"), cancellationToken: cancellationToken);
Expand All @@ -123,10 +119,10 @@ public async Task ProcessState(ITelegramBotClient botClient, Update update, Canc
}
}

private async Task PrepareTargetUserIds(long chatId, ProcessVideoDC userState)
private async Task PrepareTargetUserIds(long chatId)
{
int userId = DBforGetters.GetUserIDbyTelegramID(chatId);
switch (userState.action)
switch (action)
{
case "send_to_all_contacts":
List<long> contactUserTGIds = await CoreDB.GetAllContactUserTGIds(userId);
Expand All @@ -141,6 +137,6 @@ private async Task PrepareTargetUserIds(long chatId, ProcessVideoDC userState)
break;
}

userState.currentState = UsersGroupState.Finish;
currentState = UsersGroupState.Finish;
}
}

0 comments on commit 54b0197

Please sign in to comment.