publishhelperbot/PublishHelperBot/Program.fs

65 lines
1.9 KiB
Forth
Raw Normal View History

2023-02-25 00:04:13 +03:00
open System
2023-02-07 19:48:09 +03:00
open System.Net.Http
2023-03-07 02:46:50 +03:00
open System.Threading
2023-02-07 19:48:09 +03:00
open System.Threading.Tasks
2023-02-07 21:55:46 +03:00
open PublishHelperBot.Environment
2023-02-11 04:16:34 +03:00
open PublishHelperBot.YoutubeDl
2023-02-26 19:08:42 +03:00
open PublishHelperBot.Telegram
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-03-07 02:46:50 +03:00
let cancellation = new CancellationTokenSource()
2023-02-25 00:04:13 +03:00
let config = createConfig "SBPB_CONFIG_PATH"
2023-02-26 19:08:42 +03:00
let botClient = TelegramBotClient (config.token, new HttpClient())
2023-02-25 00:04:13 +03:00
2023-02-26 19:08:42 +03:00
let youtubeDlClient =
YoutubeDlClient.createClient {
Client = new HttpClient()
2023-02-27 23:03:31 +03:00
BaseUrl = config.youtubeDlUrl
2023-02-26 19:08:42 +03:00
}
2023-02-25 00:04:13 +03:00
2023-02-26 19:08:42 +03:00
let tgService =
TgService.createService {
Client = botClient
ChannelId = config.chanelId
YoutubeDlClient = youtubeDlClient
AdminChatId = config.adminChatId
}
2023-02-25 00:04:13 +03:00
2023-02-26 19:08:42 +03:00
let youtubeDlService =
2023-03-07 01:08:26 +03:00
YoutubeDlService.createService youtubeDlClient tgService (config.tmpYtdlSavePath |> Option.defaultValue "/tmp")
2023-02-11 04:16:34 +03:00
2023-02-26 19:08:42 +03:00
let isObsoleteUpdate =
let startDate = DateTime.UtcNow
fun (update: Update) ->
update.Type = UpdateType.Message
&& update.Message.Date < startDate
2023-02-25 00:04:13 +03:00
2023-02-26 19:08:42 +03:00
let handlePollingError _ (e: Exception) _ =
2023-02-25 00:04:13 +03:00
Logging.logger.Error(e, "Polling error")
Task.CompletedTask
Logging.logger.Information("Starting bot")
2023-02-26 19:08:42 +03:00
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
)
)
2023-02-25 00:04:13 +03:00
Logging.logger.Information("Я родился")
2023-03-07 02:46:50 +03:00
Console.CancelKeyPress |> Event.add (fun _ -> cancellation.Cancel(); printfn "Сворачиваемся...")
Async.RunSynchronously(
async {
while (not(cancellation.Token.IsCancellationRequested)) do
do! Async.Sleep 500 |> Async.Ignore
})