77 lines
2.6 KiB
Forth
77 lines
2.6 KiB
Forth
open System
|
|
open System.Net.Http
|
|
open System.Threading
|
|
open System.Threading.Tasks
|
|
open PublishHelperBot.Handlers
|
|
open PublishHelperBot.Environment
|
|
open PublishHelperBot.YoutubeDl
|
|
open Serilog.Core
|
|
open Telegram.Bot
|
|
open Telegram.Bot.Polling
|
|
open Telegram.Bot.Types
|
|
open Telegram.Bot.Types.Enums
|
|
|
|
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
|
|
|
|
let startDate = DateTime.UtcNow
|
|
|
|
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)
|
|
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
|
|
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("Я родился")
|
|
Console.ReadKey() |> ignore |