From 31733aa673cb8f7c6a0374f7496dcd0d13211085 Mon Sep 17 00:00:00 2001 From: Chris Plaatjes Date: Tue, 14 Feb 2023 12:29:11 -0500 Subject: [PATCH] Refactored classes --- Program.cs | 81 +------------------------------------- TelegramListener.cs | 95 +++++++++++++++++++++++++++++++++++++++++++++ VidDownload.cs | 1 + 3 files changed, 98 insertions(+), 79 deletions(-) create mode 100644 TelegramListener.cs diff --git a/Program.cs b/Program.cs index 6090410..7959a9f 100644 --- a/Program.cs +++ b/Program.cs @@ -22,86 +22,9 @@ namespace TeleTok static async Task Main(string[] args) { - var botClient = new TelegramBotClient(token); - using var cts = new CancellationTokenSource(); - - // StartReceiving does not block the caller thread. Receiving is done on the ThreadPool. - ReceiverOptions receiverOptions = new () - { - AllowedUpdates = Array.Empty() // receive all update types - }; - - // HandleUpdateAsync and HandlePollingErrorAsync no worky Tyler help - botClient.StartReceiving( - updateHandler: HandleUpdateAsync, - pollingErrorHandler: HandlePollingErrorAsync, - receiverOptions: receiverOptions, - cancellationToken: cts.Token - ); - - var me = await botClient.GetMeAsync(); - - Console.WriteLine($"Start listening for @{me.Username}"); - while (true) - { - // Do nothing until the stuff happens - } - - cts.Cancel(); - } - - async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) - { - // Only process Message updates: https://core.telegram.org/bots/api#message - if (update.Message is not { } message) - return; - // Only process text messages - if (message.Text is not { } messageText) - return; - - var chatId = message.Chat.Id; - - string proxyUrl; - - // Checks if the text contains a valid URL - bool isUri = Uri.IsWellFormedUriString(messageText, UriKind.RelativeOrAbsolute); - - // Passes the url along to the video downloader if it is valid AND a tiktok link - if (isUri) - { - if(messageText.Contains("tiktok.com")) - { - proxyUrl = VidDownload.TikTokURL(messageText); - - Message ttVideo = await botClient.SendVideoAsync( - chatId: chatId, - video: proxyUrl, - cancellationToken: cancellationToken - ); - } - } - - Console.WriteLine($"Received a '{messageText}' message in chat {chatId}."); - - // Echo received message text - Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "You said:\n" + messageText, - cancellationToken: cancellationToken); - } - - Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) - { - var ErrorMessage = exception switch - { - ApiRequestException apiRequestException - => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}", - _ => exception.ToString() - }; - - Console.WriteLine(ErrorMessage); - return Task.CompletedTask; + TelegramListener listener = new TelegramListener(); + } } } \ No newline at end of file diff --git a/TelegramListener.cs b/TelegramListener.cs new file mode 100644 index 0000000..4362731 --- /dev/null +++ b/TelegramListener.cs @@ -0,0 +1,95 @@ +using System; +using System.Configuration; +using Telegram.Bot; +using Telegram.Bot.Exceptions; +using Telegram.Bot.Polling; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; + +namespace TeleTok +{ + public class TelegramListener + { + public RunListener() + { + var botClient = new TelegramBotClient(TeleTok.token); + using var cts = new CancellationTokenSource(); + + // StartReceiving does not block the caller thread. Receiving is done on the ThreadPool. + ReceiverOptions receiverOptions = new () + { + AllowedUpdates = Array.Empty() // receive all update types + }; + + botClient.StartReceiving( + updateHandler: HandleUpdateAsync, + pollingErrorHandler: HandlePollingErrorAsync, + receiverOptions: receiverOptions, + cancellationToken: cts.Token + ); + + var me = await botClient.GetMeAsync(); + + Console.WriteLine($"Start listening for @{me.Username}"); + while (true) + { + // Do nothing until the stuff happens + } + + cts.Cancel(); + } + + async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) + { + // Only process Message updates: https://core.telegram.org/bots/api#message + if (update.Message is not { } message) + return; + // Only process text messages + if (message.Text is not { } messageText) + return; + + var chatId = message.Chat.Id; + + string proxyUrl; + + // Checks if the text contains a valid URL + bool isUri = Uri.IsWellFormedUriString(messageText, UriKind.RelativeOrAbsolute); + + // Passes the url along to the video downloader if it is valid AND a tiktok link + if (isUri) + { + if(messageText.Contains("tiktok.com")) + { + proxyUrl = VidDownload.TikTokURL(messageText); + + Message ttVideo = await botClient.SendVideoAsync( + chatId: chatId, + video: proxyUrl, + cancellationToken: cancellationToken + ); + } + } + + Console.WriteLine($"Received a '{messageText}' message in chat {chatId}."); + + // Echo received message text + Message sentMessage = await botClient.SendTextMessageAsync( + chatId: chatId, + text: "You said:\n" + messageText, + cancellationToken: cancellationToken); + } + + Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) + { + var ErrorMessage = exception switch + { + ApiRequestException apiRequestException + => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}", + _ => exception.ToString() + }; + + Console.WriteLine(ErrorMessage); + return Task.CompletedTask; + } + } +} diff --git a/VidDownload.cs b/VidDownload.cs index b5d5e22..01383d9 100644 --- a/VidDownload.cs +++ b/VidDownload.cs @@ -18,6 +18,7 @@ namespace TeleTok } proxyUrl = TeleTok.ptInstance + "/download?url=" + url; + Console.WriteLine("Video for " + url + " has been sent.."); return proxyUrl; }