* 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
90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
import type { OpenClawConfig } from "../../../src/config/config.js";
|
|
import type { TelegramGroupConfig } from "../../../src/config/types.telegram.js";
|
|
import { normalizeAccountId } from "../../../src/routing/session-key.js";
|
|
|
|
type TelegramGroups = Record<string, TelegramGroupConfig>;
|
|
|
|
type MigrationScope = "account" | "global";
|
|
|
|
export type TelegramGroupMigrationResult = {
|
|
migrated: boolean;
|
|
skippedExisting: boolean;
|
|
scopes: MigrationScope[];
|
|
};
|
|
|
|
function resolveAccountGroups(
|
|
cfg: OpenClawConfig,
|
|
accountId?: string | null,
|
|
): { groups?: TelegramGroups } {
|
|
if (!accountId) {
|
|
return {};
|
|
}
|
|
const normalized = normalizeAccountId(accountId);
|
|
const accounts = cfg.channels?.telegram?.accounts;
|
|
if (!accounts || typeof accounts !== "object") {
|
|
return {};
|
|
}
|
|
const exact = accounts[normalized];
|
|
if (exact?.groups) {
|
|
return { groups: exact.groups };
|
|
}
|
|
const matchKey = Object.keys(accounts).find(
|
|
(key) => key.toLowerCase() === normalized.toLowerCase(),
|
|
);
|
|
return { groups: matchKey ? accounts[matchKey]?.groups : undefined };
|
|
}
|
|
|
|
export function migrateTelegramGroupsInPlace(
|
|
groups: TelegramGroups | undefined,
|
|
oldChatId: string,
|
|
newChatId: string,
|
|
): { migrated: boolean; skippedExisting: boolean } {
|
|
if (!groups) {
|
|
return { migrated: false, skippedExisting: false };
|
|
}
|
|
if (oldChatId === newChatId) {
|
|
return { migrated: false, skippedExisting: false };
|
|
}
|
|
if (!Object.hasOwn(groups, oldChatId)) {
|
|
return { migrated: false, skippedExisting: false };
|
|
}
|
|
if (Object.hasOwn(groups, newChatId)) {
|
|
return { migrated: false, skippedExisting: true };
|
|
}
|
|
groups[newChatId] = groups[oldChatId];
|
|
delete groups[oldChatId];
|
|
return { migrated: true, skippedExisting: false };
|
|
}
|
|
|
|
export function migrateTelegramGroupConfig(params: {
|
|
cfg: OpenClawConfig;
|
|
accountId?: string | null;
|
|
oldChatId: string;
|
|
newChatId: string;
|
|
}): TelegramGroupMigrationResult {
|
|
const scopes: MigrationScope[] = [];
|
|
let migrated = false;
|
|
let skippedExisting = false;
|
|
|
|
const migrationTargets: Array<{
|
|
scope: MigrationScope;
|
|
groups: TelegramGroups | undefined;
|
|
}> = [
|
|
{ scope: "account", groups: resolveAccountGroups(params.cfg, params.accountId).groups },
|
|
{ scope: "global", groups: params.cfg.channels?.telegram?.groups },
|
|
];
|
|
|
|
for (const target of migrationTargets) {
|
|
const result = migrateTelegramGroupsInPlace(target.groups, params.oldChatId, params.newChatId);
|
|
if (result.migrated) {
|
|
migrated = true;
|
|
scopes.push(target.scope);
|
|
}
|
|
if (result.skippedExisting) {
|
|
skippedExisting = true;
|
|
}
|
|
}
|
|
|
|
return { migrated, skippedExisting, scopes };
|
|
}
|