### Install Funogram and Funogram.Telegram via Package Manager Console Source: https://github.com/dolfik1/funogram/blob/master/README.md For .NET Framework users, install the Funogram and Funogram.Telegram packages using the Package Manager Console. ```powershell Install-Package Funogram Install-Package Funogram.Telegram ``` -------------------------------- ### Main Bot Entry Point Source: https://github.com/dolfik1/funogram/blob/master/README.md Sets up the bot configuration, deletes any existing webhook, and starts the main bot loop with the defined update handler. This is the primary function to run your bot. ```fsharp [] let main _ = async { let config = Config.defaultConfig |> Config.withReadTokenFromFile let! _ = Api.deleteWebhookBase () |> api config return! startBot config updateArrived None } |> Async.RunSynchronously 0 ``` -------------------------------- ### Install Funogram and Funogram.Telegram via .NET CLI Source: https://github.com/dolfik1/funogram/blob/master/README.md Use the .NET CLI to add the Funogram and Funogram.Telegram packages to your project. ```shell dotnet add package Funogram dotnet add package Funogram.Telegram ``` -------------------------------- ### Start Bot with Update Array Handler Source: https://github.com/dolfik1/funogram/blob/master/README.md Starts the bot loop and provides a secondary handler to process the entire array of updates received at once. This is useful for batch processing or logging all updates. ```fsharp return! startBot config (fun _ -> ()) ((fun updates -> printfn "Got updates array: %A" updates) |> Some) ``` -------------------------------- ### Configure and Start Bot with Webhook Listener in F# Source: https://github.com/dolfik1/funogram/blob/master/README.md Set up an `HttpListener` to handle incoming webhook requests. The `ValidateRequest` function can be used to ensure requests are for the correct API path, often including the bot's token for security. ```fsharp 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 () ``` -------------------------------- ### Configure Bot Token from File Source: https://github.com/dolfik1/funogram/blob/master/README.md Initializes the bot configuration by reading the API token from a file named 'token'. If the file is not found, it prompts the user for input and saves it. ```fsharp let config = Config.defaultConfig |> Config.withReadTokenFromFile ``` -------------------------------- ### Open Necessary Namespaces Source: https://github.com/dolfik1/funogram/blob/master/README.md Imports the required Funogram modules for bot development. Ensure these are present at the beginning of your F# script or project. ```fsharp open Funogram.Api open Funogram.Telegram open Funogram.Telegram.Bot ``` -------------------------------- ### Process Commands with Arguments in F# Source: https://github.com/dolfik1/funogram/blob/master/README.md Use `processCommands` with `cmd` for commands without arguments and `cmdScan` for commands with arguments. `cmdScan` extracts arguments similar to `sprintf`. Commands can include the bot's username. ```fsharp let updateArrived (ctx: UpdateContext) = processCommands ctx [ cmd "/start" (fun _ -> printfn "User invoked /start command!") cmdScan "/say %s" (fun text _ -> printfn "User invoked say command with text %s" text) cmdScan "/sum %i %i" (fun a b _ -> printfn "Sum of %i and %i is %i" a b (a + b)) ] |> ignore ``` -------------------------------- ### Configure Bot Token Manually Source: https://github.com/dolfik1/funogram/blob/master/README.md Sets the bot's API token directly within the configuration object. Use this if you prefer not to use files or environment variables for your token. ```fsharp let config = { Config.defaultConfig with Token = "mysecrettoken" } ``` -------------------------------- ### Set Webhook Endpoint in F# Source: https://github.com/dolfik1/funogram/blob/master/README.md Configure Funogram to receive updates via webhooks by setting the webhook endpoint using `setWebhookBase`. This requires admin privileges and a public endpoint, potentially using a service like ngrok for local testing. ```fsharp let! hook = setWebhookBase webSocketEndpoint None None None |> api config ``` -------------------------------- ### Default Bot Configuration Source: https://github.com/dolfik1/funogram/blob/master/README.md Shows the default configuration structure for a Funogram bot, including token, update polling settings, and error handling. This is the base configuration that can be modified. ```fsharp let defaultConfig = { Token = "" Offset = Some 0L Limit = Some 100 Timeout = Some 60000 AllowedUpdates = None Client = new HttpClient() ApiEndpointUrl = Uri("https://api.telegram.org/bot") WebHook = None OnError = (fun e -> printfn "%A" e) } ``` -------------------------------- ### Send Reply Message Source: https://github.com/dolfik1/funogram/blob/master/README.md An alternative way to send a reply message using the `Req.SendMessage.Make` function. This is functionally equivalent to `Api.sendMessageReply` for this use case. ```fsharp Api.sendMessageReply chat.Id "Hello, world!" messageId |> api ctx.Config |> Async.Ignore |> Async.Start ``` ```fsharp Req.SendMessage.Make(chat.Id, text = "Hello, world!", replyToMessageId = messageId) |> api ctx.Config |> Async.Ignore |> Async.Start ``` -------------------------------- ### Delete Webhook Configuration Source: https://github.com/dolfik1/funogram/blob/master/README.md Invokes the `deleteWebhookBase` function to reset the bot's webhook configuration. This is recommended to ensure reliable updates, especially when switching between webhook and polling methods. ```fsharp let! _ = Api.deleteWebhookBase () |> api config ``` -------------------------------- ### Handle Incoming Messages Source: https://github.com/dolfik1/funogram/blob/master/README.md Defines a function to process incoming Telegram updates. This handler checks for messages and replies with 'Hello, world!' using the message ID and chat information. ```fsharp let updateArrived (ctx: UpdateContext) = match ctx.Update.Message with | Some { MessageId = messageId; Chat = chat } -> Api.sendMessageReply chat.Id "Hello, world!" messageId |> api ctx.Config |> Async.Ignore |> Async.Start | _ -> () ``` -------------------------------- ### F# Record Type for GetUpdates Request Source: https://github.com/dolfik1/funogram/blob/master/README.md Defines the F# record type for the 'getUpdates' API method, including optional parameters and a static factory method. This type implements the IRequestBase interface. ```fsharp type GetUpdates = { Offset: int64 option Limit: int64 option Timeout: int64 option AllowedUpdates: string[] option } static member Make(?offset: int64, ?limit: int64, ?timeout: int64, ?allowedUpdates: string[]) = { Offset = offset Limit = limit Timeout = timeout AllowedUpdates = allowedUpdates } interface IRequestBase with member _.MethodName = "getUpdates" ``` -------------------------------- ### F# Record Type for PollOption Response Source: https://github.com/dolfik1/funogram/blob/master/README.md Defines the F# record type for a 'PollOption' in a Telegram API response. It includes fields for text and voter count, marked with DataMember attributes for serialization. A static factory method is provided. ```fsharp /// This object contains information about one answer option in a poll. and [] PollOption = { /// Option text, 1-100 characters [] Text: string /// Number of users that voted for this option [] VoterCount: int64 } static member Create(text: string, voterCount: int64) = { Text = text VoterCount = voterCount } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.