Initial commit

This commit is contained in:
Keroosha 2023-02-07 19:48:09 +03:00
commit 0663a2b94e
10 changed files with 358 additions and 0 deletions

View file

@ -0,0 +1,51 @@
// For more information see https://aka.ms/fsharp-console-apps
open System
open System.IO
open System.Net.Http
open System.Threading
open System.Threading.Tasks
open Microsoft.FSharp.Control
open Newtonsoft.Json
open Telegram.Bot
open Telegram.Bot.Polling
open Telegram.Bot.Types
open Telegram.Bot.Types.Enums
type BotConfig = {
token: string
chanelId: int64
adminChatId: int64
}
let ReadConfig =
File.ReadAllText >> JsonConvert.DeserializeObject<BotConfig>
let CreateConfig (name: string) =
match Environment.GetEnvironmentVariable(name) with
| null -> raise <| ApplicationException("Missing config path env")
| path -> ReadConfig <| path
let CreateBot (config: BotConfig, http: HttpClient) =
TelegramBotClient(config.token, http)
let config = CreateConfig <| "SBPB_CONFIG_PATH";
let botClient = CreateBot <| (config, new HttpClient())
let shouldReply (u: Update) =
u.Type = UpdateType.Message && u.Message.Chat.Id = config.adminChatId
let updateHandle (bc: ITelegramBotClient) (u: Update) (ct: CancellationToken): Task =
match u with
| _ when shouldReply u -> bc.SendTextMessageAsync(config.chanelId, u.Message.Text, cancellationToken = ct)
| _ -> Task.CompletedTask
let handlePollingError (bc: ITelegramBotClient) (e: Exception) (t: CancellationToken) =
printfn $"{e.Message}\n{e.StackTrace}"
Task.CompletedTask
let receiverOptions = ReceiverOptions(AllowedUpdates = Array.zeroCreate<UpdateType> 0)
botClient.StartReceiving(updateHandle,handlePollingError,receiverOptions)
Console.ReadKey() |> ignore

View file

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="18.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.fs" />
<Content Include="config.dev.json" />
<Content Include="config.example.json" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,5 @@
{
"token": "abba",
"chanelId": "123123123",
"adminChatId": "123123123"
}