Program.fs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. module RedCat.Program
  2. open Microsoft.Extensions.Logging
  3. open Funogram.Api
  4. open Funogram.Telegram
  5. open Funogram.Telegram.Bot
  6. open RedCat.DrugsDataExtractor
  7. open RedCat.RecordFormatter
  8. let private factory = LoggerFactory.Create(fun builder ->
  9. builder.AddSimpleConsole(fun opts -> opts.TimestampFormat <- "[MM-dd HH:mm:ss.fff] ") |> ignore)
  10. let NewConsoleOnlyLogger (loggerName: string) =
  11. factory.CreateLogger(loggerName)
  12. let logger = NewConsoleOnlyLogger "RedCatLog"
  13. type BotCommands =
  14. | Start
  15. | Combo of string * string
  16. | Info of string
  17. | Dose of string
  18. | Search of string
  19. let parseCommand (cmd: BotCommands) =
  20. match cmd with
  21. | Start -> "Utilizzo: !info lsd"
  22. | Combo (userInputA, userInputB) ->
  23. match getComboRecord userInputA userInputB with
  24. | Ok comboRecord -> formatComboRecord comboRecord
  25. | Error errMsg ->
  26. logger.LogError errMsg
  27. $"Non trovo informazioni relative alla combo {userInputA} e {userInputB}"
  28. | Info userInput ->
  29. match getDrugRecord userInput with
  30. | Ok (DrugRecord drugData) -> formatDrugInfoResponse drugData
  31. | Ok (ResponseText suggestionsResponse) ->
  32. $"Non trovo informazioni relative a '{userInput}'.\nForse stavi cercando:\n{suggestionsResponse}"
  33. | Error _ -> $"Non trovo informazioni relative a '{userInput}'"
  34. | Dose userInput -> "Not implemented yet"
  35. | Search userInput ->
  36. match getSearchResponse userInput with
  37. | Ok (ResponseText rt) -> $"Possibili risultati: \n{rt}"
  38. | Error _ -> $"Non trovo informazioni relative a '{userInput}'"
  39. | _ -> failwith "Shouldn't happen here"
  40. let sendMsg (ctx: UpdateContext) (cmd: BotCommands) =
  41. match ctx.Update.Message with
  42. | Some { MessageId = messageId; Chat = chat } ->
  43. let msg = cmd |> parseCommand
  44. Api.sendMessageReply chat.Id msg messageId
  45. |> api ctx.Config
  46. |> Async.Ignore
  47. |> Async.Start
  48. | _ -> ()
  49. let updateArrived (ctx: UpdateContext) =
  50. processCommands ctx [|
  51. cmd "!start" (fun _ -> sendMsg ctx Start)
  52. cmdScan "!info %s" (fun userInput _ -> sendMsg ctx (Info (formatDrugInput userInput)))
  53. cmdScan "!search %s" (fun userInput _ -> sendMsg ctx (Search (formatDrugInput userInput)))
  54. //cmdScan "!combo %s %s" (fun (drugA, drugB) _ -> sendMsg ctx (Combo(drugA,drugB)))
  55. //cmdScan "!dose %s" (fun drug _ -> sendMsg ctx (Dose drug))
  56. |] |> ignore
  57. [<EntryPoint>]
  58. let main _ =
  59. async {
  60. let config = { Config.defaultConfig
  61. with Token = "1171254969:AAGraSaWhTxAoJVLnSEkh8DfrpqdqOmf1aM" }
  62. let! _ = Api.deleteWebhookBase () |> api config
  63. return! startBot config updateArrived None
  64. } |> Async.RunSynchronously
  65. 0