publishhelperbot/PublishHelperBot/Program.fs

42 lines
1.6 KiB
Forth
Raw Normal View History

2023-02-07 19:48:09 +03:00
// For more information see https://aka.ms/fsharp-console-apps
open System
open System.Net.Http
open System.Threading
open System.Threading.Tasks
2023-02-07 21:55:46 +03:00
open PublishHelperBot.Handlers
open PublishHelperBot.Environment
2023-02-11 04:16:34 +03:00
open PublishHelperBot.YoutubeDl
2023-02-07 19:48:09 +03:00
open Telegram.Bot
open Telegram.Bot.Polling
open Telegram.Bot.Types
open Telegram.Bot.Types.Enums
2023-02-07 21:55:46 +03:00
let CreateBot (config: BotConfig, http: HttpClient) = TelegramBotClient(config.token, http)
2023-02-07 19:48:09 +03:00
let config = CreateConfig <| "SBPB_CONFIG_PATH";
let botClient = CreateBot <| (config, new HttpClient())
2023-02-11 04:16:34 +03:00
let YtService = YoutubeDlBackgroundService <|
(new HttpClient(), config.YoutubeDlUrl, botClient, config.chanelId, CancellationToken.None)
2023-02-07 22:15:46 +03:00
let startDate = DateTime.UtcNow
let isObsoleteUpdate (u: Update) = u.Type = UpdateType.Message && u.Message.Date < startDate;
2023-02-07 19:48:09 +03:00
let updateHandle (bc: ITelegramBotClient) (u: Update) (ct: CancellationToken): Task =
2023-02-11 04:16:34 +03:00
let tgCtx = (u, config, bc)
2023-02-07 19:48:09 +03:00
match u with
| _ when isObsoleteUpdate u -> Task.CompletedTask
2023-02-11 04:16:34 +03:00
| _ when RelayMatch <| (u, config) -> RelayHandler <| tgCtx
| _ when YoutubeRepostMatch <| (u, config) -> YoutubeRepostHandler <| (YtService, tgCtx)
2023-02-07 19:48:09 +03:00
| _ -> Task.CompletedTask
let handlePollingError (bc: ITelegramBotClient) (e: Exception) (t: CancellationToken) =
printfn $"{e.Message}\n{e.StackTrace}"
Task.CompletedTask
let receiverOptions = ReceiverOptions(AllowedUpdates = Array.zeroCreate<UpdateType> 0)
botClient.StartReceiving(updateHandle,handlePollingError,receiverOptions)
2023-02-11 04:16:34 +03:00
YtService.StartYoutubeDlService()
2023-02-07 19:48:09 +03:00
2023-02-07 22:10:16 +03:00
printf родился"
2023-02-07 19:48:09 +03:00
Console.ReadKey() |> ignore