Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Funogram.Telegram/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ let private runBot config me updateArrived updatesArrived =
| None ->
let rec loopAsync offset =
async {
if Async.DefaultCancellationToken.IsCancellationRequested then
return ()
try
let! updatesResult =
Req.GetUpdates.Make(offset, ?limit = config.Limit, ?timeout = config.Timeout, ?allowedUpdates = (config.AllowedUpdates |> Option.map Seq.toArray))
|> bot

match updatesResult with
| Ok updates when updates |> Seq.isEmpty |> not ->
let offset = updates |> Seq.map (fun f -> f.UpdateId) |> Seq.max |> fun x -> x + 1L
let offset = updates |> Seq.map (_.UpdateId) |> Seq.max |> fun x -> x + 1L
processUpdates updates
return! loopAsync offset // send new offset
| Error e ->
Expand Down
7 changes: 3 additions & 4 deletions src/Funogram/Tools.fs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ module Api =
async {
let client = config.Client
let url = getUrl config request.MethodName

let serialize =
multipartSerializers.GetOrAdd(
request.GetType(),
Expand All @@ -488,8 +487,8 @@ module Api =
let mutable statusCode = -1
try
let! result =
if hasData then client.PostAsync(url, content) |> Async.AwaitTask
else client.GetAsync(url) |> Async.AwaitTask
if hasData then client.PostAsync(url, content, cancellationToken = Async.DefaultCancellationToken) |> Async.AwaitTask
else client.GetAsync(url, cancellationToken = Async.DefaultCancellationToken) |> Async.AwaitTask

statusCode <- result.StatusCode |> int

Expand Down Expand Up @@ -518,7 +517,7 @@ module Api =
try
use content = new ByteArrayContent(bytes)
content.Headers.ContentType <- MediaTypeHeaderValue.Parse("application/json")
let! result = client.PostAsync(url, content) |> Async.AwaitTask
let! result = client.PostAsync(url, content, cancellationToken = Async.DefaultCancellationToken) |> Async.AwaitTask
statusCode <- result.StatusCode |> int

use! stream = result.Content.ReadAsStreamAsync() |> Async.AwaitTask
Expand Down
30 changes: 22 additions & 8 deletions src/examples/Funogram.TestBot/Program.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Funogram.TestBot.Program

open System
open System.Threading
open Funogram.TestBot
open Funogram.Api
open Funogram.Telegram
Expand All @@ -18,12 +19,25 @@ type ConsoleLogger(color: ConsoleColor) =

[<EntryPoint>]
let main _ =
async {
let config = Config.defaultConfig |> Config.withReadTokenFromFile
let config =
{ config with
RequestLogger = Some (ConsoleLogger(ConsoleColor.Green)) }
let! _ = Api.deleteWebhookBase () |> api config
return! startBot config Commands.Base.updateArrived None
} |> Async.RunSynchronously
Console.CancelKeyPress.Add(fun x ->
x.Cancel <- true
Async.CancelDefaultToken()
)

AppDomain.CurrentDomain.ProcessExit.Add(fun _ ->
Async.CancelDefaultToken()
)

try
async {
let config = Config.defaultConfig |> Config.withReadTokenFromFile
let config =
{ config with
RequestLogger = Some (ConsoleLogger(ConsoleColor.Green)) }
let! _ = Api.deleteWebhookBase () |> api config
return! startBot config Commands.Base.updateArrived None
} |> Async.RunSynchronously
with
| :? OperationCanceledException ->
printfn "Graceful shutdown completed!"
0