/Funogram

F# Telegram Bot Api library

Primary LanguageF#MIT LicenseMIT

.NET Core NuGet

Funogram

F# Telegram Bot Api library!

Breaking changes

Funogram 2.0 has breaking changes. You should add two nuget packages: Funogram and Funogram.Telegram.

Getting Started

Firstly you need to install Funogram. Installation for .NET Core users:

dotnet add package Funogram

Installation for .NET Framework users:

Install-Package Funogram

Writing a Bot

To get your first bot running just use startBot function from Funogram.Bot module. If you don't need any additional configuration, you can use defaultConfig constant that stores default settings. Note that you need to extend default config with your own bot token received from BotFather. Here you go:

open Funogram.Bot

startBot { defaultConfig with Token = "your token" } onUpdate None

Every update received from Telegram Bot API will be passed to onUpdate function. It should handle user input according to UpdateContext passed. Use processCommands function to process commands addressed to your Bot, cmd and cmdScan can help you.

let onUpdate (context: UpdateContext) =
  processCommands context [
    cmd "/start" onStart
    cmd "/help" onHelp
  ]

Use Funogram.Api module to communicate with Telegram API with the help of request builders implemented using curried functions, for example sendMessage, sendPhoto and so on.

"Hello, world!"
|> sendMessage chatId
|> api accessToken
|> Async.RunSynchronously
|> ignore (* or process API response somehow *)

So, that is it! Use Intellisence-driven development approach to explore all Funogram features! For further learning you may take a look at sample Telegram bots built using this library: Test Bot, Memes Bot

Getting updates via webhooks

If you want to use webhooks, you should start application with admin privileges.

To get updates via webhooks you need send your endpoint address to Telegram server:

let! hook = setWebhookBase webSocketEndpoint None None None |> api config

You can use (ngrok)[https://ngrok.com/] service to test webhooks on your local machine that no have public address.

Then you should set WebHook field in BotConfig. WebHook field have BotWebHook type that contains two fields: Listener and ValidateRequest:

let apiPath = sprintf "/%s" config.Token
let webSocketEndpoint = sprintf "https://1c0860ec2320.ngrok.io/%s" webSocketEndpoint apiPath
let! hook = setWebhookBase webSocketEndpoint None None None |> 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 set webhook: %A" e
  return ()

Telegram (recommend)[https://core.telegram.org/bots/api#setwebhook] using a secret path in URL with your bot's token. You can validate telegram request in ValidateRequest function.

Articles

Funogram: Writing Telegram Bots In F#

Funogram.Keyboard: How to reserve seats on an airplane using F # and telegram

Plugins and Extensions

Funogram.Keyboard

Work in Progress

Old methods moved in Funogram.Rest module. New more functional api available in Funogram.Api module.

Not recommended to use functions who ends with Base, because it's may be changed in future. If you want to use this functions, you need to minimize usage of it (write wrapper, for example).

Completed đź‘Ť:

  • getUpdates
  • getMe
  • forwardMessage
  • âť•sendMessage (not tested ForceReply)
  • getUserProfilePhotos
  • âť•getFile (not tested)
  • âť•kickChatMember (not tested)
  • âť•unbanChatMember (not tested)
  • âť•leaveChat (not tested)
  • getChat
  • âť•getChatAdministrators (not tested)
  • âť•getChatMembersCount (not tested)
  • âť•getChatMember (not tested)
  • âť•answerCallbackQuery (not tested)
  • âť•editMessageText (not tested)
  • âť•editMessageCaption (not tested)
  • âť•editMessageReplyMarkup (not tested)
  • âť•deleteMessage (not tested)
  • âť•answerInlineQuery (not tested)
  • âť•sendInvoice (not tested)
  • âť•answerShippingQuery (not tested)
  • âť•answerPreCheckoutQuery (not tested)
  • sendPhoto
  • âť•sendAudio (not tested)
  • âť•sendDocument (not tested)
  • âť•sendSticker (not tested)
  • âť•sendVideo (not tested)
  • âť•sendVoice (not tested)
  • âť•sendVideoNote (not tested)
  • âť•sendLocation (not tested)
  • âť•sendVenue (not tested)
  • âť•sendContact (not tested)
  • âť•sendChatAction (not tested)
  • âť•sendGame (not tested)
  • âť•setGameScore (not tested)
  • âť•getGameHighScores (not tested)
  • âť•restrictChatMember (not tested)
  • âť•promoteChatMember (not tested)
  • âť•kickChatMember (not tested)
  • âť•exportChatInviteLink (not tested)
  • âť•setChatPhoto (not tested)
  • âť•deleteChatPhoto (not tested)
  • âť•setChatTitle (not tested)
  • âť•setChatDescription (not tested)
  • âť•pinChatMessage (not tested)
  • âť•unpinChatMessage (not tested)