Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pkg/channels/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,13 +1153,10 @@ func (c *TelegramChannel) handleMessages(ctx context.Context, messages []*telego
content = c.prependTelegramQuotedReply(content, message.ReplyToMessage)
}

// For forum topics, embed the thread ID as "chatID/threadID" so replies
// route to the correct topic and each topic gets its own session.
// Only forum groups (IsForum) are handled; regular group reply threads
// must share one session per group.
compositeChatID := fmt.Sprintf("%d", chatID)
threadID := message.MessageThreadID
if message.Chat.IsForum && threadID != 0 {
isTopic := threadID != 0 && (message.Chat.IsForum || message.IsTopicMessage)
if isTopic {
compositeChatID = fmt.Sprintf("%d/%d", chatID, threadID)
}

Expand Down Expand Up @@ -1192,7 +1189,7 @@ func (c *TelegramChannel) handleMessages(ctx context.Context, messages []*telego
Mentioned: isMentioned,
Raw: metadata,
}
if message.Chat.IsForum && threadID != 0 {
if isTopic {
inboundCtx.TopicID = fmt.Sprintf("%d", threadID)
}
if message.ReplyToMessage != nil {
Expand Down
91 changes: 91 additions & 0 deletions pkg/channels/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,33 @@ func TestSend_WithForumThreadID(t *testing.T) {
assert.Len(t, caller.calls, 1)
}

func TestSend_WithPrivateTopicThreadID(t *testing.T) {
caller := &stubCaller{
callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) {
return successResponse(t), nil
},
}
ch := newTestChannel(t, caller)

_, err := ch.Send(context.Background(), bus.OutboundMessage{
ChatID: "123456789/42",
Content: "Hello from private topic",
})

require.NoError(t, err)
require.Len(t, caller.calls, 1)

var params struct {
ChatID int64 `json:"chat_id"`
MessageThreadID int `json:"message_thread_id"`
Text string `json:"text"`
}
require.NoError(t, json.Unmarshal(caller.calls[0].Data.BodyRaw, &params))
assert.Equal(t, int64(123456789), params.ChatID)
assert.Equal(t, 42, params.MessageThreadID)
assert.Equal(t, "Hello from private topic", params.Text)
}

func TestSend_UsesContextTopicIDWhenChatIDDoesNotIncludeThread(t *testing.T) {
caller := &stubCaller{
callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) {
Expand Down Expand Up @@ -1248,6 +1275,70 @@ func TestHandleMessage_ForumTopic_SetsMetadata(t *testing.T) {
assert.Equal(t, "42", inbound.Context.TopicID)
}

func TestHandleMessage_PrivateTopic_SetsMetadata(t *testing.T) {
messageBus := bus.NewMessageBus()
ch := &TelegramChannel{
BaseChannel: channels.NewBaseChannel("telegram", nil, messageBus, nil),
chatIDs: make(map[string]int64),
ctx: context.Background(),
}

msg := &telego.Message{
Text: "hello from private topic",
MessageID: 12,
MessageThreadID: 42,
IsTopicMessage: true,
Chat: telego.Chat{
ID: 123456789,
Type: "private",
},
From: &telego.User{
ID: 7,
FirstName: "Alice",
},
}

err := ch.handleMessage(context.Background(), msg)
require.NoError(t, err)

inbound, ok := <-messageBus.InboundChan()
require.True(t, ok)
assert.Equal(t, "123456789/42", inbound.ChatID)
assert.Equal(t, "direct", inbound.Context.ChatType)
assert.Equal(t, "42", inbound.Context.TopicID)
}

func TestHandleMessage_PrivateChat_NoTopicMetadata(t *testing.T) {
messageBus := bus.NewMessageBus()
ch := &TelegramChannel{
BaseChannel: channels.NewBaseChannel("telegram", nil, messageBus, nil),
chatIDs: make(map[string]int64),
ctx: context.Background(),
}

msg := &telego.Message{
Text: "hello from private chat",
MessageID: 13,
Chat: telego.Chat{
ID: 123456789,
Type: "private",
},
From: &telego.User{
ID: 7,
FirstName: "Alice",
},
}

err := ch.handleMessage(context.Background(), msg)
require.NoError(t, err)

inbound, ok := <-messageBus.InboundChan()
require.True(t, ok)
assert.Equal(t, "123456789", inbound.ChatID)
assert.Equal(t, "direct", inbound.Context.ChatType)
assert.Empty(t, inbound.Context.TopicID)
}

func TestHandleMessage_NoForum_NoThreadMetadata(t *testing.T) {
messageBus := bus.NewMessageBus()
ch := &TelegramChannel{
Expand Down
33 changes: 33 additions & 0 deletions pkg/session/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ func TestAllocateRouteSession_TelegramForumTopicsRemainIsolatedByDefault(t *test
}
}

func TestAllocateRouteSession_TelegramPrivateTopicsRemainIsolatedByDefault(t *testing.T) {
first := AllocateRouteSession(AllocationInput{
AgentID: "main",
Context: bus.InboundContext{
Channel: "telegram",
ChatID: "123456789/42",
ChatType: "direct",
TopicID: "42",
SenderID: "7",
},
SessionPolicy: routing.SessionPolicy{
Dimensions: []string{"chat"},
},
})
second := AllocateRouteSession(AllocationInput{
AgentID: "main",
Context: bus.InboundContext{
Channel: "telegram",
ChatID: "123456789/99",
ChatType: "direct",
TopicID: "99",
SenderID: "7",
},
SessionPolicy: routing.SessionPolicy{
Dimensions: []string{"chat"},
},
})

if first.SessionKey == second.SessionKey {
t.Fatalf("private topics should not share default session key: %q", first.SessionKey)
}
}

func TestAllocateRouteSession_PicoDirectAliasesIncludeLegacyChatKey(t *testing.T) {
allocation := AllocateRouteSession(AllocationInput{
AgentID: "main",
Expand Down