publishhelperbot/PublishHelperBot/Program.fs

77 lines
2.6 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
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-25 02:46:05 +03:00
open Serilog.Core
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-25 00:04:13 +03:00
let createBot (config: BotConfig, http: HttpClient) = TelegramBotClient(config.token, http)
let config = createConfig "SBPB_CONFIG_PATH"
let botClient = createBot (config, new HttpClient())
let youtubeDlService =
let youtubeDlClient =
YoutubeDlClient.createClient {
Client = new HttpClient()
BaseUrl = config.YoutubeDlUrl
}
let tgService =
TgService.createService {
Client = botClient
ChannelId = config.chanelId
YoutubeDlClient = youtubeDlClient
AdminChatId = config.adminChatId
}
YoutubeDlService.createService youtubeDlClient tgService
2023-02-11 04:16:34 +03:00
2023-02-07 22:15:46 +03:00
let startDate = DateTime.UtcNow
2023-02-25 00:04:13 +03:00
let (|ObsoleteUpdate|RelayMatchUpdate|YoutubeRepostMatchUpdate|SkipUpdate|) (update: Update) =
let isObsoleteUpdate (update: Update) =
update.Type = UpdateType.Message && update.Message.Date < startDate
match update with
| _ when isObsoleteUpdate update -> ObsoleteUpdate
| _ when RelayMatch (update, config) -> RelayMatchUpdate
| _ when YoutubeRepostMatch (update, config) -> YoutubeRepostMatchUpdate
| _ -> SkipUpdate
let updateHandle (bc: ITelegramBotClient) (update: Update) (ct: CancellationToken): Task =
let tgCtx = (update, config, bc)
2023-02-25 02:46:05 +03:00
try
match update with
| RelayMatchUpdate() ->
Logging.logger.Information("RelayMatchUpdate")
RelayHandler tgCtx
| YoutubeRepostMatchUpdate() ->
YoutubeRepostHandler <| (youtubeDlService, tgCtx)
| ObsoleteUpdate() ->
Logging.logger.Information("Skipping obsolete update")
Task.CompletedTask
| SkipUpdate() ->
Logging.logger.Information("Skipping update")
Task.CompletedTask
with
| e ->
$"Случилась страшная хуйня:\n {e.Message}\n{e.StackTrace}" |> Logging.logger.Error
2023-02-25 00:04:13 +03:00
Task.CompletedTask
let handlePollingError (_: ITelegramBotClient) (e: Exception) (_: CancellationToken) =
Logging.logger.Error(e, "Polling error")
Task.CompletedTask
let receiverOptions = ReceiverOptions(AllowedUpdates = Array.zeroCreate<UpdateType> 0)
Logging.logger.Information("Starting bot")
botClient.StartReceiving(updateHandle, handlePollingError, receiverOptions)
Logging.logger.Information("Я родился")
2023-02-07 19:48:09 +03:00
Console.ReadKey() |> ignore