Compare commits

13 Commits

Author SHA1 Message Date
8b42fb35b3 Added branch trigger 2023-02-14 14:52:27 -05:00
9faaeef68f Added try catch for URL parsing 2023-02-14 14:48:47 -05:00
e05e4fb5e5 Updated drone yaml 2023-02-14 14:48:38 -05:00
a00cc167c3 Updated Dockerfile 2023-02-14 14:48:16 -05:00
38d0539a5f Fixed method code (It works!) 2023-02-14 13:59:20 -05:00
31733aa673 Refactored classes 2023-02-14 12:29:11 -05:00
46fcc45931 Changed to use Proxitok
Some checks reported errors
continuous-integration/drone/push Build was killed
2023-02-14 12:04:24 -05:00
a6c7f1ef2a Removed port, added proxitok entry
Some checks reported errors
continuous-integration/drone/push Build was killed
continuous-integration/drone Build was killed
2023-02-14 10:16:03 -05:00
ae4c436a1a Updated video download code
Some checks reported errors
continuous-integration/drone/push Build was killed
2023-02-14 00:35:28 -05:00
724e48da04 Added pipeline type 2023-02-14 00:34:54 -05:00
be8f1caf35 Removed trigger
Some checks reported errors
continuous-integration/drone/push Build was killed
2023-02-13 19:19:55 -05:00
27a480d2b0 Preliminary work on tiktok url regex
Some checks reported errors
continuous-integration/drone/push Build was killed
2023-02-13 19:18:49 -05:00
1f15b65bf5 Added Dockerfile 2023-02-13 19:18:34 -05:00
7 changed files with 168 additions and 23 deletions

View File

@ -1,20 +1,28 @@
---
kind: pipeline
name: build-app
type: docker
name: publish-bot
steps:
- name: build-dotnet
image: mcr.microsoft.com/dotnet/sdk:6.0
commands:
- dotnet build
- dotnet publish
steps:
- name: build-docker
image: docker:dind
volumes:
- name: dockersock
path: /var/run/docker.sock
- name: build-image
image: plugins/docker
settings:
registry: git.kizaing.ca
username:
from_secret: DOCKER_USER
password:
from_secret: DOCKER_PASS
repo: git.kizaing.ca/kizaing/TeleTok
tags: latest
platform: linux/amd64,linux/arm64
# Commented out until stuff actually works
trigger:
branch:
- main

7
Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine3
WORKDIR /app/teletok
COPY bin/Debug/net6.0/publish/* /app/teletok/
CMD [ "TeleTok" ]

View File

@ -1,25 +1,26 @@
using Microsoft.Extensions.Configuration;
using System;
using Telegram.Bot;
namespace TeleTok
{
class TeleTok
public class TeleTok
{
static async Task Main(string[] args)
{
var config = new ConfigurationBuilder()
public static IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", true)
.Build();
var token = config.GetSection("TeleTokConf:token").Value;
public static string token = config.GetSection("TeleTokConf:token").Value;
public static string ptInstance = config.GetSection("TeleTokConf:proxitokInstance").Value;
var botClient = new TelegramBotClient(token);
static async Task Main(string[] args)
{
var me = await botClient.GetMeAsync();
TelegramListener listener = new TelegramListener();
Console.WriteLine("Now listening...");
listener.RunListener();
Console.WriteLine($"Hello world! I am user {me.Id} and my name is {me.FirstName}.");
}
}
}

View File

@ -10,7 +10,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Telegram.Bot" Version="18.0.0" />
<PackageReference Include="Telegram.Bot" Version="17.0.0" />
<PackageReference Include="Telegram.Bot.Extensions.Polling" Version="1.0.2" />
</ItemGroup>
</Project>

91
TelegramListener.cs Normal file
View File

@ -0,0 +1,91 @@
using System;
using System.Configuration;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace TeleTok
{
public class TelegramListener
{
public void 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<UpdateType>() // receive all update types
};
botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
errorHandler: HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cts.Token
);
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)
{
try
{
if(messageText.Contains("tiktok.com"))
{
proxyUrl = VidDownload.TikTokURL(messageText);
Message ttVideo = await botClient.SendVideoAsync(
chatId: chatId,
video: proxyUrl,
cancellationToken: cancellationToken
);
}
}
catch
{
Console.WriteLine("Valid TikTok URI was sent, but was not a video!");
}
}
}
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;
}
}
}

37
VidDownload.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using System.Diagnostics;
using System.Net;
namespace TeleTok
{
public class VidDownload
{
// Takes the scraped TikTok URL and appends it to the proxy downloader link then returns it
public static string TikTokURL(string videourl)
{
string url = videourl;
string proxyUrl;
if(url.Contains("vm.tiktok.com"))
{
url = UnshortenUrl(url);
}
proxyUrl = TeleTok.ptInstance + "/download?url=" + url;
Console.WriteLine("Video for " + url + " has been sent..");
return proxyUrl;
}
// Runs the URL through a web request then returns the full url
static string UnshortenUrl(string videourl)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(videourl);
req.AllowAutoRedirect = false;
var resp = req.GetResponse();
string realUrl = resp.Headers["Location"];
return realUrl;
}
}
}

View File

@ -1,6 +1,6 @@
{
"TeleTokConf": {
"token": "INSERT TOKEN HERE",
"port": 5000
"proxitokInstance": "PROXITOK INSTANCE URL"
}
}