-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProgram.fs
More file actions
49 lines (42 loc) · 1.53 KB
/
Program.fs
File metadata and controls
49 lines (42 loc) · 1.53 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
module Funogram.Examples.WebSocket.Program
open System.Net.Http
open Funogram.Api
open Funogram.Types
open Funogram.Telegram
open Funogram.Telegram.Bot
open System.Net
[<Literal>]
let WebSocketEndpoint = "https://1c0860ec2320.ngrok.io" // I am using ngrok for tests
let updateArrived (ctx: UpdateContext) =
match ctx.Update.Message with
| Some { From = Some from } ->
async {
match! Api.sendMessage from.Id "I got an update via websocket!" |> api ctx.Config with
| Result.Ok _ -> printfn "Message sent successfully!"
| Result.Error _ -> printf "Cannot send message!"
} |> Async.Start
| _ -> ()
[<EntryPoint>]
let main _ =
// setup
let handler = new HttpClientHandler()
// you can use proxy if you want
// handler.Proxy <- createMyProxy ()
// handler.UseProxy <- true
let config = { Config.defaultConfig with Client = new HttpClient(handler, true) } |> Config.withReadTokenFromFile
async {
let apiPath = sprintf "/%s" config.Token
let webSocketEndpoint = sprintf "%s%s" WebSocketEndpoint apiPath
let! hook = Req.SetWebhook.Make(webSocketEndpoint) |> api config
match hook with
| Ok _ ->
use listener = new HttpListener()
listener.Prefixes.Add("http://*:4444/")
listener.Start()
let webhook = { Listener = listener; ValidateRequest = (fun req -> req.Url.LocalPath = apiPath) }
return! startBot { config with WebHook = Some webhook } updateArrived None
| Error e ->
printf "Can't setup webhook: %A" e
return ()
} |> Async.RunSynchronously
0