publishhelperbot/PublishHelperBot/Program.fs
2023-03-07 02:46:50 +03:00

65 lines
1.9 KiB
Forth

open System
open System.Net.Http
open System.Threading
open System.Threading.Tasks
open PublishHelperBot.Environment
open PublishHelperBot.YoutubeDl
open PublishHelperBot.Telegram
open Telegram.Bot
open Telegram.Bot.Polling
open Telegram.Bot.Types
open Telegram.Bot.Types.Enums
let cancellation = new CancellationTokenSource()
let config = createConfig "SBPB_CONFIG_PATH"
let botClient = TelegramBotClient (config.token, new HttpClient())
let youtubeDlClient =
YoutubeDlClient.createClient {
Client = new HttpClient()
BaseUrl = config.youtubeDlUrl
}
let tgService =
TgService.createService {
Client = botClient
ChannelId = config.chanelId
YoutubeDlClient = youtubeDlClient
AdminChatId = config.adminChatId
}
let youtubeDlService =
YoutubeDlService.createService youtubeDlClient tgService (config.tmpYtdlSavePath |> Option.defaultValue "/tmp")
let isObsoleteUpdate =
let startDate = DateTime.UtcNow
fun (update: Update) ->
update.Type = UpdateType.Message
&& update.Message.Date < startDate
let handlePollingError _ (e: Exception) _ =
Logging.logger.Error(e, "Polling error")
Task.CompletedTask
Logging.logger.Information("Starting bot")
let botHandler = TgUpdateHandler.createHandler config tgService youtubeDlService
botClient.StartReceiving(
(fun client update _ ->
if not (isObsoleteUpdate update) then
botHandler.PostUpdate(update)
Task.CompletedTask
),
handlePollingError,
ReceiverOptions(
AllowedUpdates = Array.zeroCreate<UpdateType> 0
)
)
Logging.logger.Information("Я родился")
Console.CancelKeyPress |> Event.add (fun _ -> cancellation.Cancel(); printfn "Сворачиваемся...")
Async.RunSynchronously(
async {
while (not(cancellation.Token.IsCancellationRequested)) do
do! Async.Sleep 500 |> Async.Ignore
})