Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed wrong deserialization #58

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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