Initial infrastructure
This commit is contained in:
parent
f6cafef823
commit
f61fea6244
@ -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<IMigrationRunner>().MigrateUp()
|
||||
|
||||
let createContext (connectionString: string) =
|
||||
new DbContext(connectionString, PostgreSQLTools.GetDataProvider())
|
||||
|
||||
[<TimestampedMigration(2023us, 4us, 6us, 20us, 8us)>]
|
||||
type InitialMigration() =
|
||||
inherit AutoReversingMigration()
|
||||
|
@ -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<BotConfig>
|
||||
let private readConfig = File.ReadAllText >> JsonSerializer.Deserialize<BotConfig>
|
||||
|
||||
let public createConfig (name: string) =
|
||||
match Environment.GetEnvironmentVariable(name) with
|
||||
|
@ -10,6 +10,7 @@
|
||||
<Compile Include="Telegram.fs" />
|
||||
<Compile Include="Env.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
<Content Include="config.example.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 ()
|
||||
)
|
3
Keroosha.SilencerBot/config.example.json
Normal file
3
Keroosha.SilencerBot/config.example.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"ConnectionString": "Server=127.0.0.1;User id=postgres;password=postgres;database=silencer-bot"
|
||||
}
|
Loading…
Reference in New Issue
Block a user