Skip to content

Commit

Permalink
feat(MediaDownloader.cs): add logic for checking access conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenonEl committed Dec 28, 2024
1 parent 4c6ec75 commit 113e952
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
20 changes: 17 additions & 3 deletions TelegramBot/MediaDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,23 @@ private static async Task UpdateHandler(ITelegramBotClient botClient, Update upd

if (update.Message != null && update.Message.Text != null)
{
CoreDB.AddUser(update.Message.Chat.FirstName!, update.Message.Chat.Id);
await PrivateUpdateHandler.ProcessMessage(botClient, update, cancellationToken, chatId);
bool hasAccess = CoreDB.CheckExistsUser(chatId);

if (!hasAccess)
{
string startParameter = Utils.Utils.ParseStartCommand(update.Message.Text);
if (!string.IsNullOrEmpty(startParameter) && Config.CanUserStartUsingBot(startParameter))
{
CoreDB.AddUser(update.Message.Chat.FirstName!, chatId);
update.Message.Text = "/start";
hasAccess = true;
}
}

if (hasAccess)
{
await PrivateUpdateHandler.ProcessMessage(botClient, update, cancellationToken, chatId);
}
}
else if (update.CallbackQuery != null)
{
Expand All @@ -83,7 +98,6 @@ private static async Task UpdateHandler(ITelegramBotClient botClient, Update upd
if (update.Message != null && update.Message.Text != null && update.Message.Text.Contains('/'))
{
await GroupUpdateHandler.HandleGroupUpdate(update, botClient, cancellationToken);
return;
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions TelegramBot/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ public static bool IsLink(string input)
return Uri.TryCreate(input, UriKind.Absolute, out Uri? uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
}

public static string ParseStartCommand(string message)
{
string[] parts = message.Split(' ', StringSplitOptions.RemoveEmptyEntries);

if (parts.Length > 0 && parts[0] == "/start")
{
if (parts.Length > 1 && Guid.TryParse(parts[1], out Guid link))
{
return link.ToString();
}
else
{
return "";
}
}
else
{
return "";
}
}
}

public class ProgressReportingStream : Stream
Expand Down

0 comments on commit 113e952

Please sign in to comment.