From fbcbf60b09b9d0ba2f52bdb4dcb4f721096f604f Mon Sep 17 00:00:00 2001 From: Ayrat Hudaygulov Date: Fri, 18 Oct 2024 17:56:19 +0100 Subject: [PATCH] fixed wrong deserialization (#58) --- src/VahterBanBot.Tests/ContainerTestBase.fs | 4 ++-- src/VahterBanBot/Program.fs | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/VahterBanBot.Tests/ContainerTestBase.fs b/src/VahterBanBot.Tests/ContainerTestBase.fs index 6307e09..8fee468 100644 --- a/src/VahterBanBot.Tests/ContainerTestBase.fs +++ b/src/VahterBanBot.Tests/ContainerTestBase.fs @@ -87,8 +87,8 @@ type VahterTestContainers() = .WithEnvironment("BOT_TELEGRAM_TOKEN", "123:456") .WithEnvironment("BOT_AUTH_TOKEN", "OUR_SECRET") .WithEnvironment("LOGS_CHANNEL_ID", "-123") - .WithEnvironment("CHATS_TO_MONITOR", """{"pro.hell":-666,"dotnetru":-42}""") - .WithEnvironment("ALLOWED_USERS", """{"vahter_1":34,"vahter_2":69}""") + .WithEnvironment("CHATS_TO_MONITOR", """{"pro.hell":"-666","dotnetru":-42}""") + .WithEnvironment("ALLOWED_USERS", """{"vahter_1":"34","vahter_2":69}""") .WithEnvironment("SHOULD_DELETE_CHANNEL_MESSAGES", "true") .WithEnvironment("IGNORE_SIDE_EFFECTS", "false") .WithEnvironment("USE_FAKE_TG_API", "true") diff --git a/src/VahterBanBot/Program.fs b/src/VahterBanBot/Program.fs index 9b55a65..2867b3d 100644 --- a/src/VahterBanBot/Program.fs +++ b/src/VahterBanBot/Program.fs @@ -3,6 +3,7 @@ open System open System.Collections.Generic open System.Text.Json +open System.Text.Json.Serialization open System.Threading open System.Threading.Tasks open Dapper @@ -36,6 +37,13 @@ type Root = class end Dapper.FSharp.PostgreSQL.OptionTypes.register() SqlMapper.AddTypeHandler(CallbackMessageTypeHandler()); +let botConfJsonOptions = + let opts = JsonSerializerOptions(JsonSerializerDefaults.Web) + opts.NumberHandling <- JsonNumberHandling.AllowReadingFromString + opts +let fromJson<'a> (json: string) = + JsonSerializer.Deserialize<'a>(json, botConfJsonOptions) + let botConf = { BotToken = getEnv "BOT_TELEGRAM_TOKEN" Route = getEnvOr "BOT_HOOK_ROUTE" "/bot" @@ -43,8 +51,8 @@ let botConf = BotUserId = getEnv "BOT_USER_ID" |> int64 BotUserName = getEnv "BOT_USER_NAME" LogsChannelId = getEnv "LOGS_CHANNEL_ID" |> int64 - ChatsToMonitor = getEnv "CHATS_TO_MONITOR" |> JsonSerializer.Deserialize<_> - AllowedUsers = getEnv "ALLOWED_USERS" |> JsonSerializer.Deserialize<_> + ChatsToMonitor = getEnv "CHATS_TO_MONITOR" |> fromJson + AllowedUsers = getEnv "ALLOWED_USERS" |> fromJson ShouldDeleteChannelMessages = getEnvOr "SHOULD_DELETE_CHANNEL_MESSAGES" "true" |> bool.Parse IgnoreSideEffects = getEnvOr "IGNORE_SIDE_EFFECTS" "false" |> bool.Parse UsePolling = getEnvOr "USE_POLLING" "false" |> bool.Parse @@ -67,7 +75,7 @@ let botConf = MlTrainingSetFraction = getEnvOr "ML_TRAINING_SET_FRACTION" "0.2" |> float MlSpamThreshold = getEnvOr "ML_SPAM_THRESHOLD" "0.5" |> single MlWarningThreshold = getEnvOr "ML_WARNING_THRESHOLD" "0.0" |> single - MlStopWordsInChats = getEnvOr "ML_STOP_WORDS_IN_CHATS" "{}" |> JsonSerializer.Deserialize<_> } + MlStopWordsInChats = getEnvOr "ML_STOP_WORDS_IN_CHATS" "{}" |> fromJson } let validateApiKey (ctx : HttpContext) = match ctx.TryGetRequestHeader "X-Telegram-Bot-Api-Secret-Token" with