Skip to content

Commit

Permalink
fixed wrong deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Oct 18, 2024
1 parent d35a8d8 commit a361ff1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/VahterBanBot.Tests/ContainerTestBase.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 11 additions & 3 deletions src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,15 +37,22 @@ 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"
SecretToken = getEnv "BOT_AUTH_TOKEN"
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
Expand All @@ -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
Expand Down

0 comments on commit a361ff1

Please sign in to comment.