TeleTok/Program.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2023-02-13 16:34:22 -05:00
using Microsoft.Extensions.Configuration;
using System;
2023-02-13 19:18:49 -05:00
using System.Web;
using System.Text.RegularExpressions;
2023-02-13 16:34:22 -05:00
using Telegram.Bot;
2023-02-13 19:18:49 -05:00
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
2023-02-13 16:34:22 -05:00
namespace TeleTok
{
2023-02-14 12:04:24 -05:00
public class TeleTok
2023-02-13 16:34:22 -05:00
{
2023-02-14 12:04:24 -05:00
public static IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", true)
.Build();
2023-02-13 16:34:22 -05:00
2023-02-14 12:04:24 -05:00
public static string token = config.GetSection("TeleTokConf:token").Value;
public static string ptInstance = config.GetSection("TeleTokConf:proxitokInstance").Value;
2023-02-13 16:34:22 -05:00
2023-02-14 12:04:24 -05:00
static async Task Main(string[] args)
{
2023-02-13 16:34:22 -05:00
var botClient = new TelegramBotClient(token);
2023-02-13 19:18:49 -05:00
using var cts = new CancellationTokenSource();
2023-02-13 16:34:22 -05:00
2023-02-13 19:18:49 -05:00
// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
ReceiverOptions receiverOptions = new ()
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types
};
// HandleUpdateAsync and HandlePollingErrorAsync no worky Tyler help
botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cts.Token
);
2023-02-13 16:34:22 -05:00
var me = await botClient.GetMeAsync();
2023-02-13 19:18:49 -05:00
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;
2023-02-14 12:04:24 -05:00
string proxyUrl;
2023-02-13 19:18:49 -05:00
2023-02-14 00:35:28 -05:00
// 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
2023-02-13 19:18:49 -05:00
if (isUri)
{
2023-02-14 00:35:28 -05:00
if(messageText.Contains("tiktok.com"))
{
2023-02-14 12:04:24 -05:00
proxyUrl = VidDownload.TikTokURL(messageText);
2023-02-14 00:35:28 -05:00
Message ttVideo = await botClient.SendVideoAsync(
chatId: chatId,
2023-02-14 12:04:24 -05:00
video: proxyUrl,
2023-02-14 00:35:28 -05:00
cancellationToken: cancellationToken
);
}
2023-02-13 19:18:49 -05:00
}
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;
2023-02-13 16:34:22 -05:00
}
}
}