mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:44:33 +00:00
Slack: add modal private metadata utilities
This commit is contained in:
42
src/slack/modal-metadata.ts
Normal file
42
src/slack/modal-metadata.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
export type SlackModalPrivateMetadata = {
|
||||
sessionKey?: string;
|
||||
channelId?: string;
|
||||
channelType?: string;
|
||||
};
|
||||
|
||||
const SLACK_PRIVATE_METADATA_MAX = 3000;
|
||||
|
||||
function normalizeString(value: unknown) {
|
||||
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
export function parseSlackModalPrivateMetadata(raw: unknown): SlackModalPrivateMetadata {
|
||||
if (typeof raw !== "string" || raw.trim().length === 0) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Record<string, unknown>;
|
||||
return {
|
||||
sessionKey: normalizeString(parsed.sessionKey),
|
||||
channelId: normalizeString(parsed.channelId),
|
||||
channelType: normalizeString(parsed.channelType),
|
||||
};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function encodeSlackModalPrivateMetadata(input: SlackModalPrivateMetadata): string {
|
||||
const payload: SlackModalPrivateMetadata = {
|
||||
...(input.sessionKey ? { sessionKey: input.sessionKey } : {}),
|
||||
...(input.channelId ? { channelId: input.channelId } : {}),
|
||||
...(input.channelType ? { channelType: input.channelType } : {}),
|
||||
};
|
||||
const encoded = JSON.stringify(payload);
|
||||
if (encoded.length > SLACK_PRIVATE_METADATA_MAX) {
|
||||
throw new Error(
|
||||
`Slack modal private_metadata cannot exceed ${SLACK_PRIVATE_METADATA_MAX} chars`,
|
||||
);
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
Reference in New Issue
Block a user