-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
620 lines (548 loc) · 24.6 KB
/
Copy pathProgram.fs
File metadata and controls
620 lines (548 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
module Program
open System
open System.IO
open System.Threading
open System.Threading.Tasks
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.DependencyInjection
open Funogram.Api
open Funogram.Telegram
open Funogram.Telegram.Bot
open Funogram.Telegram.Types
open Serilog
open Telebot.Bus
open Telebot.LoggingHandler
open Telebot.Messages
open Telebot.PrometheusMetrics
open Telebot.TelemetryService
open Telebot.Text
open Prometheus
open Suave
open Suave.Filters
open Suave.Operators
open Suave.Successful
// Global cancellation token source for graceful shutdown
let private cancellationTokenSource = new CancellationTokenSource()
// Health monitoring background service
type HealthMonitoringService() =
inherit BackgroundService()
override this.ExecuteAsync(stoppingToken: CancellationToken) =
async {
while not stoppingToken.IsCancellationRequested do
try
// Update various metrics
do! updateQueueMetrics()
// Perform health checks
let! httpHealthy = Telebot.HttpClient.healthCheckAsync()
let! busHealthy = healthCheckAsync()
// Overall health status
let overallHealth = httpHealthy && busHealthy
healthCheckStatus.WithLabels([|"overall"|]).Set(if overallHealth then 1.0 else 0.0)
// Wait 30 seconds before next check
do! Async.Sleep(30000)
with
| ex ->
Log.Error(ex, "Error in health monitoring service")
do! Async.Sleep(5000) // Shorter retry interval on error
} |> Async.StartAsTask :> Task
let getChatOrUserName (chat: Funogram.Telegram.Types.Chat) (user: Funogram.Telegram.Types.User option) =
match chat.Title with
| Some title when not (System.String.IsNullOrWhiteSpace(title)) -> title
| _ ->
match user with
| Some u ->
match u.Username with
| Some username when not (System.String.IsNullOrWhiteSpace(username)) -> $"@{username}"
| _ ->
let first = u.FirstName
let last = defaultArg u.LastName ""
let full = $"{first} {last}".Trim()
if System.String.IsNullOrWhiteSpace(full) then $"User_{u.Id}" else full
| None ->
match chat.Username with
| Some username when not (System.String.IsNullOrWhiteSpace(username)) -> $"@{username}"
| _ ->
let first = defaultArg chat.FirstName ""
let last = defaultArg chat.LastName ""
let full = $"{first} {last}".Trim()
if System.String.IsNullOrWhiteSpace(full) then $"Chat_{chat.Id}" else full
let getChatType (chat: Funogram.Telegram.Types.Chat) =
match chat.Type with
| Funogram.Telegram.Types.ChatType.Private -> "private"
| _ -> "group"
let getUserName (user: Funogram.Telegram.Types.User option) =
match user with
| Some u ->
match u.Username with
| Some username when not (System.String.IsNullOrWhiteSpace(username)) -> $"@{username}"
| _ ->
let first = u.FirstName
let last = defaultArg u.LastName ""
let full = $"{first} {last}".Trim()
if System.String.IsNullOrWhiteSpace(full) then $"User_{u.Id}" else full
| None -> "Unknown"
let stripHtml (html: string) : string =
if System.String.IsNullOrWhiteSpace(html) then ""
else
html
.Replace("<br>", "\n")
.Replace("<br/>", "\n")
.Replace("<br />", "\n")
.Replace("<blockquote>", "\n“")
.Replace("</blockquote>", "”\n")
.Replace("<b>", "")
.Replace("</b>", "")
.Replace("<i>", "")
.Replace("</i>", "")
.Replace("<", "<")
.Replace(">", ">")
.Replace("&", "&")
.Trim()
let hasSupportedLinks (text: string option) =
let ig = getLinks Telebot.Instagram.Instagram.postRegex text @ getLinks Telebot.Instagram.Instagram.shareRegex text
let tw = getLinks Telebot.Twitter.Twitter.twitterRegex text
let tt = getLinks Telebot.TikTok.TikTok.tikTokRegex text
let yt = getLinks Telebot.Youtube.Youtube.youtubeRegex text
not (List.isEmpty ig && List.isEmpty tw && List.isEmpty tt && List.isEmpty yt)
// Enhanced update handler with telemetry
let updateArrivedAsync (ctx: UpdateContext) : Async<unit> =
async {
match ctx.Update.Message, ctx.Update.CallbackQuery with
| Some {
MessageId = messageId
Chat = chat
Text = messageText
From = user
}, _ ->
let chatName = getChatOrUserName chat user
let chatType = getChatType chat
let senderName = getUserName user
receivedMessagesCounter.WithLabels([|chatName; chatType; senderName|]).Inc()
// Check blacklist
let userId = user |> Option.map (fun u -> u.Id)
if Telebot.Blacklist.isBlacklisted chat.Id userId then
Log.Information($"Message ignored due to blacklist. ChatId: {chat.Id}, UserId: {userId}")
unprocessedMessagesCounter.WithLabels([|chatName; chatType; senderName|]).Inc()
return ()
// Create telemetry context
let chatIdStr = chat.Id.ToString()
let messageIdStr = messageId.ToString()
let userIdStr = user |> Option.map (fun u -> u.Id.ToString())
return! withMessageTelemetry chatIdStr messageIdStr "process_telegram_message" (fun scope ->
async {
// Add user information to telemetry
userIdStr |> Option.iter (fun id -> TelemetryScope.addProperty "user_id" id scope |> ignore)
TelemetryScope.addProperty "chat_type" (chat.Type.ToString()) scope |> ignore
messageText |> Option.iter (fun text ->
TelemetryScope.addProperty "message_length" text.Length scope |> ignore
TelemetryScope.addProperty "has_text" true scope |> ignore
)
TelemetryScope.logInfo $"Processing message {messageId} from chat {chat.Id}" scope
// Increment metrics
newMessageCounter.Inc()
if not (hasSupportedLinks messageText) then
unprocessedMessagesCounter.WithLabels([|chatName; chatType; senderName|]).Inc()
// Create message data
let mId = MessageId.Create messageId
let cId = ChatId.Int chat.Id
let updateMessage = {
MessageText = messageText
MessageId = mId
ChatId = cId
}
// Send to bus asynchronously
do! sendToBusAsync updateMessage
TelemetryScope.logInfo "Message processed and sent to bus successfully" scope
}
)
| _, Some cb ->
try
match cb.Data with
| Some data when data.StartsWith("pop_orig:") ->
let cacheId = data.Substring("pop_orig:".Length)
match Telebot.Translation.tryGetTranslationFromCache cacheId with
| Some cached ->
let plainText = stripHtml cached.OriginalText
let popupText: string = if plainText.Length > 200 then plainText.Substring(0, 197) + "..." else plainText
let answerReq = Req.AnswerCallbackQuery.Make(cb.Id, text = popupText, showAlert = true)
do! answerReq |> api ctx.Config |> Async.Ignore
| None ->
let answerReq = Req.AnswerCallbackQuery.Make(cb.Id, text = "Original text not found or expired.", showAlert = true)
do! answerReq |> api ctx.Config |> Async.Ignore
| Some data when data.StartsWith("show_orig:") || data.StartsWith("show_trans:") ->
let isOrig = data.StartsWith("show_orig:")
let cacheId = if isOrig then data.Substring("show_orig:".Length) else data.Substring("show_trans:".Length)
match Telebot.Translation.tryGetTranslationFromCache cacheId with
| Some cached ->
let newText = if isOrig then cached.OriginalText else cached.TranslatedText
let newToggleLabel = if isOrig then "Show Translated Text" else "Show Original Text"
let newToggleData = if isOrig then $"show_trans:{cacheId}" else $"show_orig:{cacheId}"
let btnToggle = InlineKeyboardButton.Create(newToggleLabel, callbackData = newToggleData)
let isGroupChat =
match cb.Message with
| Some (MaybeInaccessibleMessage.Message msg) -> msg.Chat.Id < 0L
| _ -> false
let buttons =
if not isGroupChat then
let webAppBase = System.Environment.GetEnvironmentVariable("WEBAPP_BASE_URL")
if not (System.String.IsNullOrWhiteSpace(webAppBase)) then
let url = $"{webAppBase.Trim().TrimEnd('/')}/webapp?id={cacheId}"
let btnWebApp = InlineKeyboardButton.Create("Original (Web)", webApp = WebAppInfo.Create(url))
[| [| btnWebApp; btnToggle |] |]
else
[| [| btnToggle |] |]
else
[| [| btnToggle |] |]
let keyboard = InlineKeyboardMarkup.Create(buttons)
let answerReq = Req.AnswerCallbackQuery.Make(cb.Id)
do! answerReq |> api ctx.Config |> Async.Ignore
match cb.Message with
| Some (MaybeInaccessibleMessage.Message msg) ->
let chatId = ChatId.Int msg.Chat.Id
if msg.Text.IsSome then
let editReq =
Req.EditMessageText.Make(
chatId = chatId,
messageId = msg.MessageId,
text = newText,
parseMode = ParseMode.HTML,
replyMarkup = keyboard
)
do! editReq |> api ctx.Config |> Async.Ignore
else
let editReq =
Req.EditMessageCaption.Make(
chatId = chatId,
messageId = msg.MessageId,
caption = newText,
parseMode = ParseMode.HTML,
replyMarkup = keyboard
)
do! editReq |> api ctx.Config |> Async.Ignore
| _ -> ()
| None ->
let answerReq = Req.AnswerCallbackQuery.Make(cb.Id, text = "Translation not found or expired.", showAlert = true)
do! answerReq |> api ctx.Config |> Async.Ignore
| _ ->
let answerReq = Req.AnswerCallbackQuery.Make(cb.Id)
do! answerReq |> api ctx.Config |> Async.Ignore
with ex ->
Log.Error(ex, "Error processing callback query")
| _ ->
return ()
}
// Prometheus metrics endpoint with async support
let prometheusEndpoint =
let metricsAsync (ctx: HttpContext) =
async {
try
use stream = new MemoryStream()
let registry = Metrics.DefaultRegistry
do! registry.CollectAndExportAsTextAsync(stream, cancellationTokenSource.Token) |> Async.AwaitTask
stream.Position <- 0
use reader = new StreamReader(stream)
let! content = reader.ReadToEndAsync() |> Async.AwaitTask
return! OK content ctx
with
| ex ->
Log.Error(ex, "Error generating metrics")
return! ServerErrors.INTERNAL_ERROR "Error generating metrics" ctx
}
path "/metrics" >=> Writers.setMimeType "text/plain" >=> metricsAsync
let generateWebAppHtml (contentHtml: string) =
$"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Original Tweet Text</title>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<style>
:root {{
--bg-color: var(--tg-theme-bg-color, #18181b);
--text-color: var(--tg-theme-text-color, #f4f4f5);
--hint-color: var(--tg-theme-hint-color, #a1a1aa);
--btn-color: var(--tg-theme-button-color, #3b82f6);
--btn-text: var(--tg-theme-button-text-color, #ffffff);
--card-bg: var(--tg-theme-secondary-bg-color, #27272a);
}}
body {{
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--bg-color);
color: var(--text-color);
margin: 0;
padding: 20px;
box-sizing: border-box;
display: flex;
flex-direction: column;
min-height: 100vh;
}}
.header {{
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
padding-bottom: 12px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}}
.title {{
font-size: 1.1rem;
font-weight: 600;
margin: 0;
display: flex;
align-items: center;
gap: 8px;
}}
.badge {{
background: var(--btn-color);
color: var(--btn-text);
font-size: 0.75rem;
padding: 2px 8px;
border-radius: 12px;
font-weight: 500;
}}
.content {{
background: var(--card-bg);
border-radius: 16px;
padding: 16px;
line-height: 1.6;
font-size: 1rem;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
border: 1px solid rgba(255, 255, 255, 0.05);
word-break: break-word;
flex-grow: 1;
}}
blockquote {{
margin: 12px 0;
padding: 8px 12px;
border-left: 3px solid var(--btn-color);
background: rgba(255, 255, 255, 0.03);
border-radius: 0 8px 8px 0;
}}
a {{
color: var(--btn-color);
text-decoration: none;
}}
.close-btn {{
margin-top: 20px;
width: 100%%;
padding: 14px;
background-color: var(--btn-color);
color: var(--btn-text);
border: none;
border-radius: 12px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: opacity 0.2s;
}}
.close-btn:active {{
opacity: 0.8;
}}
</style>
</head>
<body>
<div class="header">
<h1 class="title">Original Tweet <span class="badge">Original</span></h1>
</div>
<div class="content">
{contentHtml}
</div>
<button class="close-btn" onclick="Telegram.WebApp.close()">Close</button>
<script>
Telegram.WebApp.ready();
Telegram.WebApp.expand();
</script>
</body>
</html>"""
let webAppHandler (ctx: HttpContext) =
async {
let cacheId = ctx.request.queryParam "id"
match cacheId with
| Choice1Of2 id ->
match Telebot.Translation.tryGetTranslationFromCache id with
| Some cached ->
let html = generateWebAppHtml cached.OriginalText
return! (OK html >=> Writers.setMimeType "text/html; charset=utf-8") ctx
| None ->
let html = generateWebAppHtml "<i>Original text not found or expired.</i>"
return! (OK html >=> Writers.setMimeType "text/html; charset=utf-8") ctx
| _ ->
let html = generateWebAppHtml "<i>Invalid request.</i>"
return! (OK html >=> Writers.setMimeType "text/html; charset=utf-8") ctx
}
// Health check and WebApp endpoint
let healthEndpoint =
let healthCheckAsync (ctx: HttpContext) =
async {
try
let! httpHealthy = Telebot.HttpClient.healthCheckAsync()
let! busHealthy = Telebot.Bus.healthCheckAsync()
let overall = httpHealthy && busHealthy
let status = if overall then "healthy" else "unhealthy"
let statusCode = if overall then OK else ServerErrors.SERVICE_UNAVAILABLE
let healthData = {|
status = status
timestamp = DateTimeOffset.UtcNow.ToString("O")
checks = {|
http_client = if httpHealthy then "healthy" else "unhealthy"
message_bus = if busHealthy then "healthy" else "unhealthy"
|}
|}
return! statusCode (Newtonsoft.Json.JsonConvert.SerializeObject(healthData)) ctx
with
| ex ->
Log.Error(ex, "Health check failed")
return! ServerErrors.INTERNAL_ERROR "Health check failed" ctx
}
choose [
path "/health" >=> Writers.setMimeType "application/json" >=> healthCheckAsync
path "/webapp" >=> webAppHandler
]
// Start web server asynchronously
let startWebServerAsync (port: int) (app: WebPart) : Async<unit> =
async {
let config = {
defaultConfig with
bindings = [ HttpBinding.createSimple HTTP "0.0.0.0" port ]
cancellationToken = cancellationTokenSource.Token
}
Log.Information($"Starting web server on port {port}")
// Start the server in the background - don't wait for it
async {
startWebServer config app
return ()
} |> Async.Start
Log.Information($"Web server started successfully on port {port}")
}
// Configure structured logging
let configureLogging () =
Log.Logger <-
LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithCorrelationId()
.Enrich.WithEnvironmentName()
.Enrich.WithProcessId()
.Enrich.WithThreadId()
.WriteTo.Console(outputTemplate =
"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} " +
"{Properties:j}{NewLine}{Exception}")
.CreateLogger()
Log.Information("Structured logging configured")
// Graceful shutdown handler
let setupGracefulShutdown () =
let shutdown () =
async {
Log.Information("Initiating graceful shutdown...")
try
try
// Cancel all background operations
cancellationTokenSource.Cancel()
// Shutdown services in reverse order
do! shutdownBusAsync()
Telebot.HttpClient.cleanup()
shutdown()
Log.Information("Graceful shutdown completed")
with
| ex -> Log.Error(ex, "Error during shutdown")
finally
Log.CloseAndFlush()
}
// Handle console cancel events
Console.CancelKeyPress.Add(fun _ ->
shutdown() |> Async.RunSynchronously
Environment.Exit(0)
)
// Handle process termination
AppDomain.CurrentDomain.ProcessExit.Add(fun _ ->
shutdown() |> Async.RunSynchronously
)
// Create and configure host with dependency injection
let createHost () =
Host.CreateDefaultBuilder()
.ConfigureServices(fun context services ->
// Add HTTP client factory
Telebot.HttpClient.initializeHttpClientFactory() |> ignore
// Add background services
services.AddHostedService<HealthMonitoringService>() |> ignore
)
.Build()
// Main async entry point
let mainAsync () : Async<int> =
async {
try
// Set console encoding
Console.OutputEncoding <- Text.Encoding.UTF8
// Configure logging first
configureLogging()
// Initialize telemetry
initialize()
// Setup graceful shutdown
setupGracefulShutdown()
Log.Information("Starting Telebot application...")
return! withOperationTelemetry "application_startup" (fun scope ->
async {
try
// Validate required environment variables first
let botToken = Environment.GetEnvironmentVariable "TELEGRAM_BOT_TOKEN"
if String.IsNullOrWhiteSpace botToken then
failwith "TELEGRAM_BOT_TOKEN environment variable is required but not set. Please set this environment variable before running the application."
// Initialize SQLite translation database
Telebot.Translation.initDb()
// Initialize metrics
initializeApplicationMetrics()
// Initialize the message bus
let! _ = initializeBusAsync()
TelemetryScope.logInfo "Message bus initialized" scope
// Create and start the host for background services
use host = createHost()
do! host.StartAsync(cancellationTokenSource.Token) |> Async.AwaitTask
TelemetryScope.logInfo "Background services started" scope
// Start the metrics and health web servers on separate ports
let metricsPort =
Environment.GetEnvironmentVariable "METRICS_PORT"
|> Option.ofObj
|> Option.map int
|> Option.defaultValue 3001
let healthPort =
Environment.GetEnvironmentVariable "HEALTH_PORT"
|> Option.ofObj
|> Option.map int
|> Option.defaultValue 3002
do! startWebServerAsync metricsPort prometheusEndpoint
do! startWebServerAsync healthPort healthEndpoint
TelemetryScope.logInfo $"Metrics server started on port {metricsPort}, Health server started on port {healthPort}" scope
// Configure and start the Telegram bot
let config =
Config.defaultConfig
|> Config.withReadTokenFromEnv "TELEGRAM_BOT_TOKEN"
let configWithLogger = {
config with RequestLogger = Some(SerilogLogger())
}
// Remove webhook and start polling
let! _ = Api.deleteWebhookBase () |> api configWithLogger
TelemetryScope.logInfo "Webhook removed, starting bot polling" scope
// Start bot with enhanced async handler
do! startBot configWithLogger (updateArrivedAsync >> Async.RunSynchronously) None
return 0 // Success
with
| ex ->
TelemetryScope.logError (Some ex) "Application startup failed" scope
return 1
}
)
with
| ex ->
Log.Fatal(ex, "Critical error during application startup")
return 1
}
[<EntryPoint>]
let main _ =
let result =
try
mainAsync() |> Async.RunSynchronously
finally
// Ensure cleanup even if main fails
cancellationTokenSource.Cancel()
Log.CloseAndFlush()
result