fix: remove redundant SplitMessage in Send() per review feedback
WithMaxMessageLength(4000) already ensures msg.Content ≤ 4000 chars before reaching Send(), making the SplitMessage call redundant. The HTML expansion safety net (re-split when >4096 after conversion) is still preserved. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
11017ac7ba
commit
f07dbd1db2
2 changed files with 11 additions and 12 deletions
|
|
@ -237,14 +237,10 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the raw markdown before converting to HTML so that
|
// The Manager already splits messages to ≤4000 chars (WithMaxMessageLength),
|
||||||
// SplitMessage's code-fence-aware logic works correctly and
|
// so msg.Content is guaranteed to be within that limit. We still need to
|
||||||
// we never break HTML tags/entities by splitting converted output.
|
// check if HTML expansion pushes it beyond Telegram's 4096-char API limit.
|
||||||
mdChunks := channels.SplitMessage(msg.Content, 4000)
|
queue := []string{msg.Content}
|
||||||
|
|
||||||
// Use a queue so that chunks whose HTML expansion still exceeds
|
|
||||||
// Telegram's 4096-char limit can be re-split until every chunk fits.
|
|
||||||
queue := append([]string{}, mdChunks...)
|
|
||||||
for len(queue) > 0 {
|
for len(queue) > 0 {
|
||||||
chunk := queue[0]
|
chunk := queue[0]
|
||||||
queue = queue[1:]
|
queue = queue[1:]
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,11 @@ func TestSend_ShortMessage_SingleCall(t *testing.T) {
|
||||||
assert.Len(t, caller.calls, 1, "short message should result in exactly one SendMessage call")
|
assert.Len(t, caller.calls, 1, "short message should result in exactly one SendMessage call")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSend_LongMessage_MultipleCalls(t *testing.T) {
|
func TestSend_LongMessage_SingleCall(t *testing.T) {
|
||||||
|
// With WithMaxMessageLength(4000), the Manager pre-splits messages before
|
||||||
|
// they reach Send(). A message at exactly 4000 chars should go through
|
||||||
|
// as a single SendMessage call (no re-split needed since HTML expansion
|
||||||
|
// won't exceed 4096 for plain text).
|
||||||
caller := &stubCaller{
|
caller := &stubCaller{
|
||||||
callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) {
|
callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) {
|
||||||
return successResponse(t), nil
|
return successResponse(t), nil
|
||||||
|
|
@ -123,8 +127,7 @@ func TestSend_LongMessage_MultipleCalls(t *testing.T) {
|
||||||
}
|
}
|
||||||
ch := newTestChannel(t, caller)
|
ch := newTestChannel(t, caller)
|
||||||
|
|
||||||
// Create a message over 4000 chars so it gets split into multiple chunks.
|
longContent := strings.Repeat("a", 4000)
|
||||||
longContent := strings.Repeat("a", 4001)
|
|
||||||
|
|
||||||
err := ch.Send(context.Background(), bus.OutboundMessage{
|
err := ch.Send(context.Background(), bus.OutboundMessage{
|
||||||
ChatID: "12345",
|
ChatID: "12345",
|
||||||
|
|
@ -132,7 +135,7 @@ func TestSend_LongMessage_MultipleCalls(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Greater(t, len(caller.calls), 1, "long message should be split into multiple SendMessage calls")
|
assert.Len(t, caller.calls, 1, "pre-split message within limit should result in one SendMessage call")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSend_HTMLFallback_PerChunk(t *testing.T) {
|
func TestSend_HTMLFallback_PerChunk(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue