fix(telegram): update Send parameters to properly proxy OutboundMessage to SendMessageWithID
This commit is contained in:
parent
0b65f7d31c
commit
eeabdd098f
2 changed files with 32 additions and 32 deletions
|
|
@ -164,29 +164,29 @@ func (c *TelegramChannel) Stop(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
|
func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
|
||||||
_, err := c.SendMessageWithID(ctx, msg.ChatID, msg.Content)
|
_, err := c.SendMessageWithID(ctx, msg)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendMessageWithID implements an optional interface for AgentLoop to send a message synchronously and get the MessageID.
|
// SendMessageWithID implements an optional interface for AgentLoop to send a message synchronously and get the MessageID.
|
||||||
func (c *TelegramChannel) SendMessageWithID(ctx context.Context, chatID string, content string) (string, error) {
|
func (c *TelegramChannel) SendMessageWithID(ctx context.Context, msg bus.OutboundMessage) (string, error) {
|
||||||
if !c.IsRunning() {
|
if !c.IsRunning() {
|
||||||
return "", channels.ErrNotRunning
|
return "", channels.ErrNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
cid, err := parseChatID(chatID)
|
cid, err := parseChatID(msg.ChatID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("invalid chat ID %s: %w", chatID, channels.ErrSendFailed)
|
return "", fmt.Errorf("invalid chat ID %s: %w", msg.ChatID, channels.ErrSendFailed)
|
||||||
}
|
}
|
||||||
|
|
||||||
if content == "" {
|
if msg.Content == "" {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Manager already splits messages to ≤4000 chars (WithMaxMessageLength),
|
// The Manager already splits messages to ≤4000 chars (WithMaxMessageLength),
|
||||||
// so msg.Content is guaranteed to be within that limit. We still need to
|
// so msg.Content is guaranteed to be within that limit. We still need to
|
||||||
// check if HTML expansion pushes it beyond Telegram's 4096-char API limit.
|
// check if HTML expansion pushes it beyond Telegram's 4096-char API limit.
|
||||||
queue := []string{content}
|
queue := []string{msg.Content}
|
||||||
var lastMsgID int
|
var lastMsgID int
|
||||||
|
|
||||||
for len(queue) > 0 {
|
for len(queue) > 0 {
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,19 @@
|
||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mymmrac/telego"
|
"github.com/mymmrac/telego"
|
||||||
ta "github.com/mymmrac/telego/telegoapi"
|
ta "github.com/mymmrac/telego/telegoapi"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/bus"
|
"github.com/sipeed/picoclaw/pkg/bus"
|
||||||
"github.com/sipeed/picoclaw/pkg/channels"
|
"github.com/sipeed/picoclaw/pkg/channels"
|
||||||
)
|
)
|
||||||
|
|
||||||
const testToken = "1234567890:aaaabbbbaaaabbbbaaaabbbbaaaabbbbccc"
|
const testToken = "1234567890:aaaabbbbaaaabbbbaaaabbbbaaaabbbbccc"
|
||||||
|
|
@ -42,8 +42,8 @@ func (s *stubConstructor) JSONRequest(parameters any) (*ta.RequestData, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stubConstructor) MultipartRequest(
|
func (s *stubConstructor) MultipartRequest(
|
||||||
parameters map[string]string,
|
parameters map[string]string,
|
||||||
files map[string]ta.NamedReader,
|
files map[string]ta.NamedReader,
|
||||||
) (*ta.RequestData, error) {
|
) (*ta.RequestData, error) {
|
||||||
return &ta.RequestData{}, nil
|
return &ta.RequestData{}, nil
|
||||||
}
|
}
|
||||||
|
|
@ -62,15 +62,15 @@ func newTestChannel(t *testing.T, caller *stubCaller) *TelegramChannel {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
bot, err := telego.NewBot(testToken,
|
bot, err := telego.NewBot(testToken,
|
||||||
telego.WithAPICaller(caller),
|
telego.WithAPICaller(caller),
|
||||||
telego.WithRequestConstructor(&stubConstructor{}),
|
telego.WithRequestConstructor(&stubConstructor{}),
|
||||||
telego.WithDiscardLogger(),
|
telego.WithDiscardLogger(),
|
||||||
)
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
base := channels.NewBaseChannel("telegram", nil, nil, nil,
|
base := channels.NewBaseChannel("telegram", nil, nil, nil,
|
||||||
channels.WithMaxMessageLength(4000),
|
channels.WithMaxMessageLength(4000),
|
||||||
)
|
)
|
||||||
base.SetRunning(true)
|
base.SetRunning(true)
|
||||||
|
|
||||||
return &TelegramChannel{
|
return &TelegramChannel{
|
||||||
|
|
@ -106,7 +106,7 @@ func TestSendMessageWithID_EmptyContent(t *testing.T) {
|
||||||
}
|
}
|
||||||
ch := newTestChannel(t, caller)
|
ch := newTestChannel(t, caller)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", "")
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: ""})
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Empty(t, msgID)
|
assert.Empty(t, msgID)
|
||||||
|
|
@ -121,7 +121,7 @@ func TestSendMessageWithID_ShortMessage_SingleCall(t *testing.T) {
|
||||||
}
|
}
|
||||||
ch := newTestChannel(t, caller)
|
ch := newTestChannel(t, caller)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", "Hello, world!")
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: "Hello, world!"})
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "1", msgID)
|
assert.Equal(t, "1", msgID)
|
||||||
|
|
@ -138,7 +138,7 @@ func TestSendMessageWithID_LongMessage_SingleCall(t *testing.T) {
|
||||||
|
|
||||||
longContent := strings.Repeat("a", 4000)
|
longContent := strings.Repeat("a", 4000)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", longContent)
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: longContent})
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "1", msgID)
|
assert.Equal(t, "1", msgID)
|
||||||
|
|
@ -158,7 +158,7 @@ func TestSendMessageWithID_HTMLFallback_PerChunk(t *testing.T) {
|
||||||
}
|
}
|
||||||
ch := newTestChannel(t, caller)
|
ch := newTestChannel(t, caller)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", "Hello **world**")
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: "Hello **world**"})
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "1", msgID)
|
assert.Equal(t, "1", msgID)
|
||||||
|
|
@ -173,7 +173,7 @@ func TestSendMessageWithID_HTMLFallback_BothFail(t *testing.T) {
|
||||||
}
|
}
|
||||||
ch := newTestChannel(t, caller)
|
ch := newTestChannel(t, caller)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", "Hello")
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: "Hello"})
|
||||||
|
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Empty(t, msgID)
|
assert.Empty(t, msgID)
|
||||||
|
|
@ -191,7 +191,7 @@ func TestSendMessageWithID_LongMessage_HTMLFallback_StopsOnError(t *testing.T) {
|
||||||
|
|
||||||
longContent := strings.Repeat("x", 4001)
|
longContent := strings.Repeat("x", 4001)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", longContent)
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: longContent})
|
||||||
|
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Empty(t, msgID)
|
assert.Empty(t, msgID)
|
||||||
|
|
@ -209,7 +209,7 @@ func TestSendMessageWithID_MarkdownShortButHTMLLong_MultipleCalls(t *testing.T)
|
||||||
markdownContent := strings.Repeat("**a** ", 600)
|
markdownContent := strings.Repeat("**a** ", 600)
|
||||||
assert.LessOrEqual(t, len([]rune(markdownContent)), 4000)
|
assert.LessOrEqual(t, len([]rune(markdownContent)), 4000)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", markdownContent)
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: markdownContent})
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "1", msgID)
|
assert.Equal(t, "1", msgID)
|
||||||
|
|
@ -226,7 +226,7 @@ func TestSendMessageWithID_NotRunning(t *testing.T) {
|
||||||
ch := newTestChannel(t, caller)
|
ch := newTestChannel(t, caller)
|
||||||
ch.SetRunning(false)
|
ch.SetRunning(false)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "12345", "Hello")
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "12345", Content: "Hello"})
|
||||||
|
|
||||||
assert.ErrorIs(t, err, channels.ErrNotRunning)
|
assert.ErrorIs(t, err, channels.ErrNotRunning)
|
||||||
assert.Empty(t, msgID)
|
assert.Empty(t, msgID)
|
||||||
|
|
@ -242,7 +242,7 @@ func TestSendMessageWithID_InvalidChatID(t *testing.T) {
|
||||||
}
|
}
|
||||||
ch := newTestChannel(t, caller)
|
ch := newTestChannel(t, caller)
|
||||||
|
|
||||||
msgID, err := ch.SendMessageWithID(context.Background(), "not-a-number", "Hello")
|
msgID, err := ch.SendMessageWithID(context.Background(), bus.OutboundMessage{ChatID: "not-a-number", Content: "Hello"})
|
||||||
|
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Empty(t, msgID)
|
assert.Empty(t, msgID)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue