redcatbot/RedCat/Program.fs
2024-05-16 11:03:36 +02:00

74 lines
No EOL
2.7 KiB
Forth

module RedCat.Program
open Microsoft.Extensions.Logging
open Funogram.Api
open Funogram.Telegram
open Funogram.Telegram.Bot
open RedCat.DrugsDataExtractor
open RedCat.RecordFormatter
let private factory = LoggerFactory.Create(fun builder ->
builder.AddSimpleConsole(fun opts -> opts.TimestampFormat <- "[MM-dd HH:mm:ss.fff] ") |> ignore)
let NewConsoleOnlyLogger (loggerName: string) =
factory.CreateLogger(loggerName)
let logger = NewConsoleOnlyLogger "RedCatLog"
type BotCommands =
| Start
| Combo of string * string
| Info of string
| Dose of string
| Search of string
let parseCommand (cmd: BotCommands) =
match cmd with
| Start -> "Utilizzo: !info lsd"
| Combo (userInputA, userInputB) ->
match getComboRecord userInputA userInputB with
| Ok comboRecord -> formatComboRecord comboRecord
| Error errMsg ->
logger.LogError errMsg
$"Non trovo informazioni relative alla combo {userInputA} e {userInputB}"
| Info userInput ->
match getDrugRecord userInput with
| Ok (DrugRecord drugData) -> formatDrugInfoResponse drugData
| Ok (ResponseText suggestionsResponse) ->
$"Non trovo informazioni relative a '{userInput}'.\nForse stavi cercando:\n{suggestionsResponse}"
| Error _ -> $"Non trovo informazioni relative a '{userInput}'"
| Dose userInput -> "Not implemented yet"
| Search userInput ->
match getSearchResponse userInput with
| Ok (ResponseText rt) -> $"Possibili risultati: \n{rt}"
| Error _ -> $"Non trovo informazioni relative a '{userInput}'"
| _ -> failwith "Shouldn't happen here"
let sendMsg (ctx: UpdateContext) (cmd: BotCommands) =
match ctx.Update.Message with
| Some { MessageId = messageId; Chat = chat } ->
let msg = cmd |> parseCommand
Api.sendMessageReply chat.Id msg messageId
|> api ctx.Config
|> Async.Ignore
|> Async.Start
| _ -> ()
let updateArrived (ctx: UpdateContext) =
processCommands ctx [|
cmd "!start" (fun _ -> sendMsg ctx Start)
cmdScan "!info %s" (fun userInput _ -> sendMsg ctx (Info (formatDrugInput userInput)))
cmdScan "!search %s" (fun userInput _ -> sendMsg ctx (Search (formatDrugInput userInput)))
//cmdScan "!combo %s %s" (fun (drugA, drugB) _ -> sendMsg ctx (Combo(drugA,drugB)))
//cmdScan "!dose %s" (fun drug _ -> sendMsg ctx (Dose drug))
|] |> ignore
[<EntryPoint>]
let main _ =
async {
let config = { Config.defaultConfig
with Token = "1171254969:AAGraSaWhTxAoJVLnSEkh8DfrpqdqOmf1aM" }
let! _ = Api.deleteWebhookBase () |> api config
return! startBot config updateArrived None
} |> Async.RunSynchronously
0