Compare commits
	
		
			11 Commits
		
	
	
		
			27a480d2b0
			...
			bugfixes/c
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					8b42fb35b3 | ||
| 
						 | 
					9faaeef68f | ||
| 
						 | 
					e05e4fb5e5 | ||
| 
						 | 
					a00cc167c3 | ||
| 
						 | 
					38d0539a5f | ||
| 
						 | 
					31733aa673 | ||
| 
						 | 
					46fcc45931 | ||
| 
						 | 
					a6c7f1ef2a | ||
| 
						 | 
					ae4c436a1a | ||
| 
						 | 
					724e48da04 | ||
| 
						 | 
					be8f1caf35 | 
							
								
								
									
										24
									
								
								.drone.yml
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								.drone.yml
									
									
									
									
									
								
							@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,6 @@ FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine3
 | 
			
		||||
 | 
			
		||||
WORKDIR /app/teletok
 | 
			
		||||
 | 
			
		||||
RUN apk update && apk add --update nodejs nodejs-npm 
 | 
			
		||||
 | 
			
		||||
RUN npm i -g tiktok-scraper
 | 
			
		||||
 | 
			
		||||
COPY bin/Debug/net6.0/* /app/teletok/
 | 
			
		||||
COPY bin/Debug/net6.0/publish/* /app/teletok/
 | 
			
		||||
 | 
			
		||||
CMD [ "TeleTok" ]
 | 
			
		||||
							
								
								
									
										91
									
								
								Program.cs
									
									
									
									
									
								
							
							
						
						
									
										91
									
								
								Program.cs
									
									
									
									
									
								
							@@ -1,93 +1,26 @@
 | 
			
		||||
using Microsoft.Extensions.Configuration;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Web;
 | 
			
		||||
using System.Text.RegularExpressions;
 | 
			
		||||
using Telegram.Bot;
 | 
			
		||||
using Telegram.Bot.Exceptions;
 | 
			
		||||
using Telegram.Bot.Polling;
 | 
			
		||||
using Telegram.Bot.Types;
 | 
			
		||||
using Telegram.Bot.Types.Enums;
 | 
			
		||||
 | 
			
		||||
namespace TeleTok
 | 
			
		||||
{
 | 
			
		||||
    class TeleTok
 | 
			
		||||
    public class TeleTok
 | 
			
		||||
    {
 | 
			
		||||
        public static IConfigurationRoot config = new ConfigurationBuilder()
 | 
			
		||||
            .SetBasePath(Directory.GetCurrentDirectory())
 | 
			
		||||
            .AddJsonFile("config.json", true)
 | 
			
		||||
            .Build();
 | 
			
		||||
 | 
			
		||||
        public static string token = config.GetSection("TeleTokConf:token").Value;
 | 
			
		||||
        public static string ptInstance = config.GetSection("TeleTokConf:proxitokInstance").Value;
 | 
			
		||||
 | 
			
		||||
        static async Task Main(string[] args)
 | 
			
		||||
        {
 | 
			
		||||
            var config = new ConfigurationBuilder()
 | 
			
		||||
                .SetBasePath(Directory.GetCurrentDirectory())
 | 
			
		||||
                .AddJsonFile("config.json", true)
 | 
			
		||||
                .Build();
 | 
			
		||||
            
 | 
			
		||||
            var token = config.GetSection("TeleTokConf:token").Value;
 | 
			
		||||
            TelegramListener listener = new TelegramListener();
 | 
			
		||||
            Console.WriteLine("Now listening...");
 | 
			
		||||
 | 
			
		||||
            var botClient = new TelegramBotClient(token);
 | 
			
		||||
            using var cts = new CancellationTokenSource();
 | 
			
		||||
            listener.RunListener();
 | 
			
		||||
            
 | 
			
		||||
            // 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
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            
 | 
			
		||||
            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;
 | 
			
		||||
 | 
			
		||||
            bool isUri = Uri.IsWellFormedUriString(message.ToString(), UriKind.RelativeOrAbsolute);
 | 
			
		||||
 | 
			
		||||
            if (isUri)
 | 
			
		||||
            {
 | 
			
		||||
                Regex isTikTok = new Regex(@"(?x)(http(s)?:\/\/)?(?:www|m)\.(?:tiktok.com)\/(?:v|embed|trending)(?:\/)?(?:\?shareId=)?(?P<id>[\da-z]+)", RegexOptions.Singleline);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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
									
								
							
							
						
						
									
										91
									
								
								TelegramListener.cs
									
									
									
									
									
										Normal 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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,10 +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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
    "TeleTokConf": {
 | 
			
		||||
        "token": "INSERT TOKEN HERE",
 | 
			
		||||
        "port": 5000
 | 
			
		||||
        "proxitokInstance": "PROXITOK INSTANCE URL"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user