mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-12 04:56:08 +00:00
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
// Slack plugin module implements probe behavior.
|
|
import type { BaseProbeResult } from "openclaw/plugin-sdk/channel-contract";
|
|
import { withTimeout } from "openclaw/plugin-sdk/text-utility-runtime";
|
|
import { createSlackWebClient } from "./client.js";
|
|
import { formatSlackError } from "./errors.js";
|
|
import { formatSlackBotTokenIdentityWarning } from "./token.js";
|
|
|
|
export type SlackProbe = BaseProbeResult & {
|
|
status?: number | null;
|
|
elapsedMs?: number | null;
|
|
bot?: { id?: string; name?: string };
|
|
team?: { id?: string; name?: string };
|
|
warning?: string;
|
|
};
|
|
|
|
export async function probeSlack(
|
|
token: string,
|
|
timeoutMs = 2500,
|
|
opts?: { accountId?: string | null },
|
|
): Promise<SlackProbe> {
|
|
const client = createSlackWebClient(token);
|
|
const start = Date.now();
|
|
try {
|
|
const result = await withTimeout(client.auth.test(), timeoutMs);
|
|
if (!result.ok) {
|
|
return {
|
|
ok: false,
|
|
status: 200,
|
|
error: result.error ?? "unknown",
|
|
elapsedMs: Date.now() - start,
|
|
};
|
|
}
|
|
const warning = formatSlackBotTokenIdentityWarning({
|
|
auth: result,
|
|
accountId: opts?.accountId,
|
|
});
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
elapsedMs: Date.now() - start,
|
|
bot: { id: result.user_id, name: result.user },
|
|
team: { id: result.team_id, name: result.team },
|
|
...(warning ? { warning } : {}),
|
|
};
|
|
} catch (err) {
|
|
const message = formatSlackError(err);
|
|
const status =
|
|
typeof (err as { statusCode?: number }).statusCode === "number"
|
|
? (err as { statusCode?: number }).statusCode
|
|
: null;
|
|
return {
|
|
ok: false,
|
|
status,
|
|
error: message,
|
|
elapsedMs: Date.now() - start,
|
|
};
|
|
}
|
|
}
|