From c317e0b973c23bb36c14ff1599066859b556b276 Mon Sep 17 00:00:00 2001 From: Keroosha Date: Tue, 7 Feb 2023 22:10:16 +0300 Subject: [PATCH] Ignore obsolete messages --- PublishHelperBot/Handlers.fs | 12 +++++++++++- PublishHelperBot/Program.fs | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/PublishHelperBot/Handlers.fs b/PublishHelperBot/Handlers.fs index ceb93a4..e400398 100644 --- a/PublishHelperBot/Handlers.fs +++ b/PublishHelperBot/Handlers.fs @@ -11,6 +11,7 @@ type HandlerArgs = Update * BotConfig * ITelegramBotClient type HandlerRequirements = BaseHandlerArgs -> bool type Handler = HandlerArgs -> Task +// Utils let UpdateIsAMessage (x: Update) = x.Type = UpdateType.Message let FromAdminChat (x: Message, c: BotConfig) = x.Chat.Id = c.adminChatId 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 // Post (Relay) command - type RelayCaptionMode = WithAuthor | Anonymous | Unknown let RelaySupportedContent (x: Message) = 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, parseMode = RelayParseMode) | _ -> 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 + \ No newline at end of file diff --git a/PublishHelperBot/Program.fs b/PublishHelperBot/Program.fs index b6507d1..16231a6 100644 --- a/PublishHelperBot/Program.fs +++ b/PublishHelperBot/Program.fs @@ -14,9 +14,12 @@ open Telegram.Bot.Types.Enums let CreateBot (config: BotConfig, http: HttpClient) = TelegramBotClient(config.token, http) let config = CreateConfig <| "SBPB_CONFIG_PATH"; let botClient = CreateBot <| (config, new HttpClient()) +let startDate = DateTime.Now let updateHandle (bc: ITelegramBotClient) (u: Update) (ct: CancellationToken): Task = 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) | _ -> Task.CompletedTask @@ -28,4 +31,5 @@ let receiverOptions = ReceiverOptions(AllowedUpdates = Array.zeroCreate ignore \ No newline at end of file