mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-28 02:12:07 +00:00
57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import type {
|
|
ChannelMessageActionAdapter,
|
|
ChannelMessageActionName,
|
|
OpenClawConfig,
|
|
} from "openclaw/plugin-sdk/zalo";
|
|
import { extractToolSend, jsonResult, readStringParam } from "openclaw/plugin-sdk/zalo";
|
|
import { listEnabledZaloAccounts } from "./accounts.js";
|
|
import { sendMessageZalo } from "./send.js";
|
|
|
|
const providerId = "zalo";
|
|
|
|
function listEnabledAccounts(cfg: OpenClawConfig) {
|
|
return listEnabledZaloAccounts(cfg).filter(
|
|
(account) => account.enabled && account.tokenSource !== "none",
|
|
);
|
|
}
|
|
|
|
export const zaloMessageActions: ChannelMessageActionAdapter = {
|
|
listActions: ({ cfg }) => {
|
|
const accounts = listEnabledAccounts(cfg);
|
|
if (accounts.length === 0) {
|
|
return [];
|
|
}
|
|
const actions = new Set<ChannelMessageActionName>(["send"]);
|
|
return Array.from(actions);
|
|
},
|
|
getCapabilities: () => [],
|
|
extractToolSend: ({ args }) => extractToolSend(args, "sendMessage"),
|
|
handleAction: async ({ action, params, cfg, accountId }) => {
|
|
if (action === "send") {
|
|
const to = readStringParam(params, "to", { required: true });
|
|
const content = readStringParam(params, "message", {
|
|
required: true,
|
|
allowEmpty: true,
|
|
});
|
|
const mediaUrl = readStringParam(params, "media", { trim: false });
|
|
|
|
const result = await sendMessageZalo(to ?? "", content ?? "", {
|
|
accountId: accountId ?? undefined,
|
|
mediaUrl: mediaUrl ?? undefined,
|
|
cfg: cfg,
|
|
});
|
|
|
|
if (!result.ok) {
|
|
return jsonResult({
|
|
ok: false,
|
|
error: result.error ?? "Failed to send Zalo message",
|
|
});
|
|
}
|
|
|
|
return jsonResult({ ok: true, to, messageId: result.messageId });
|
|
}
|
|
|
|
throw new Error(`Action ${action} is not supported for provider ${providerId}.`);
|
|
},
|
|
};
|