Skip to content

Commit

Permalink
Update Telegram API
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianAllred committed Nov 4, 2024
1 parent 51c50df commit ac8ee3c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
7 changes: 4 additions & 3 deletions Abstract/ReceiverServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ public async Task ReceiveAsync(CancellationToken stoppingToken)
// ToDo: we can inject ReceiverOptions through IOptions container
var receiverOptions = new ReceiverOptions()
{
AllowedUpdates = Array.Empty<UpdateType>(),
ThrowPendingUpdates = true,
AllowedUpdates = [],
};

var me = await _botClient.GetMeAsync(stoppingToken);
await _botClient.DropPendingUpdates(cancellationToken: stoppingToken);

var me = await _botClient.GetMe(stoppingToken);
_logger.LogInformation("Start receiving updates for {BotName}", me.Username ?? "My Awesome Bot");

// Start receiving updates
Expand Down
2 changes: 1 addition & 1 deletion TelegramVideoBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="FFMpegCore" Version="5.1.0" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
<PackageReference Include="GitVersion.MsBuild" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
10 changes: 5 additions & 5 deletions Workers/DownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private async Task StartDownloads()
{
var replyBuilder = new StringBuilder("Failed to find video, are you sure this website/format is supported?\n\n");
replyBuilder.AppendLine("Please check the list of supported sites [here](https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md)\\.");
await client.SendTextMessageAsync(download.ChatId, replyBuilder.ToString(), parseMode: ParseMode.MarkdownV2, replyToMessageId: download.ReplyId);
await client.SendMessage(download.ChatId, replyBuilder.ToString(), parseMode: ParseMode.MarkdownV2, replyParameters: download.ReplyId);
continue;
}

Expand All @@ -92,13 +92,13 @@ private async Task StartDownloads()
var videoFileInfo = new FileInfo(filePath);
if (videoFileInfo.Length > fileSizeLimit * 1000 * 1000)
{
await client.SendTextMessageAsync(download.ChatId, $"Video `{download.VideoUrl}` is larger than 50MB and requires further compression, please wait\\.", parseMode: ParseMode.MarkdownV2, replyToMessageId: download.ReplyId);
await client.SendMessage(download.ChatId, $"Video `{download.VideoUrl}` is larger than 50MB and requires further compression, please wait\\.", parseMode: ParseMode.MarkdownV2, replyParameters: download.ReplyId);
CompressVideo(filePath);
filePath = $"{Path.GetFileNameWithoutExtension(filePath)}.mp4";
}
else if (videoFileInfo.Extension != ".mp4") // This is an "else" because the compression above will set the correct extension
{
await client.SendTextMessageAsync(download.ChatId, $"Video `{download.VideoUrl}` must be transcoded, please wait\\.", parseMode: ParseMode.MarkdownV2, replyToMessageId: download.ReplyId);
await client.SendMessage(download.ChatId, $"Video `{download.VideoUrl}` must be transcoded, please wait\\.", parseMode: ParseMode.MarkdownV2, replyParameters: download.ReplyId);
CompressVideo(filePath);
filePath = $"{Path.GetFileNameWithoutExtension(filePath)}.mp4";
}
Expand All @@ -113,15 +113,15 @@ private async Task StartDownloads()
{
var inputFile = InputFile.FromStream(videoStream);
var analysis = await FFProbe.AnalyseAsync(filePath);
await client.SendVideoAsync(download.ChatId, inputFile, replyToMessageId: download.ReplyId, height: analysis.PrimaryVideoStream!.Height, width: analysis.PrimaryVideoStream!.Width);
await client.SendVideo(download.ChatId, inputFile, replyParameters: download.ReplyId, height: analysis.PrimaryVideoStream!.Height, width: analysis.PrimaryVideoStream!.Width);
System.IO.File.Delete(filePath);
}
}
catch (Exception ex)
{
var replyBuilder = new StringBuilder($"Sorry, something went wrong downloading `{download.VideoUrl}`\\. I can't \\(currently\\!\\) access private videos, so please make sure it's available to the public\\.\n\n");
replyBuilder.AppendLine("If that's not the problem, contact [my creator](tg://user?id=247371329) for more help\\.");
await client.SendTextMessageAsync(download.ChatId, replyBuilder.ToString(), parseMode: ParseMode.MarkdownV2, replyToMessageId: download.ReplyId);
await client.SendMessage(download.ChatId, replyBuilder.ToString(), parseMode: ParseMode.MarkdownV2, replyParameters: download.ReplyId);
logger.LogError(ex, ex.Message);
}
finally
Expand Down
17 changes: 13 additions & 4 deletions Workers/UpdateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class UpdateHandler(EnvironmentConfig config, ILogger<UpdateHandler> logg

if (downloadUrls.Length == 0 || (downloadUrls.Length == 1 && downloadUrls[0].StartsWith("/download")))
{
await client.SendTextMessageAsync(message.Chat.Id, "No URL included in message.", replyToMessageId: message.MessageId, cancellationToken: cancellationToken);
await client.SendMessage(message.Chat.Id, "No URL included in message.", replyParameters: message.MessageId, cancellationToken: cancellationToken);
return;
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public class UpdateHandler(EnvironmentConfig config, ILogger<UpdateHandler> logg
replyBuilder.AppendJoin('\n', queueStatuses.Where(pair => pair.Value == Enums.DownloadQueueStatus.UnknownError).Select(pair => $"`{pair.Key}`"));
}

await client.SendTextMessageAsync(message.Chat.Id, replyBuilder.ToString(), replyToMessageId: message.MessageId, parseMode: ParseMode.MarkdownV2, cancellationToken: cancellationToken);
await client.SendMessage(message.Chat.Id, replyBuilder.ToString(), replyParameters: message.MessageId, parseMode: ParseMode.MarkdownV2, cancellationToken: cancellationToken);
}

private async Task HandleHelp(ITelegramBotClient client, Message message, CancellationToken cancellationToken = new())
Expand All @@ -133,7 +133,7 @@ public class UpdateHandler(EnvironmentConfig config, ILogger<UpdateHandler> logg

try
{
await client.SendTextMessageAsync(message.Chat.Id, replyBuilder.ToString(), parseMode: ParseMode.MarkdownV2, cancellationToken: cancellationToken);
await client.SendMessage(message.Chat.Id, replyBuilder.ToString(), parseMode: ParseMode.MarkdownV2, cancellationToken: cancellationToken);
}
catch (Exception ex)
{
Expand All @@ -144,11 +144,20 @@ public class UpdateHandler(EnvironmentConfig config, ILogger<UpdateHandler> logg

try
{
await client.SendTextMessageAsync(message.Chat.Id, replyBuilder.ToString(), cancellationToken: cancellationToken);
await client.SendMessage(message.Chat.Id, replyBuilder.ToString(), cancellationToken: cancellationToken);
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
}
}

public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken)
{
if (source is HandleErrorSource.HandleUpdateError) throw exception;

logger.LogError(exception, exception.Message);

await Task.CompletedTask;
}
}

0 comments on commit ac8ee3c

Please sign in to comment.