Skip to content

Commit 3a2e076

Browse files
authored
Handle message_reaction updates to fix NRE (#93)
* Handle message_reaction updates to fix NRE When receiving message_reaction updates from Telegram, both Message and EditedMessage are null, causing NRE when accessing EditedOrMessage.Photo. - Add onMessageReaction function that logs reaction details as a span - Update onUpdate to check for MessageReaction before processing OCR/message - Add Tg.quickReaction test helper - Add test to verify bot returns 200 OK on reaction updates * crlf * fix
1 parent d725abf commit 3a2e076

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*]
2+
end_of_line = lf
3+
insert_final_newline = true

.gitattributes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
* text=auto eol=lf
2+
3+
*.bat text eol=crlf
4+
*.cmd text eol=crlf
5+
*.ps1 text eol=crlf
6+
*.psm1 text eol=crlf
7+
*.psd1 text eol=crlf
8+
9+
*.sln text eol=crlf
10+
11+
*.png binary
12+
*.jpg binary
13+
*.jpeg binary
14+
*.gif binary
15+
*.webp binary
16+
*.zip binary
17+
*.pdf binary

src/VahterBanBot.Tests/MessageTests.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,21 @@ type MessageTests(fixture: MlDisabledVahterTestContainers) =
5959

6060
Assert.Null(dbMsg.Value.text)
6161
}
62+
63+
[<Fact>]
64+
let ``Message reaction update returns OK`` () = task {
65+
// first send a message to have a valid message_id
66+
let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0])
67+
let! _ = fixture.SendMessage msgUpdate
68+
69+
// send a reaction update for that message
70+
let reactionUpdate = Tg.quickReaction(
71+
chat = fixture.ChatsToMonitor[0],
72+
messageId = msgUpdate.Message.MessageId,
73+
from = Tg.user()
74+
)
75+
let! resp = fixture.SendMessage reactionUpdate
76+
Assert.Equal(HttpStatusCode.OK, resp.StatusCode)
77+
}
6278

6379
interface IAssemblyFixture<MlDisabledVahterTestContainers>

src/VahterBanBot.Tests/TgMessageUtils.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ type Tg() =
8282
)
8383
)
8484

85+
static member quickReaction(chat: Chat, messageId: int, from: User, ?emoji: string) =
86+
let reactionEmoji = emoji |> Option.defaultValue "👍"
87+
Update(
88+
Id = next(),
89+
MessageReaction = MessageReactionUpdated(
90+
Chat = chat,
91+
MessageId = messageId,
92+
User = from,
93+
Date = DateTime.UtcNow,
94+
OldReaction = [||],
95+
NewReaction = [| ReactionTypeEmoji(Emoji = reactionEmoji) |]
96+
)
97+
)
98+
8599
static member spamPhoto =
86100
PhotoSize(
87101
FileId = "spam",

src/VahterBanBot/Bot.fs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module VahterBanBot.Bot
1+
module VahterBanBot.Bot
22

33
open System
44
open System.Diagnostics
@@ -848,6 +848,24 @@ let onCallback
848848
do! botClient.AnswerCallbackQueryAsync(callbackQuery.Id)
849849
}
850850

851+
let onMessageReaction (logger: ILogger) (reaction: MessageReactionUpdated) =
852+
use _ =
853+
botActivity
854+
.StartActivity("messageReaction")
855+
.SetTag("chatId", reaction.Chat.Id)
856+
.SetTag("chatUsername", reaction.Chat.Username)
857+
.SetTag("messageId", reaction.MessageId)
858+
.SetTag("userId", reaction.User.Id)
859+
.SetTag("userUsername", reaction.User.Username)
860+
logger.LogInformation(
861+
"Reaction from {Username} ({UserId}) on message {MessageId} in {ChatUsername} ({ChatId})",
862+
reaction.User.Username,
863+
reaction.User.Id,
864+
reaction.MessageId,
865+
reaction.Chat.Username,
866+
reaction.Chat.Id
867+
)
868+
851869
let onUpdate
852870
(botUser: DbUser)
853871
(botClient: ITelegramBotClient)
@@ -859,6 +877,8 @@ let onUpdate
859877
use _ = botActivity.StartActivity("onUpdate")
860878
if update.CallbackQuery <> null then
861879
do! onCallback botClient botConfig logger update.CallbackQuery
880+
elif update.MessageReaction <> null then
881+
onMessageReaction logger update.MessageReaction
862882
else
863883
do! tryEnrichMessageWithOcr botClient botConfig computerVision logger update
864884
do! onMessage botUser botClient botConfig logger ml update.EditedOrMessage

0 commit comments

Comments
 (0)