Initial infrastructure

This commit is contained in:
Kirill Poletaev 2023-04-06 22:39:03 +03:00
parent f6cafef823
commit f61fea6244
6 changed files with 56 additions and 19 deletions

View File

@ -6,6 +6,7 @@ open FluentMigrator.Runner;
open LinqToDB open LinqToDB
open LinqToDB.Data open LinqToDB.Data
open LinqToDB.DataProvider open LinqToDB.DataProvider
open LinqToDB.DataProvider.PostgreSQL
open LinqToDB.Mapping open LinqToDB.Mapping
open Microsoft.Extensions.DependencyInjection open Microsoft.Extensions.DependencyInjection
@ -52,6 +53,9 @@ let migrateApp (connectionString: string) =
use scope = serviceProvider.CreateScope() use scope = serviceProvider.CreateScope()
serviceProvider.GetRequiredService<IMigrationRunner>().MigrateUp() serviceProvider.GetRequiredService<IMigrationRunner>().MigrateUp()
let createContext (connectionString: string) =
new DbContext(connectionString, PostgreSQLTools.GetDataProvider())
[<TimestampedMigration(2023us, 4us, 6us, 20us, 8us)>] [<TimestampedMigration(2023us, 4us, 6us, 20us, 8us)>]
type InitialMigration() = type InitialMigration() =
inherit AutoReversingMigration() inherit AutoReversingMigration()

View File

@ -11,16 +11,11 @@ module Logging =
config.WriteTo.Console().CreateLogger() config.WriteTo.Console().CreateLogger()
type public BotConfig = { type public BotConfig = {
token: string tempSavePath: string
relayUrl: string connectionString: string
chanelId: int64
adminChatId: int64
youtubeDlUrl: string
tmpYtdlSavePath: string Option
} }
let private readConfig = let private readConfig = File.ReadAllText >> JsonSerializer.Deserialize<BotConfig>
File.ReadAllText >> JsonSerializer.Deserialize<BotConfig>
let public createConfig (name: string) = let public createConfig (name: string) =
match Environment.GetEnvironmentVariable(name) with match Environment.GetEnvironmentVariable(name) with

View File

@ -10,6 +10,7 @@
<Compile Include="Telegram.fs" /> <Compile Include="Telegram.fs" />
<Compile Include="Env.fs" /> <Compile Include="Env.fs" />
<Compile Include="Program.fs" /> <Compile Include="Program.fs" />
<Content Include="config.example.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,25 +1,25 @@
// For more information see https://aka.ms/fsharp-console-apps // For more information see https://aka.ms/fsharp-console-apps
open System open System
open Funogram
open Funogram.Telegram open Funogram.Telegram
open Funogram.Telegram.Bot open Funogram.Telegram.Bot
open Funogram.Tools open Funogram.Tools
open Keroosha.SilencerBot open Keroosha.SilencerBot
open Keroosha.SilencerBot.Telegram
type Config = {
connectionString: string
saveDir: string
}
let config = Env.createConfig "SILENCER_BOT_CONFIG_PATH" 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) 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 { async {
let config = Config.defaultConfig |> Config.withReadTokenFromFile let! _ = Api.makeRequestAsync botConfig <| Api.deleteWebhookBase()
let! _ = Api.deleteWebhookBase () |> Api.makeRequestAsync config return! startBot botConfig handleUpdate None
return! startBot config handleUpdate None
} |> Async.RunSynchronously } |> Async.RunSynchronously

View File

@ -1,2 +1,36 @@
module Keroosha.SilencerBot.Telegram 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 ()
)

View File

@ -0,0 +1,3 @@
{
"ConnectionString": "Server=127.0.0.1;User id=postgres;password=postgres;database=silencer-bot"
}