mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-12 21:53:00 -06:00
fix(dev): lazy-load telegram pairing smoke
This commit is contained in:
@@ -1,9 +1,37 @@
|
||||
// Test Device Pair Telegram script supports OpenClaw repository automation.
|
||||
import { sendMessageTelegram } from "../../extensions/telegram/runtime-api.js";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { getRuntimeConfig } from "../../src/config/config.js";
|
||||
import { matchPluginCommand, executePluginCommand } from "../../src/plugins/commands.js";
|
||||
import { loadOpenClawPlugins } from "../../src/plugins/loader.js";
|
||||
|
||||
type SendMessageTelegram = (
|
||||
chatId: string,
|
||||
text: string,
|
||||
options: {
|
||||
accountId?: string;
|
||||
cfg?: ReturnType<typeof getRuntimeConfig>;
|
||||
},
|
||||
) => Promise<{ chatId?: string; messageId?: string }>;
|
||||
|
||||
type DevicePairTelegramDeps = {
|
||||
executePluginCommand: typeof executePluginCommand;
|
||||
getRuntimeConfig: typeof getRuntimeConfig;
|
||||
loadOpenClawPlugins: typeof loadOpenClawPlugins;
|
||||
matchPluginCommand: typeof matchPluginCommand;
|
||||
sendMessageTelegram: SendMessageTelegram;
|
||||
};
|
||||
|
||||
type DevicePairTelegramResult = {
|
||||
accountId?: string;
|
||||
chatId: string;
|
||||
messageId?: string;
|
||||
sent: boolean;
|
||||
};
|
||||
|
||||
class UsageError extends Error {
|
||||
readonly exitCode = 1;
|
||||
}
|
||||
|
||||
function writeStdoutLine(...parts: string[]): void {
|
||||
process.stdout.write(`${parts.join(" ")}\n`);
|
||||
}
|
||||
@@ -12,8 +40,7 @@ function writeStderrLine(message: string): void {
|
||||
process.stderr.write(`${message}\n`);
|
||||
}
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const getArg = (flag: string, short?: string) => {
|
||||
function readArg(args: string[], flag: string, short?: string): string | undefined {
|
||||
const idx = args.indexOf(flag);
|
||||
if (idx !== -1 && idx + 1 < args.length) {
|
||||
return args[idx + 1];
|
||||
@@ -25,44 +52,102 @@ const getArg = (flag: string, short?: string) => {
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
}
|
||||
|
||||
const chatId = getArg("--chat", "-c");
|
||||
const accountId = getArg("--account", "-a");
|
||||
if (!chatId) {
|
||||
writeStderrLine(
|
||||
function usage(): string {
|
||||
return [
|
||||
"Usage: bun scripts/dev/test-device-pair-telegram.ts --chat <telegram-chat-id> [--account <accountId>]",
|
||||
);
|
||||
process.exit(1);
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
const cfg = getRuntimeConfig();
|
||||
loadOpenClawPlugins({ config: cfg });
|
||||
|
||||
const match = matchPluginCommand("/pair");
|
||||
if (!match) {
|
||||
writeStderrLine("/pair plugin command not registered.");
|
||||
process.exit(1);
|
||||
async function loadTelegramRuntimeSendMessage(): Promise<SendMessageTelegram> {
|
||||
const specifier = "../../extensions/telegram/runtime-api.js";
|
||||
const runtime = (await import(specifier)) as { sendMessageTelegram?: SendMessageTelegram };
|
||||
if (typeof runtime.sendMessageTelegram !== "function") {
|
||||
throw new Error("Telegram runtime-api.js did not export sendMessageTelegram");
|
||||
}
|
||||
return runtime.sendMessageTelegram;
|
||||
}
|
||||
|
||||
const result = await executePluginCommand({
|
||||
command: match.command,
|
||||
args: match.args,
|
||||
senderId: chatId,
|
||||
channel: "telegram",
|
||||
channelId: "telegram",
|
||||
isAuthorizedSender: true,
|
||||
commandBody: "/pair",
|
||||
config: cfg,
|
||||
from: `telegram:${chatId}`,
|
||||
to: `telegram:${chatId}`,
|
||||
accountId,
|
||||
});
|
||||
function createDefaultDeps(): DevicePairTelegramDeps {
|
||||
return {
|
||||
executePluginCommand,
|
||||
getRuntimeConfig,
|
||||
loadOpenClawPlugins,
|
||||
matchPluginCommand,
|
||||
sendMessageTelegram: async (...args) => {
|
||||
const sendMessageTelegram = await loadTelegramRuntimeSendMessage();
|
||||
return await sendMessageTelegram(...args);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (result.text) {
|
||||
await sendMessageTelegram(chatId, result.text, {
|
||||
async function runDevicePairTelegram(
|
||||
args = process.argv.slice(2),
|
||||
deps: DevicePairTelegramDeps = createDefaultDeps(),
|
||||
): Promise<DevicePairTelegramResult> {
|
||||
const chatId = readArg(args, "--chat", "-c");
|
||||
const accountId = readArg(args, "--account", "-a");
|
||||
if (!chatId) {
|
||||
throw new UsageError(usage());
|
||||
}
|
||||
|
||||
const cfg = deps.getRuntimeConfig();
|
||||
deps.loadOpenClawPlugins({ config: cfg });
|
||||
|
||||
const match = deps.matchPluginCommand("/pair", { channel: "telegram" });
|
||||
if (!match) {
|
||||
throw new Error("/pair plugin command not registered.");
|
||||
}
|
||||
|
||||
const result = await deps.executePluginCommand({
|
||||
command: match.command,
|
||||
args: match.args,
|
||||
senderId: chatId,
|
||||
channel: "telegram",
|
||||
channelId: "telegram",
|
||||
isAuthorizedSender: true,
|
||||
commandBody: "/pair",
|
||||
config: cfg,
|
||||
from: `telegram:${chatId}`,
|
||||
to: `telegram:${chatId}`,
|
||||
accountId,
|
||||
});
|
||||
|
||||
if (!result.text) {
|
||||
return { accountId, chatId, sent: false };
|
||||
}
|
||||
|
||||
const sent = await deps.sendMessageTelegram(chatId, result.text, {
|
||||
accountId,
|
||||
cfg,
|
||||
});
|
||||
|
||||
return {
|
||||
accountId,
|
||||
chatId: sent.chatId ?? chatId,
|
||||
messageId: sent.messageId,
|
||||
sent: true,
|
||||
};
|
||||
}
|
||||
|
||||
writeStdoutLine("Sent split /pair messages to", chatId, accountId ? `(${accountId})` : "");
|
||||
async function main(): Promise<void> {
|
||||
try {
|
||||
const result = await runDevicePairTelegram();
|
||||
writeStdoutLine(
|
||||
"Sent split /pair messages to",
|
||||
result.chatId,
|
||||
result.accountId ? `(${result.accountId})` : "",
|
||||
result.messageId ? `message=${result.messageId}` : "",
|
||||
);
|
||||
} catch (error) {
|
||||
writeStderrLine(error instanceof Error ? error.message : String(error));
|
||||
process.exitCode = error instanceof UsageError ? error.exitCode : 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
|
||||
await main();
|
||||
}
|
||||
|
||||
export { runDevicePairTelegram };
|
||||
|
||||
Reference in New Issue
Block a user