Skip to content

Commit

Permalink
added logging of update body
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Jan 22, 2024
1 parent e2b40e2 commit 64e48e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DEBUG=true
LOGS_CHANNEL_ID=-1000000000000
ALLOWED_USERS={"you":"123467890"}
CHATS_TO_MONITOR={"your_channel":"-100123467890"}
DATABASE_URL=AMAZING_ADO_NET_CONNECTION_STRING
DATABASE_URL=Server=localhost;Database=vahter_bot_ban;Port=5432;User Id=vahter_bot_ban_service;Password=password;
OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://zipkin:9411/api/v2/spans
OTEL_EXPORTER_CONSOLE=false
IGNORE_SIDE_EFFECTS=true
17 changes: 13 additions & 4 deletions src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,21 @@ let webApp = choose [
GET >=> text "OK"

POST >=> route botConf.Route >=> requiresApiKey >=> bindJson<Update> (fun update next ctx -> task {
use banOnReplyActivity = botActivity.StartActivity("postUpdate")
let updateBodyJson =
try JsonConvert.SerializeObject update
with e -> e.Message
banOnReplyActivity.SetCustomProperty("updateBodyObject", update)
banOnReplyActivity.SetCustomProperty("updateBodyJson", updateBodyJson)

use scope = ctx.RequestServices.CreateScope()
let telegramClient = scope.ServiceProvider.GetRequiredService<ITelegramBotClient>()
let logger = ctx.GetLogger<Root>()
try
do! onUpdate telegramClient botConf (ctx.GetLogger "VahterBanBot.Bot") update.Message
with e ->
logger.LogError(e, "Unexpected error while processing update")
with e ->
logger.LogError(e, $"Unexpected error while processing update: {updateBodyJson}")

return! Successful.OK() next ctx
})
]
Expand All @@ -126,7 +134,7 @@ let server = app.RunAsync()

let telegramClient = app.Services.GetRequiredService<ITelegramBotClient>()

let startLogMsg =
let getStartLogMsg() =
let sb = System.Text.StringBuilder()
%sb.AppendLine("Bot started with following configuration")
%sb.AppendLine("AllowedUsers:")
Expand All @@ -141,8 +149,9 @@ let startLogMsg =

sb.ToString()

app.Logger.LogInformation startLogMsg
if not botConf.IgnoreSideEffects then
let startLogMsg = getStartLogMsg()
app.Logger.LogInformation startLogMsg
telegramClient.SendTextMessageAsync(ChatId(botConf.LogsChannelId), startLogMsg).Wait()

// Dev mode only
Expand Down

0 comments on commit 64e48e2

Please sign in to comment.