mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-18 21:40:53 +00:00
* refactor: move Telegram channel implementation to extensions/telegram/src/ Move all Telegram channel code (123 files + 10 bot/ files + 8 channel plugin files) from src/telegram/ and src/channels/plugins/*/telegram.ts to extensions/telegram/src/. Leave thin re-export shims at original locations so cross-cutting src/ imports continue to resolve. - Fix all relative import paths in moved files (../X/ -> ../../../src/X/) - Fix vi.mock paths in 60 test files - Fix inline typeof import() expressions - Update tsconfig.plugin-sdk.dts.json rootDir to "." for cross-directory DTS - Update write-plugin-sdk-entry-dts.ts for new rootDir structure - Move channel plugin files with correct path remapping * fix: support keyed telegram send deps * fix: sync telegram extension copies with latest main * fix: correct import paths and remove misplaced files in telegram extension * fix: sync outbound-adapter with main (add sendTelegramPayloadMessages) and fix delivery.test import path
153 lines
5.2 KiB
TypeScript
153 lines
5.2 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import {
|
|
clearRuntimeConfigSnapshot,
|
|
setRuntimeConfigSnapshot,
|
|
} from "../../../src/config/config.js";
|
|
import { buildTelegramMessageContextForTest } from "./bot-message-context.test-harness.js";
|
|
|
|
describe("buildTelegramMessageContext dm thread sessions", () => {
|
|
const buildContext = async (message: Record<string, unknown>) =>
|
|
await buildTelegramMessageContextForTest({
|
|
message,
|
|
});
|
|
|
|
it("uses thread session key for dm topics", async () => {
|
|
const ctx = await buildContext({
|
|
message_id: 1,
|
|
chat: { id: 1234, type: "private" },
|
|
date: 1700000000,
|
|
text: "hello",
|
|
message_thread_id: 42,
|
|
from: { id: 42, first_name: "Alice" },
|
|
});
|
|
|
|
expect(ctx).not.toBeNull();
|
|
expect(ctx?.ctxPayload?.MessageThreadId).toBe(42);
|
|
expect(ctx?.ctxPayload?.SessionKey).toBe("agent:main:main:thread:1234:42");
|
|
});
|
|
|
|
it("keeps legacy dm session key when no thread id", async () => {
|
|
const ctx = await buildContext({
|
|
message_id: 2,
|
|
chat: { id: 1234, type: "private" },
|
|
date: 1700000001,
|
|
text: "hello",
|
|
from: { id: 42, first_name: "Alice" },
|
|
});
|
|
|
|
expect(ctx).not.toBeNull();
|
|
expect(ctx?.ctxPayload?.MessageThreadId).toBeUndefined();
|
|
expect(ctx?.ctxPayload?.SessionKey).toBe("agent:main:main");
|
|
});
|
|
});
|
|
|
|
describe("buildTelegramMessageContext group sessions without forum", () => {
|
|
const buildContext = async (message: Record<string, unknown>) =>
|
|
await buildTelegramMessageContextForTest({
|
|
message,
|
|
options: { forceWasMentioned: true },
|
|
resolveGroupActivation: () => true,
|
|
});
|
|
|
|
it("ignores message_thread_id for regular groups (not forums)", async () => {
|
|
// When someone replies to a message in a non-forum group, Telegram sends
|
|
// message_thread_id but this should NOT create a separate session
|
|
const ctx = await buildContext({
|
|
message_id: 1,
|
|
chat: { id: -1001234567890, type: "supergroup", title: "Test Group" },
|
|
date: 1700000000,
|
|
text: "@bot hello",
|
|
message_thread_id: 42, // This is a reply thread, NOT a forum topic
|
|
from: { id: 42, first_name: "Alice" },
|
|
});
|
|
|
|
expect(ctx).not.toBeNull();
|
|
// Session key should NOT include :topic:42
|
|
expect(ctx?.ctxPayload?.SessionKey).toBe("agent:main:telegram:group:-1001234567890");
|
|
// MessageThreadId should be undefined (not a forum)
|
|
expect(ctx?.ctxPayload?.MessageThreadId).toBeUndefined();
|
|
});
|
|
|
|
it("keeps same session for regular group with and without message_thread_id", async () => {
|
|
const ctxWithThread = await buildContext({
|
|
message_id: 1,
|
|
chat: { id: -1001234567890, type: "supergroup", title: "Test Group" },
|
|
date: 1700000000,
|
|
text: "@bot hello",
|
|
message_thread_id: 42,
|
|
from: { id: 42, first_name: "Alice" },
|
|
});
|
|
|
|
const ctxWithoutThread = await buildContext({
|
|
message_id: 2,
|
|
chat: { id: -1001234567890, type: "supergroup", title: "Test Group" },
|
|
date: 1700000001,
|
|
text: "@bot world",
|
|
from: { id: 42, first_name: "Alice" },
|
|
});
|
|
|
|
expect(ctxWithThread).not.toBeNull();
|
|
expect(ctxWithoutThread).not.toBeNull();
|
|
// Both messages should use the same session key
|
|
expect(ctxWithThread?.ctxPayload?.SessionKey).toBe(ctxWithoutThread?.ctxPayload?.SessionKey);
|
|
});
|
|
|
|
it("uses topic session for forum groups with message_thread_id", async () => {
|
|
const ctx = await buildContext({
|
|
message_id: 1,
|
|
chat: { id: -1001234567890, type: "supergroup", title: "Test Forum", is_forum: true },
|
|
date: 1700000000,
|
|
text: "@bot hello",
|
|
message_thread_id: 99,
|
|
from: { id: 42, first_name: "Alice" },
|
|
});
|
|
|
|
expect(ctx).not.toBeNull();
|
|
// Session key SHOULD include :topic:99 for forums
|
|
expect(ctx?.ctxPayload?.SessionKey).toBe("agent:main:telegram:group:-1001234567890:topic:99");
|
|
expect(ctx?.ctxPayload?.MessageThreadId).toBe(99);
|
|
});
|
|
});
|
|
|
|
describe("buildTelegramMessageContext direct peer routing", () => {
|
|
afterEach(() => {
|
|
clearRuntimeConfigSnapshot();
|
|
});
|
|
|
|
it("isolates dm sessions by sender id when chat id differs", async () => {
|
|
const runtimeCfg = {
|
|
agents: { defaults: { model: "anthropic/claude-opus-4-5", workspace: "/tmp/openclaw" } },
|
|
channels: { telegram: {} },
|
|
messages: { groupChat: { mentionPatterns: [] } },
|
|
session: { dmScope: "per-channel-peer" as const },
|
|
};
|
|
setRuntimeConfigSnapshot(runtimeCfg);
|
|
|
|
const baseMessage = {
|
|
chat: { id: 777777777, type: "private" as const },
|
|
date: 1700000000,
|
|
text: "hello",
|
|
};
|
|
|
|
const first = await buildTelegramMessageContextForTest({
|
|
cfg: runtimeCfg,
|
|
message: {
|
|
...baseMessage,
|
|
message_id: 1,
|
|
from: { id: 123456789, first_name: "Alice" },
|
|
},
|
|
});
|
|
const second = await buildTelegramMessageContextForTest({
|
|
cfg: runtimeCfg,
|
|
message: {
|
|
...baseMessage,
|
|
message_id: 2,
|
|
from: { id: 987654321, first_name: "Bob" },
|
|
},
|
|
});
|
|
|
|
expect(first?.ctxPayload?.SessionKey).toBe("agent:main:telegram:direct:123456789");
|
|
expect(second?.ctxPayload?.SessionKey).toBe("agent:main:telegram:direct:987654321");
|
|
});
|
|
});
|