refactor(webhooks): reuse plugin-sdk webhook path helpers

This commit is contained in:
Peter Steinberger
2026-02-15 19:35:48 +00:00
parent 80eb91d9e7
commit 00e63da336
2 changed files with 15 additions and 60 deletions

View File

@@ -2,7 +2,9 @@ import type { IncomingMessage, ServerResponse } from "node:http";
import type { OpenClawConfig } from "openclaw/plugin-sdk";
import {
createReplyPrefixOptions,
normalizeWebhookPath,
readJsonBodyWithLimit,
resolveWebhookPath,
requestBodyErrorToText,
resolveMentionGatingWithBypass,
} from "openclaw/plugin-sdk";
@@ -86,34 +88,6 @@ function warnDeprecatedUsersEmailEntries(
);
}
function normalizeWebhookPath(raw: string): string {
const trimmed = raw.trim();
if (!trimmed) {
return "/";
}
const withSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
if (withSlash.length > 1 && withSlash.endsWith("/")) {
return withSlash.slice(0, -1);
}
return withSlash;
}
function resolveWebhookPath(webhookPath?: string, webhookUrl?: string): string | null {
const trimmedPath = webhookPath?.trim();
if (trimmedPath) {
return normalizeWebhookPath(trimmedPath);
}
if (webhookUrl?.trim()) {
try {
const parsed = new URL(webhookUrl);
return normalizeWebhookPath(parsed.pathname || "/");
} catch {
return null;
}
}
return "/googlechat";
}
export function registerGoogleChatWebhookTarget(target: WebhookTarget): () => void {
const key = normalizeWebhookPath(target.path);
const normalizedTarget = { ...target, path: key };
@@ -933,7 +907,11 @@ async function uploadAttachmentForReply(params: {
export function monitorGoogleChatProvider(options: GoogleChatMonitorOptions): () => void {
const core = getGoogleChatRuntime();
const webhookPath = resolveWebhookPath(options.webhookPath, options.webhookUrl);
const webhookPath = resolveWebhookPath({
webhookPath: options.webhookPath,
webhookUrl: options.webhookUrl,
defaultPath: "/googlechat",
});
if (!webhookPath) {
options.runtime.error?.(`[${options.account.accountId}] invalid webhook path`);
return () => {};
@@ -968,8 +946,11 @@ export function resolveGoogleChatWebhookPath(params: {
account: ResolvedGoogleChatAccount;
}): string {
return (
resolveWebhookPath(params.account.config.webhookPath, params.account.config.webhookUrl) ??
"/googlechat"
resolveWebhookPath({
webhookPath: params.account.config.webhookPath,
webhookUrl: params.account.config.webhookUrl,
defaultPath: "/googlechat",
}) ?? "/googlechat"
);
}

View File

@@ -2,7 +2,9 @@ import type { IncomingMessage, ServerResponse } from "node:http";
import type { OpenClawConfig, MarkdownTableMode } from "openclaw/plugin-sdk";
import {
createReplyPrefixOptions,
normalizeWebhookPath,
readJsonBodyWithLimit,
resolveWebhookPath,
requestBodyErrorToText,
} from "openclaw/plugin-sdk";
import type { ResolvedZaloAccount } from "./accounts.js";
@@ -80,34 +82,6 @@ type WebhookTarget = {
const webhookTargets = new Map<string, WebhookTarget[]>();
function normalizeWebhookPath(raw: string): string {
const trimmed = raw.trim();
if (!trimmed) {
return "/";
}
const withSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
if (withSlash.length > 1 && withSlash.endsWith("/")) {
return withSlash.slice(0, -1);
}
return withSlash;
}
function resolveWebhookPath(webhookPath?: string, webhookUrl?: string): string | null {
const trimmedPath = webhookPath?.trim();
if (trimmedPath) {
return normalizeWebhookPath(trimmedPath);
}
if (webhookUrl?.trim()) {
try {
const parsed = new URL(webhookUrl);
return normalizeWebhookPath(parsed.pathname || "/");
} catch {
return null;
}
}
return null;
}
export function registerZaloWebhookTarget(target: WebhookTarget): () => void {
const key = normalizeWebhookPath(target.path);
const normalizedTarget = { ...target, path: key };
@@ -700,7 +674,7 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
throw new Error("Zalo webhook secret must be 8-256 characters");
}
const path = resolveWebhookPath(webhookPath, webhookUrl);
const path = resolveWebhookPath({ webhookPath, webhookUrl, defaultPath: null });
if (!path) {
throw new Error("Zalo webhookPath could not be derived");
}