From f61fea6244acaa102faf8dc6e3e75e8a23efdd84 Mon Sep 17 00:00:00 2001 From: Kirill Poletaev Date: Thu, 6 Apr 2023 22:39:03 +0300 Subject: [PATCH] Initial infrastructure --- Keroosha.SilencerBot/Database.fs | 4 +++ Keroosha.SilencerBot/Env.fs | 11 ++---- .../Keroosha.SilencerBot.fsproj | 1 + Keroosha.SilencerBot/Program.fs | 22 ++++++------ Keroosha.SilencerBot/Telegram.fs | 34 +++++++++++++++++++ Keroosha.SilencerBot/config.example.json | 3 ++ 6 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 Keroosha.SilencerBot/config.example.json diff --git a/Keroosha.SilencerBot/Database.fs b/Keroosha.SilencerBot/Database.fs index 9a4262c..5ced8ac 100644 --- a/Keroosha.SilencerBot/Database.fs +++ b/Keroosha.SilencerBot/Database.fs @@ -6,6 +6,7 @@ open FluentMigrator.Runner; open LinqToDB open LinqToDB.Data open LinqToDB.DataProvider +open LinqToDB.DataProvider.PostgreSQL open LinqToDB.Mapping open Microsoft.Extensions.DependencyInjection @@ -52,6 +53,9 @@ let migrateApp (connectionString: string) = use scope = serviceProvider.CreateScope() serviceProvider.GetRequiredService().MigrateUp() +let createContext (connectionString: string) = + new DbContext(connectionString, PostgreSQLTools.GetDataProvider()) + [] type InitialMigration() = inherit AutoReversingMigration() diff --git a/Keroosha.SilencerBot/Env.fs b/Keroosha.SilencerBot/Env.fs index 9176972..95c5a53 100644 --- a/Keroosha.SilencerBot/Env.fs +++ b/Keroosha.SilencerBot/Env.fs @@ -11,16 +11,11 @@ module Logging = config.WriteTo.Console().CreateLogger() type public BotConfig = { - token: string - relayUrl: string - chanelId: int64 - adminChatId: int64 - youtubeDlUrl: string - tmpYtdlSavePath: string Option + tempSavePath: string + connectionString: string } -let private readConfig = - File.ReadAllText >> JsonSerializer.Deserialize +let private readConfig = File.ReadAllText >> JsonSerializer.Deserialize let public createConfig (name: string) = match Environment.GetEnvironmentVariable(name) with diff --git a/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj b/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj index 747ed28..7dabb0d 100644 --- a/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj +++ b/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj @@ -10,6 +10,7 @@ + diff --git a/Keroosha.SilencerBot/Program.fs b/Keroosha.SilencerBot/Program.fs index 97d73fb..eb83363 100644 --- a/Keroosha.SilencerBot/Program.fs +++ b/Keroosha.SilencerBot/Program.fs @@ -1,25 +1,25 @@ // For more information see https://aka.ms/fsharp-console-apps open System +open Funogram open Funogram.Telegram open Funogram.Telegram.Bot open Funogram.Tools open Keroosha.SilencerBot - -type Config = { - connectionString: string - saveDir: string -} +open Keroosha.SilencerBot.Telegram let config = Env.createConfig "SILENCER_BOT_CONFIG_PATH" +let botConfig = Config.defaultConfig |> Config.withReadTokenFromFile -let handleUpdate (ctx: UpdateContext) = - () - +let ctxFactory = fun () -> Database.createContext <| config.connectionString Console.CancelKeyPress |> Event.add (fun _ -> Environment.Exit <| 0) +Database.migrateApp config.connectionString + +let botInbox = createBotInbox <| (botConfig, ctxFactory) +let handleUpdate (ctx: UpdateContext) = resolveUpdate ctx |> botInbox.Post + async { - let config = Config.defaultConfig |> Config.withReadTokenFromFile - let! _ = Api.deleteWebhookBase () |> Api.makeRequestAsync config - return! startBot config handleUpdate None + let! _ = Api.makeRequestAsync botConfig <| Api.deleteWebhookBase() + return! startBot botConfig handleUpdate None } |> Async.RunSynchronously diff --git a/Keroosha.SilencerBot/Telegram.fs b/Keroosha.SilencerBot/Telegram.fs index b818d07..19f2e06 100644 --- a/Keroosha.SilencerBot/Telegram.fs +++ b/Keroosha.SilencerBot/Telegram.fs @@ -1,2 +1,36 @@ module Keroosha.SilencerBot.Telegram +open Funogram.Telegram.Bot +open Funogram.Telegram.Types +open Funogram.Types +open Keroosha.SilencerBot.Database + +type VoiceRemoveArgs = { + fileId: string + chatId: int64 +} + +type Features = + | VoiceRemove of VoiceRemoveArgs + | Unknown + +let isVoiceRemoveAction(update: Update) = + update.Message.IsSome && update.Message.Value.Audio.IsSome + +let resolveUpdate (ctx: UpdateContext) = + match ctx.Update with + | x when isVoiceRemoveAction x -> + VoiceRemove { fileId = x.Message.Value.Audio.Value.FileId; chatId = x.Message.Value.Chat.Id } + | _ -> Unknown + +let createBotInbox (cfg: BotConfig, db: unit -> DbContext) = MailboxProcessor.Start(fun (inbox) -> + let rec loop () = + async { + match! inbox.Receive() with + | VoiceRemove x -> () + | Unknown -> () + + return! loop () + } + loop () + ) \ No newline at end of file diff --git a/Keroosha.SilencerBot/config.example.json b/Keroosha.SilencerBot/config.example.json new file mode 100644 index 0000000..9cf4472 --- /dev/null +++ b/Keroosha.SilencerBot/config.example.json @@ -0,0 +1,3 @@ +{ + "ConnectionString": "Server=127.0.0.1;User id=postgres;password=postgres;database=silencer-bot" +} \ No newline at end of file