Initial infrastructure
This commit is contained in:
parent
f6cafef823
commit
f61fea6244
@ -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()
|
||||||
|
@ -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
|
||||||
|
@ -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>
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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 ()
|
||||||
|
)
|
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