diff --git a/src/VahterBanBot.Tests/MLBanTests.fs b/src/VahterBanBot.Tests/MLBanTests.fs index 0ab5909..d380587 100644 --- a/src/VahterBanBot.Tests/MLBanTests.fs +++ b/src/VahterBanBot.Tests/MLBanTests.fs @@ -236,6 +236,18 @@ type MLBanTests(fixture: VahterTestContainers, _unused: MlAwaitFixture) = let! msgBanned = fixture.MessageIsAutoDeleted msgUpdate.Message Assert.True msgBanned } + + [] + let ``Message which were edited triggers auto-delete`` () = task { + // record a message, which originally was not a spam, + // but it was edited to be a spam + let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0], text = "a", editedText = "2222222") + let! _ = fixture.SendMessage msgUpdate + + // assert that the message got auto banned + let! msgBanned = fixture.MessageIsAutoDeleted msgUpdate.Message + Assert.True msgBanned + } interface IAssemblyFixture interface IClassFixture diff --git a/src/VahterBanBot.Tests/TgMessageUtils.fs b/src/VahterBanBot.Tests/TgMessageUtils.fs index 6ab2921..ad97c34 100644 --- a/src/VahterBanBot.Tests/TgMessageUtils.fs +++ b/src/VahterBanBot.Tests/TgMessageUtils.fs @@ -32,19 +32,33 @@ type Tg() = ) ) - static member quickMsg (?text: string, ?chat: Chat, ?from: User, ?date: DateTime, ?callback: CallbackQuery, ?caption: string) = + static member quickMsg (?text: string, ?chat: Chat, ?from: User, ?date: DateTime, ?callback: CallbackQuery, ?caption: string, ?editedText: string) = + let updateId = next() + let msgId = next() Update( - Id = next(), + Id = updateId, Message = Message( - MessageId = next(), + MessageId = msgId, Text = (text |> Option.defaultValue (Guid.NewGuid().ToString())), Chat = (chat |> Option.defaultValue (Tg.chat())), From = (from |> Option.defaultValue (Tg.user())), Date = (date |> Option.defaultValue DateTime.UtcNow), Caption = (caption |> Option.defaultValue null), ReplyToMessage = null - ) + ), + EditedMessage = + if editedText |> Option.isSome then + Message( + MessageId = msgId, + Text = editedText.Value, + Chat = (chat |> Option.defaultValue (Tg.chat())), + From = (from |> Option.defaultValue (Tg.user())), + Date = (date |> Option.defaultValue DateTime.UtcNow), + Caption = (caption |> Option.defaultValue null), + ReplyToMessage = null + ) + else null ) static member replyMsg (msg: Message, ?text: string, ?from: User, ?date: DateTime) = diff --git a/src/VahterBanBot/Bot.fs b/src/VahterBanBot/Bot.fs index c9f1c9d..65d4e5a 100644 --- a/src/VahterBanBot/Bot.fs +++ b/src/VahterBanBot/Bot.fs @@ -746,5 +746,5 @@ let onUpdate if update.CallbackQuery <> null then do! onCallback botClient botConfig logger update.CallbackQuery else - do! onMessage botUser botClient botConfig logger ml update.Message + do! onMessage botUser botClient botConfig logger ml update.EditedOrMessage } diff --git a/src/VahterBanBot/Utils.fs b/src/VahterBanBot/Utils.fs index a2de58e..8deb0cd 100644 --- a/src/VahterBanBot/Utils.fs +++ b/src/VahterBanBot/Utils.fs @@ -63,3 +63,10 @@ type Telegram.Bot.Types.Message with msg.Caption else msg.Text + +type Telegram.Bot.Types.Update with + member msg.EditedOrMessage = + if isNull msg.EditedMessage then + msg.Message + else + msg.EditedMessage