Ignore obsolete messages

This commit is contained in:
Keroosha 2023-02-07 22:10:16 +03:00
parent 1ebcfa9788
commit c317e0b973
2 changed files with 15 additions and 1 deletions

View File

@ -11,6 +11,7 @@ type HandlerArgs = Update * BotConfig * ITelegramBotClient
type HandlerRequirements = BaseHandlerArgs -> bool type HandlerRequirements = BaseHandlerArgs -> bool
type Handler = HandlerArgs -> Task type Handler = HandlerArgs -> Task
// Utils
let UpdateIsAMessage (x: Update) = x.Type = UpdateType.Message let UpdateIsAMessage (x: Update) = x.Type = UpdateType.Message
let FromAdminChat (x: Message, c: BotConfig) = x.Chat.Id = c.adminChatId let FromAdminChat (x: Message, c: BotConfig) = x.Chat.Id = c.adminChatId
let HasReply (x: Message) = not(isNull x.ReplyToMessage) let HasReply (x: Message) = not(isNull x.ReplyToMessage)
@ -19,7 +20,6 @@ let UrlsAsAlbumInputMedia (urls: string[]): IAlbumInputMedia[] =
Array.map (fun (x: string) -> InputMediaPhoto(x)) urls Array.map (fun (x: string) -> InputMediaPhoto(x)) urls
// Post (Relay) command // Post (Relay) command
type RelayCaptionMode = WithAuthor | Anonymous | Unknown type RelayCaptionMode = WithAuthor | Anonymous | Unknown
let RelaySupportedContent (x: Message) = let RelaySupportedContent (x: Message) =
match x.Type with match x.Type with
@ -65,3 +65,13 @@ let public RelayHandler: Handler = fun (u, c, tg) ->
| MessageType.Video -> tg.SendVideoAsync(channelId, reply.Video.FileId, caption = caption.Value, | MessageType.Video -> tg.SendVideoAsync(channelId, reply.Video.FileId, caption = caption.Value,
parseMode = RelayParseMode) parseMode = RelayParseMode)
| _ -> Task.CompletedTask | _ -> Task.CompletedTask
// Youtube repost
let public YoutubeRepostMatch: HandlerRequirements = fun (u, c) ->
UpdateIsAMessage u &&
FromAdminChat <| (u.Message, c) &&
u.Message.Text.StartsWith("\\yt") &&
u.Message.Text.Split(' ').Length = 2

View File

@ -14,9 +14,12 @@ open Telegram.Bot.Types.Enums
let CreateBot (config: BotConfig, http: HttpClient) = TelegramBotClient(config.token, http) let CreateBot (config: BotConfig, http: HttpClient) = TelegramBotClient(config.token, http)
let config = CreateConfig <| "SBPB_CONFIG_PATH"; let config = CreateConfig <| "SBPB_CONFIG_PATH";
let botClient = CreateBot <| (config, new HttpClient()) let botClient = CreateBot <| (config, new HttpClient())
let startDate = DateTime.Now
let updateHandle (bc: ITelegramBotClient) (u: Update) (ct: CancellationToken): Task = let updateHandle (bc: ITelegramBotClient) (u: Update) (ct: CancellationToken): Task =
match u with match u with
// Ignore old messages (to prevent overposting on downtime)
| _ when u.Message.Date < startDate -> Task.CompletedTask
| _ when RelayMatch <| (u, config) -> RelayHandler <| (u, config, bc) | _ when RelayMatch <| (u, config) -> RelayHandler <| (u, config, bc)
| _ -> Task.CompletedTask | _ -> Task.CompletedTask
@ -28,4 +31,5 @@ let receiverOptions = ReceiverOptions(AllowedUpdates = Array.zeroCreate<UpdateTy
botClient.StartReceiving(updateHandle,handlePollingError,receiverOptions) botClient.StartReceiving(updateHandle,handlePollingError,receiverOptions)
printf родился"
Console.ReadKey() |> ignore Console.ReadKey() |> ignore