Skip to content

Instantly share code, notes, and snippets.

@paavohuhtala
Created December 3, 2016 19:08
Show Gist options
  • Save paavohuhtala/099fd3e826b2b37901108881e0ff7430 to your computer and use it in GitHub Desktop.
Save paavohuhtala/099fd3e826b2b37901108881e0ff7430 to your computer and use it in GitHub Desktop.
Telegram echo bot in F# \w Hopac
open System
open Telegram.Bot
open Telegram.Bot.Types
open Telegram.Bot.Types.Enums
open Hopac
let token = "nope"
let msgBox = Mailbox<Message> ()
module Bot =
let replyToMsg (bot: TelegramBotClient) (msg: Message) x = job {
do! Job.awaitUnitTask (bot.SendChatActionAsync(msg.Chat.Id, ChatAction.Typing))
do! timeOutMillis (String.length x * 10 + 100)
do! Job.awaitUnitTask (bot.SendTextMessageAsync(msg.Chat.Id, x))
}
let serverJob bot = job {
let! msg = Mailbox.take msgBox
printfn "%s" msg.Text
do! Bot.replyToMsg bot msg msg.Text
}
[<EntryPoint>]
let main argv =
let bot = Telegram.Bot.TelegramBotClient(token)
bot.GetMeAsync().Result |> ignore
bot.StartReceiving ()
bot.OnMessage.Add (fun x -> Mailbox.Now.send msgBox x.Message)
Job.foreverServer (serverJob bot)
|> start
Console.ReadKey (true)
|> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment