From 3500590f11bf5c218999b1aba5c2de5fe48ceb2a Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 30 Mar 2026 17:05:42 +0300 Subject: [PATCH] handlematrix: fix reading send response --- pkg/connector/handlematrix.go | 38 +++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/pkg/connector/handlematrix.go b/pkg/connector/handlematrix.go index 13ccafde..751f2435 100644 --- a/pkg/connector/handlematrix.go +++ b/pkg/connector/handlematrix.go @@ -404,17 +404,47 @@ func (t *TelegramClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2. tgDate = sentMessage.Date hasher.Write([]byte(msg.Content.Body)) case *tg.Updates: - tgDate = sentMessage.Date + var realSentMessage *tg.Message for _, u := range sentMessage.Updates { switch update := u.(type) { case *tg.UpdateMessageID: tgMessageID = update.ID + if update.RandomID != randomID { + log.Warn(). + Int64("update_random_id", update.RandomID). + Int64("expected_random_id", randomID). + Msg("Random ID in response does not match sent random ID") + } case *tg.UpdateNewMessage: - msg := update.Message.(*tg.Message) - hasher.Write([]byte(msg.Message)) - hasher.Write(mediaHashID(ctx, msg.Media)) + if realSentMessage != nil { + log.Warn(). + Int("prev_id", realSentMessage.ID). + Int("new_id", update.Message.GetID()). + Msg("Multiple messages in send response") + } + realSentMessage = update.Message.(*tg.Message) + case *tg.UpdateNewChannelMessage: + if realSentMessage != nil { + log.Warn(). + Int("prev_id", realSentMessage.ID). + Int("new_id", update.Message.GetID()). + Msg("Multiple messages in send response") + } + realSentMessage = update.Message.(*tg.Message) + case *tg.UpdateReadChannelInbox, *tg.UpdateReadHistoryInbox, *tg.UpdateReadMonoForumInbox: + // ignore + default: + log.Warn().Type("update_type", update).Msg("Unexpected update type in send message response") } } + if realSentMessage != nil { + tgDate = realSentMessage.Date + hasher.Write([]byte(realSentMessage.Message)) + hasher.Write(mediaHashID(ctx, realSentMessage.Media)) + } else { + hasher.Write([]byte(msg.Content.Body)) + tgDate = sentMessage.Date + } if tgMessageID == 0 { return nil, fmt.Errorf("couldn't find update message ID update") }