Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Oct 18, 2024
1 parent 7f25e74 commit 3855a2b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/VahterBanBot.Tests/ContainerTestBase.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ open System
open System.IO
open System.Net.Http
open System.Text
open System.Text.Json
open System.Threading.Tasks
open DotNet.Testcontainers.Builders
open DotNet.Testcontainers.Configurations
open DotNet.Testcontainers.Containers
open Newtonsoft.Json
open Npgsql
open Telegram.Bot.Types
open Testcontainers.PostgreSql
open VahterBanBot.Tests.TgMessageUtils
open VahterBanBot.Types
open VahterBanBot.Utils
open Xunit
open Dapper

Expand Down Expand Up @@ -168,10 +169,10 @@ type VahterTestContainers() =
member _.Uri = uri

member this.SendMessage(update: Update) = task {
let json = JsonConvert.SerializeObject(update)
let json = JsonSerializer.Serialize(update, options = jsonOptions)
return! this.SendMessage(json)
}

member _.SendMessage(json: string) = task {
let content = new StringContent(json, Encoding.UTF8, "application/json")
let! resp = httpClient.PostAsync("/bot", content)
Expand Down
4 changes: 3 additions & 1 deletion src/VahterBanBot.Tests/TgMessageUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module VahterBanBot.Tests.TgMessageUtils
open System
open System.Threading
open Telegram.Bot.Types
open Telegram.Bot.Types.Enums

type Tg() =
static let mutable i = 1L // higher than the data in the test_seed.sql
Expand All @@ -17,7 +18,8 @@ type Tg() =
static member chat (?id: int64, ?username: string) =
Chat(
Id = (id |> Option.defaultValue (nextInt64())),
Username = (username |> Option.defaultValue null)
Username = (username |> Option.defaultValue null),
Type = ChatType.Supergroup
)

static member callback(data: string, ?from: User) =
Expand Down
7 changes: 4 additions & 3 deletions src/VahterBanBot/FakeTgApi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ open System
open System.Net
open System.Net.Http
open System.Text
open System.Text.Json
open System.Threading.Tasks
open Newtonsoft.Json
open Telegram.Bot.Types
open Telegram.Bot.Types.Enums
open VahterBanBot.Types
open VahterBanBot.Utils

let fakeTgApi (botConf: BotConfiguration) =
{ new DelegatingHandler() with
Expand Down Expand Up @@ -37,7 +38,7 @@ let fakeTgApi (botConf: BotConfiguration) =
Type = ChatType.Private
)
)
|> JsonConvert.SerializeObject
|> fun x -> JsonSerializer.Serialize(x, options = jsonOptions)
apiResult message
elif url.EndsWith "/getChatAdministrators" then
// respond with the request body as a string
Expand All @@ -59,7 +60,7 @@ let fakeTgApi (botConf: BotConfiguration) =
)
)
|]
|> JsonConvert.SerializeObject
|> fun x -> JsonSerializer.Serialize(x, options = jsonOptions)
apiResult message
else
// return 500 for any other request
Expand Down
11 changes: 6 additions & 5 deletions src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

open System
open System.Collections.Generic
open System.Text.Json
open System.Threading
open System.Threading.Tasks
open Dapper
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Logging
open Microsoft.FSharp.Core
open Newtonsoft.Json
open Telegram.Bot
open Telegram.Bot.Polling
open Telegram.Bot.Types
Expand Down Expand Up @@ -43,8 +43,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" |> JsonConvert.DeserializeObject<_>
AllowedUsers = getEnv "ALLOWED_USERS" |> JsonConvert.DeserializeObject<_>
ChatsToMonitor = getEnv "CHATS_TO_MONITOR" |> JsonSerializer.Deserialize<_>
AllowedUsers = getEnv "ALLOWED_USERS" |> JsonSerializer.Deserialize<_>
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 +67,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" "{}" |> JsonConvert.DeserializeObject<_> }
MlStopWordsInChats = getEnvOr "ML_STOP_WORDS_IN_CHATS" "{}" |> JsonSerializer.Deserialize<_> }

let validateApiKey (ctx : HttpContext) =
match ctx.TryGetRequestHeader "X-Telegram-Bot-Api-Secret-Token" with
Expand All @@ -81,6 +81,7 @@ let builder = WebApplication.CreateBuilder()
%builder.Services
.AddSingleton(botConf)
.AddGiraffe()
.ConfigureTelegramBot<Microsoft.AspNetCore.Http.Json.JsonOptions>(fun x -> x.SerializerOptions)
.AddHostedService<CleanupService>()
.AddHostedService<StartupMessage>()
.AddHostedService<UpdateChatAdmins>()
Expand Down Expand Up @@ -153,7 +154,7 @@ let webApp = choose [

POST >=> route botConf.Route >=> requiresApiKey >=> bindJson<Update> (fun update next ctx -> task {
let updateBodyJson =
try JsonConvert.SerializeObject update
try JsonSerializer.Serialize(update, options = jsonOptions)
with e -> e.Message
use topActivity =
botActivity
Expand Down
8 changes: 4 additions & 4 deletions src/VahterBanBot/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
open System
open System.Collections.Generic
open System.Text
open System.Text.Json
open Dapper
open Newtonsoft.Json
open Telegram.Bot.Types
open Utils

Expand Down Expand Up @@ -93,7 +93,7 @@ type DbMessage =
user_id = message.From.Id
created_at = DateTime.UtcNow
text = message.TextOrCaption
raw_message = JsonConvert.SerializeObject message }
raw_message = JsonSerializer.Serialize(message, options = jsonOptions) }

[<CLIMutable>]
type VahterStat =
Expand Down Expand Up @@ -148,9 +148,9 @@ type CallbackMessageTypeHandler() =
inherit SqlMapper.TypeHandler<CallbackMessage>()

override this.SetValue(parameter, value) =
parameter.Value <- JsonConvert.SerializeObject value
parameter.Value <- JsonSerializer.Serialize(value, options = jsonOptions)
override this.Parse(value) =
JsonConvert.DeserializeObject<CallbackMessage>(value.ToString())
JsonSerializer.Deserialize<CallbackMessage>(value.ToString(), options = jsonOptions)

[<CLIMutable>]
type UserStats =
Expand Down
13 changes: 13 additions & 0 deletions src/VahterBanBot/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,16 @@ type Telegram.Bot.Types.Update with
msg.Message
else
msg.EditedMessage

// needed for STJ
let jsonOptions =
let baseOpts = Microsoft.AspNetCore.Http.Json.JsonOptions()
Telegram.Bot.JsonBotAPI.Configure(baseOpts.SerializerOptions)

// HACK TIME
// there is a contradiction in Telegram.Bot library where User.IsBot is not nullable and required during deserialization,
// but it is omitted when default on deserialization via settings setup in JsonBotAPI.Configure
// so we'll override this setting explicitly
baseOpts.SerializerOptions.DefaultIgnoreCondition <- System.Text.Json.Serialization.JsonIgnoreCondition.Never

baseOpts.SerializerOptions
3 changes: 1 addition & 2 deletions src/VahterBanBot/VahterBanBot.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Dapper.FSharp" Version="4.8.0" />
<PackageReference Include="Giraffe" Version="6.4.0" />
<PackageReference Include="Giraffe" Version="7.0.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.3" />
Expand Down

0 comments on commit 3855a2b

Please sign in to comment.