@alfe.ai/openclaw-whatsapp 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugin.cjs +2 -2
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +3 -2
- package/dist/plugin.js.map +1 -1
- package/openclaw.plugin.json +0 -1
- package/package.json +1 -1
package/dist/plugin.cjs
CHANGED
|
@@ -12,6 +12,7 @@ let _alfe_ai_config = require("@alfe.ai/config");
|
|
|
12
12
|
* - whatsapp_send_template — send a template message to initiate conversation
|
|
13
13
|
* - whatsapp_list_templates — list available approved templates
|
|
14
14
|
*/
|
|
15
|
+
const pkg = (0, require("node:module").createRequire)(require("url").pathToFileURL(__filename).href)("../package.json");
|
|
15
16
|
function ok(data) {
|
|
16
17
|
return {
|
|
17
18
|
content: [{
|
|
@@ -123,10 +124,9 @@ const plugin = {
|
|
|
123
124
|
id: "@alfe.ai/openclaw-whatsapp",
|
|
124
125
|
name: "Alfe WhatsApp Plugin",
|
|
125
126
|
description: "WhatsApp integration — messaging and templates via Twilio",
|
|
126
|
-
version:
|
|
127
|
+
version: pkg.version,
|
|
127
128
|
activate(api) {
|
|
128
129
|
const log = api.logger;
|
|
129
|
-
if (api.registrationMode && api.registrationMode !== "full") return;
|
|
130
130
|
for (const tool of whatsappTools) api.registerTool(tool);
|
|
131
131
|
log.info(`Registered ${String(whatsappTools.length)} WhatsApp tools: ${whatsappTools.map((t) => t.name).join(", ")}`);
|
|
132
132
|
if (!globalThis.__whatsappPluginActivated) {
|
package/dist/plugin.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.cts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"plugin.d.cts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;;UAkBU,MAAA,CAiBO;MAIH,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;MAC0B,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;OAA4B,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;EAAO,KAAA,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;AAAA;UAfjE,iBAAA,CAoNT;QAlCe,EAjLN,MAiLM;kBA8BE,CAAA,EAAA,MAAA,GAAA,YAAA,GAAA,eAAA,GAAA,cAAA;EAAiB,YAAA,CAAA,IAAA,EA7Md,OA6Mc,CAAA,EAAA,IAAA;uEA5MoC;4DACX;;;;UAKlD,OAAA;;;;cAII;wCAC0B,4BAA4B;;;;;;;;cA6J9D;;;;;gBAMU;kBA8BE"}
|
package/dist/plugin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;;UAkBU,MAAA,CAiBO;MAIH,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;MAC0B,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;OAA4B,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;EAAO,KAAA,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,IAAA;AAAA;UAfjE,iBAAA,CAoNT;QAlCe,EAjLN,MAiLM;kBA8BE,CAAA,EAAA,MAAA,GAAA,YAAA,GAAA,eAAA,GAAA,cAAA;EAAiB,YAAA,CAAA,IAAA,EA7Md,OA6Mc,CAAA,EAAA,IAAA;uEA5MoC;4DACX;;;;UAKlD,OAAA;;;;cAII;wCAC0B,4BAA4B;;;;;;;;cA6J9D;;;;;gBAMU;kBA8BE"}
|
package/dist/plugin.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import { Type } from "@sinclair/typebox";
|
|
2
3
|
import { resolveConfig } from "@alfe.ai/config";
|
|
3
4
|
//#region src/plugin.ts
|
|
@@ -12,6 +13,7 @@ import { resolveConfig } from "@alfe.ai/config";
|
|
|
12
13
|
* - whatsapp_send_template — send a template message to initiate conversation
|
|
13
14
|
* - whatsapp_list_templates — list available approved templates
|
|
14
15
|
*/
|
|
16
|
+
const pkg = createRequire(import.meta.url)("../package.json");
|
|
15
17
|
function ok(data) {
|
|
16
18
|
return {
|
|
17
19
|
content: [{
|
|
@@ -123,10 +125,9 @@ const plugin = {
|
|
|
123
125
|
id: "@alfe.ai/openclaw-whatsapp",
|
|
124
126
|
name: "Alfe WhatsApp Plugin",
|
|
125
127
|
description: "WhatsApp integration — messaging and templates via Twilio",
|
|
126
|
-
version:
|
|
128
|
+
version: pkg.version,
|
|
127
129
|
activate(api) {
|
|
128
130
|
const log = api.logger;
|
|
129
|
-
if (api.registrationMode && api.registrationMode !== "full") return;
|
|
130
131
|
for (const tool of whatsappTools) api.registerTool(tool);
|
|
131
132
|
log.info(`Registered ${String(whatsappTools.length)} WhatsApp tools: ${whatsappTools.map((t) => t.name).join(", ")}`);
|
|
132
133
|
if (!globalThis.__whatsappPluginActivated) {
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["/**\n * @alfe/openclaw-whatsapp — OpenClaw native plugin\n *\n * Registers WhatsApp tools with OpenClaw. Tools call the Twilio service\n * API using the agent's API key for authentication.\n *\n * This plugin provides:\n * - whatsapp_send_message — send a free-form WhatsApp message (requires active session)\n * - whatsapp_send_template — send a template message to initiate conversation\n * - whatsapp_list_templates — list available approved templates\n */\n\nimport { Type, type TSchema } from \"@sinclair/typebox\";\nimport { resolveConfig } from \"@alfe.ai/config\";\n\ninterface Logger {\n info(msg: string, ...args: unknown[]): void;\n warn(msg: string, ...args: unknown[]): void;\n error(msg: string, ...args: unknown[]): void;\n debug(msg: string, ...args: unknown[]): void;\n}\n\ninterface OpenClawPluginApi {\n logger: Logger;\n registrationMode?: \"full\" | \"setup-only\" | \"setup-runtime\" | \"cli-metadata\";\n registerTool(tool: ToolDef): void;\n registerGatewayMethod(name: string, handler: (...args: unknown[]) => Promise<unknown>): void;\n on(event: string, handler: (...args: unknown[]) => void | Promise<void>, options?: { priority?: number }): void;\n}\n\n// ── Tool types ───────────────────────────────────────────────\n\ninterface ToolDef {\n name: string;\n description: string;\n label: string;\n parameters: TSchema;\n execute: (toolCallId: string, params: Record<string, unknown>) => Promise<{\n content: { type: \"text\"; text: string }[];\n details: unknown;\n }>;\n}\n\nfunction ok(data: unknown) {\n return { content: [{ type: \"text\" as const, text: JSON.stringify(data) }], details: data };\n}\n\nfunction errResult(message: string) {\n return { content: [{ type: \"text\" as const, text: JSON.stringify({ error: message }) }], details: { error: message } };\n}\n\nfunction defineTool(def: {\n name: string;\n description: string;\n parameters: TSchema;\n handler: (params: Record<string, unknown>) => Promise<unknown>;\n}): ToolDef {\n return {\n name: def.name,\n description: def.description,\n label: def.name,\n parameters: def.parameters,\n execute: async (_toolCallId: string, params: Record<string, unknown>) => {\n try {\n const result = await def.handler(params);\n return ok(result);\n } catch (e) {\n return errResult((e as Error).message);\n }\n },\n };\n}\n\n// ── API client ───────────────────────────────────────────────\n\nlet apiUrl = \"\";\nlet apiKey = \"\";\n\nasync function whatsappApi(\n method: string,\n path: string,\n body?: Record<string, unknown>,\n): Promise<Record<string, unknown>> {\n const url = `${apiUrl}${path}`;\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${apiKey}`,\n };\n\n const res = await fetch(url, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n const json = (await res.json()) as Record<string, unknown>;\n if (!res.ok) {\n const errorMsg = typeof json.error === \"string\"\n ? json.error\n : typeof json.message === \"string\"\n ? json.message\n : `WhatsApp service returned ${String(res.status)}`;\n throw new Error(errorMsg);\n }\n\n // unwrap { data: ... } envelope if present\n if (json.data && typeof json.data === \"object\") {\n return json.data as Record<string, unknown>;\n }\n return json;\n}\n\n// ── Tool definitions ─────────────────────────────────────────\n\nconst whatsappTools: ToolDef[] = [\n defineTool({\n name: \"whatsapp_send_message\",\n description:\n \"Send a free-form WhatsApp message from your phone number. \" +\n \"This only works if the recipient has messaged you within the last 24 hours (active session). \" +\n \"If there is no active session, you must use whatsapp_send_template to initiate the conversation first.\",\n parameters: Type.Object({\n to: Type.String({ description: \"Recipient phone number in E.164 format (e.g., +12025551234)\" }),\n body: Type.String({ description: \"The message content to send\" }),\n }),\n handler: async (params) => {\n const { to, body } = params as { to: string; body: string };\n\n // Pre-check session to give a clear error before hitting the API\n try {\n const session = await whatsappApi(\"GET\", `/mobile/whatsapp/session?to=${encodeURIComponent(to)}`);\n if (!session.active) {\n throw new Error(\n \"No active WhatsApp session with this user. Use whatsapp_send_template to initiate the conversation first, then the user can reply and you can send free-form messages.\",\n );\n }\n } catch (e) {\n const msg = (e as Error).message;\n if (msg.includes(\"No active WhatsApp session\") || msg.includes(\"whatsapp_send_template\")) {\n throw e;\n }\n // If session check fails for other reasons, try sending anyway — the API will enforce\n }\n\n return whatsappApi(\"POST\", \"/mobile/whatsapp/send\", { to, body });\n },\n }),\n\n defineTool({\n name: \"whatsapp_send_template\",\n description:\n \"Send a pre-approved WhatsApp template message to initiate a conversation. \" +\n \"Use this when you need to message a user who hasn't contacted you recently (no active 24-hour session). \" +\n \"First call whatsapp_list_templates to see available templates and their variables, \" +\n \"then call this tool with the chosen template. \" +\n \"After the user replies to your template, you can use whatsapp_send_message for free-form messages.\",\n parameters: Type.Object({\n to: Type.String({ description: \"Recipient phone number in E.164 format (e.g., +12025551234)\" }),\n contentSid: Type.String({ description: \"The template content SID (from whatsapp_list_templates)\" }),\n contentVariables: Type.Object({}, { description: \"Variables to fill in the template, e.g. {\\\"1\\\": \\\"Kevin\\\", \\\"2\\\": \\\"Agent Name\\\"}\", additionalProperties: Type.String() }),\n bodyPreview: Type.Optional(Type.String({ description: \"The filled template text for conversation context\" })),\n }),\n handler: async (params) => {\n const { to, contentSid, contentVariables, bodyPreview } = params as {\n to: string;\n contentSid: string;\n contentVariables: Record<string, string>;\n bodyPreview?: string;\n };\n return whatsappApi(\"POST\", \"/mobile/whatsapp/send-template\", {\n to,\n contentSid,\n contentVariables,\n bodyPreview,\n });\n },\n }),\n\n defineTool({\n name: \"whatsapp_list_templates\",\n description:\n \"List all approved WhatsApp message templates available for outreach. \" +\n \"Each template has a contentSid, name, body with {{1}}, {{2}} placeholders, \" +\n \"and variables showing what each placeholder represents. \" +\n \"Use this to find the right template before calling whatsapp_send_template.\",\n parameters: Type.Object({}),\n handler: async () => {\n return whatsappApi(\"GET\", \"/mobile/whatsapp/templates\");\n },\n }),\n];\n\n// ── Plugin definition ────────────────────────────────────────\n\nconst plugin = {\n id: \"@alfe.ai/openclaw-whatsapp\",\n name: \"Alfe WhatsApp Plugin\",\n description: \"WhatsApp integration — messaging and templates via Twilio\",\n version: \"0.0.1\",\n\n activate(api: OpenClawPluginApi) {\n const log = api.logger;\n\n // Only run during full gateway execution — skip CLI commands, setup, etc.\n if (api.registrationMode && api.registrationMode !== \"full\") {\n return;\n }\n\n // Always register tools — OpenClaw may reload the plugin registry per-session\n for (const tool of whatsappTools) {\n api.registerTool(tool);\n }\n log.info(`Registered ${String(whatsappTools.length)} WhatsApp tools: ${whatsappTools.map((t) => t.name).join(\", \")}`);\n\n // Only initialize config once (side effects)\n if (!(globalThis as Record<string, unknown>).__whatsappPluginActivated) {\n (globalThis as Record<string, unknown>).__whatsappPluginActivated = true;\n log.info(\"Alfe WhatsApp plugin activating...\");\n\n try {\n const config = resolveConfig();\n apiUrl = config.apiUrl;\n apiKey = config.apiKey;\n } catch (err) {\n log.error(`Failed to resolve config: ${err instanceof Error ? err.message : String(err)}`);\n log.warn(\"WhatsApp tools will fail — no API config available\");\n }\n\n log.info(`WhatsApp API: ${apiUrl}`);\n }\n\n log.info(\"Alfe WhatsApp plugin activated\");\n },\n\n deactivate(api: OpenClawPluginApi) {\n (globalThis as Record<string, unknown>).__whatsappPluginActivated = false;\n api.logger.info(\"Alfe WhatsApp plugin deactivated\");\n },\n};\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;AA2CA,SAAS,GAAG,MAAe;AACzB,QAAO;EAAE,SAAS,CAAC;GAAE,MAAM;GAAiB,MAAM,KAAK,UAAU,KAAK;GAAE,CAAC;EAAE,SAAS;EAAM;;AAG5F,SAAS,UAAU,SAAiB;AAClC,QAAO;EAAE,SAAS,CAAC;GAAE,MAAM;GAAiB,MAAM,KAAK,UAAU,EAAE,OAAO,SAAS,CAAC;GAAE,CAAC;EAAE,SAAS,EAAE,OAAO,SAAS;EAAE;;AAGxH,SAAS,WAAW,KAKR;AACV,QAAO;EACL,MAAM,IAAI;EACV,aAAa,IAAI;EACjB,OAAO,IAAI;EACX,YAAY,IAAI;EAChB,SAAS,OAAO,aAAqB,WAAoC;AACvE,OAAI;AAEF,WAAO,GADQ,MAAM,IAAI,QAAQ,OAAO,CACvB;YACV,GAAG;AACV,WAAO,UAAW,EAAY,QAAQ;;;EAG3C;;AAKH,IAAI,SAAS;AACb,IAAI,SAAS;AAEb,eAAe,YACb,QACA,MACA,MACkC;CAClC,MAAM,MAAM,GAAG,SAAS;CACxB,MAAM,UAAkC;EACtC,gBAAgB;EAChB,eAAe,UAAU;EAC1B;CAED,MAAM,MAAM,MAAM,MAAM,KAAK;EAC3B;EACA;EACA,MAAM,O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,MAAM,EAAE,IAAI,YAAY,kBAAkB,gBAAgB;AAM1D,UAAO,YAAY,QAAQ,kCAAkC;IAC3D;IACA;IACA;IACA;IACD,CAAC;;EAEL,CAAC;CAEF,WAAW;EACT,MAAM;EACN,aACE;EAIF,YAAY,KAAK,OAAO,EAAE,CAAC;EAC3B,SAAS,YAAY;AACnB,UAAO,YAAY,OAAO,6BAA6B;;EAE1D,CAAC;CACH;AAID,MAAM,SAAS;CACb,IAAI;CACJ,MAAM;CACN,aAAa;CACb,SAAS;CAET,SAAS,KAAwB;EAC/B,MAAM,MAAM,IAAI;AAGhB,MAAI,IAAI,oBAAoB,IAAI,qBAAqB,OACnD;AAIF,OAAK,MAAM,QAAQ,cACjB,KAAI,aAAa,KAAK;AAExB,MAAI,KAAK,cAAc,OAAO,cAAc,OAAO,CAAC,mBAAmB,cAAc,KAAK,MAAM,EAAE,KAAK,CAAC,KAAK,KAAK,GAAG;AAGrH,MAAI,CAAE,WAAuC,2BAA2B;AACrE,cAAuC,4BAA4B;AACpE,OAAI,KAAK,qCAAqC;AAE9C,OAAI;IACF,MAAM,SAAS,eAAe;AAC9B,aAAS,OAAO;AAChB,aAAS,OAAO;YACT,KAAK;AACZ,QAAI,MAAM,6BAA6B,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,GAAG;AAC1F,QAAI,KAAK,qDAAqD;;AAGhE,OAAI,KAAK,iBAAiB,SAAS;;AAGrC,MAAI,KAAK,iCAAiC;;CAG5C,WAAW,KAAwB;AAChC,aAAuC,4BAA4B;AACpE,MAAI,OAAO,KAAK,mCAAmC;;CAEtD"}
|
|
1
|
+
{"version":3,"file":"plugin.js","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["/**\n * @alfe/openclaw-whatsapp — OpenClaw native plugin\n *\n * Registers WhatsApp tools with OpenClaw. Tools call the Twilio service\n * API using the agent's API key for authentication.\n *\n * This plugin provides:\n * - whatsapp_send_message — send a free-form WhatsApp message (requires active session)\n * - whatsapp_send_template — send a template message to initiate conversation\n * - whatsapp_list_templates — list available approved templates\n */\n\nimport { Type, type TSchema } from \"@sinclair/typebox\";\nimport { resolveConfig } from \"@alfe.ai/config\";\nimport { createRequire } from 'node:module';\nconst require = createRequire(import.meta.url);\nconst pkg = require('../package.json') as { version: string };\n\ninterface Logger {\n info(msg: string, ...args: unknown[]): void;\n warn(msg: string, ...args: unknown[]): void;\n error(msg: string, ...args: unknown[]): void;\n debug(msg: string, ...args: unknown[]): void;\n}\n\ninterface OpenClawPluginApi {\n logger: Logger;\n registrationMode?: \"full\" | \"setup-only\" | \"setup-runtime\" | \"cli-metadata\";\n registerTool(tool: ToolDef): void;\n registerGatewayMethod(name: string, handler: (...args: unknown[]) => Promise<unknown>): void;\n on(event: string, handler: (...args: unknown[]) => void | Promise<void>, options?: { priority?: number }): void;\n}\n\n// ── Tool types ───────────────────────────────────────────────\n\ninterface ToolDef {\n name: string;\n description: string;\n label: string;\n parameters: TSchema;\n execute: (toolCallId: string, params: Record<string, unknown>) => Promise<{\n content: { type: \"text\"; text: string }[];\n details: unknown;\n }>;\n}\n\nfunction ok(data: unknown) {\n return { content: [{ type: \"text\" as const, text: JSON.stringify(data) }], details: data };\n}\n\nfunction errResult(message: string) {\n return { content: [{ type: \"text\" as const, text: JSON.stringify({ error: message }) }], details: { error: message } };\n}\n\nfunction defineTool(def: {\n name: string;\n description: string;\n parameters: TSchema;\n handler: (params: Record<string, unknown>) => Promise<unknown>;\n}): ToolDef {\n return {\n name: def.name,\n description: def.description,\n label: def.name,\n parameters: def.parameters,\n execute: async (_toolCallId: string, params: Record<string, unknown>) => {\n try {\n const result = await def.handler(params);\n return ok(result);\n } catch (e) {\n return errResult((e as Error).message);\n }\n },\n };\n}\n\n// ── API client ───────────────────────────────────────────────\n\nlet apiUrl = \"\";\nlet apiKey = \"\";\n\nasync function whatsappApi(\n method: string,\n path: string,\n body?: Record<string, unknown>,\n): Promise<Record<string, unknown>> {\n const url = `${apiUrl}${path}`;\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${apiKey}`,\n };\n\n const res = await fetch(url, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n const json = (await res.json()) as Record<string, unknown>;\n if (!res.ok) {\n const errorMsg = typeof json.error === \"string\"\n ? json.error\n : typeof json.message === \"string\"\n ? json.message\n : `WhatsApp service returned ${String(res.status)}`;\n throw new Error(errorMsg);\n }\n\n // unwrap { data: ... } envelope if present\n if (json.data && typeof json.data === \"object\") {\n return json.data as Record<string, unknown>;\n }\n return json;\n}\n\n// ── Tool definitions ─────────────────────────────────────────\n\nconst whatsappTools: ToolDef[] = [\n defineTool({\n name: \"whatsapp_send_message\",\n description:\n \"Send a free-form WhatsApp message from your phone number. \" +\n \"This only works if the recipient has messaged you within the last 24 hours (active session). \" +\n \"If there is no active session, you must use whatsapp_send_template to initiate the conversation first.\",\n parameters: Type.Object({\n to: Type.String({ description: \"Recipient phone number in E.164 format (e.g., +12025551234)\" }),\n body: Type.String({ description: \"The message content to send\" }),\n }),\n handler: async (params) => {\n const { to, body } = params as { to: string; body: string };\n\n // Pre-check session to give a clear error before hitting the API\n try {\n const session = await whatsappApi(\"GET\", `/mobile/whatsapp/session?to=${encodeURIComponent(to)}`);\n if (!session.active) {\n throw new Error(\n \"No active WhatsApp session with this user. Use whatsapp_send_template to initiate the conversation first, then the user can reply and you can send free-form messages.\",\n );\n }\n } catch (e) {\n const msg = (e as Error).message;\n if (msg.includes(\"No active WhatsApp session\") || msg.includes(\"whatsapp_send_template\")) {\n throw e;\n }\n // If session check fails for other reasons, try sending anyway — the API will enforce\n }\n\n return whatsappApi(\"POST\", \"/mobile/whatsapp/send\", { to, body });\n },\n }),\n\n defineTool({\n name: \"whatsapp_send_template\",\n description:\n \"Send a pre-approved WhatsApp template message to initiate a conversation. \" +\n \"Use this when you need to message a user who hasn't contacted you recently (no active 24-hour session). \" +\n \"First call whatsapp_list_templates to see available templates and their variables, \" +\n \"then call this tool with the chosen template. \" +\n \"After the user replies to your template, you can use whatsapp_send_message for free-form messages.\",\n parameters: Type.Object({\n to: Type.String({ description: \"Recipient phone number in E.164 format (e.g., +12025551234)\" }),\n contentSid: Type.String({ description: \"The template content SID (from whatsapp_list_templates)\" }),\n contentVariables: Type.Object({}, { description: \"Variables to fill in the template, e.g. {\\\"1\\\": \\\"Kevin\\\", \\\"2\\\": \\\"Agent Name\\\"}\", additionalProperties: Type.String() }),\n bodyPreview: Type.Optional(Type.String({ description: \"The filled template text for conversation context\" })),\n }),\n handler: async (params) => {\n const { to, contentSid, contentVariables, bodyPreview } = params as {\n to: string;\n contentSid: string;\n contentVariables: Record<string, string>;\n bodyPreview?: string;\n };\n return whatsappApi(\"POST\", \"/mobile/whatsapp/send-template\", {\n to,\n contentSid,\n contentVariables,\n bodyPreview,\n });\n },\n }),\n\n defineTool({\n name: \"whatsapp_list_templates\",\n description:\n \"List all approved WhatsApp message templates available for outreach. \" +\n \"Each template has a contentSid, name, body with {{1}}, {{2}} placeholders, \" +\n \"and variables showing what each placeholder represents. \" +\n \"Use this to find the right template before calling whatsapp_send_template.\",\n parameters: Type.Object({}),\n handler: async () => {\n return whatsappApi(\"GET\", \"/mobile/whatsapp/templates\");\n },\n }),\n];\n\n// ── Plugin definition ────────────────────────────────────────\n\nconst plugin = {\n id: \"@alfe.ai/openclaw-whatsapp\",\n name: \"Alfe WhatsApp Plugin\",\n description: \"WhatsApp integration — messaging and templates via Twilio\",\n version: pkg.version,\n\n activate(api: OpenClawPluginApi) {\n const log = api.logger;\n\n\n // Always register tools — OpenClaw may reload the plugin registry per-session\n for (const tool of whatsappTools) {\n api.registerTool(tool);\n }\n log.info(`Registered ${String(whatsappTools.length)} WhatsApp tools: ${whatsappTools.map((t) => t.name).join(\", \")}`);\n\n // Only initialize config once (side effects)\n if (!(globalThis as Record<string, unknown>).__whatsappPluginActivated) {\n (globalThis as Record<string, unknown>).__whatsappPluginActivated = true;\n log.info(\"Alfe WhatsApp plugin activating...\");\n\n try {\n const config = resolveConfig();\n apiUrl = config.apiUrl;\n apiKey = config.apiKey;\n } catch (err) {\n log.error(`Failed to resolve config: ${err instanceof Error ? err.message : String(err)}`);\n log.warn(\"WhatsApp tools will fail — no API config available\");\n }\n\n log.info(`WhatsApp API: ${apiUrl}`);\n }\n\n log.info(\"Alfe WhatsApp plugin activated\");\n },\n\n deactivate(api: OpenClawPluginApi) {\n (globalThis as Record<string, unknown>).__whatsappPluginActivated = false;\n api.logger.info(\"Alfe WhatsApp plugin deactivated\");\n },\n};\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;;AAgBA,MAAM,MADU,cAAc,OAAO,KAAK,IAAI,CAC1B,kBAAkB;AA8BtC,SAAS,GAAG,MAAe;AACzB,QAAO;EAAE,SAAS,CAAC;GAAE,MAAM;GAAiB,MAAM,KAAK,UAAU,KAAK;GAAE,CAAC;EAAE,SAAS;EAAM;;AAG5F,SAAS,UAAU,SAAiB;AAClC,QAAO;EAAE,SAAS,CAAC;GAAE,MAAM;GAAiB,MAAM,KAAK,UAAU,EAAE,OAAO,SAAS,CAAC;GAAE,CAAC;EAAE,SAAS,EAAE,OAAO,SAAS;EAAE;;AAGxH,SAAS,WAAW,KAKR;AACV,QAAO;EACL,MAAM,IAAI;EACV,aAAa,IAAI;EACjB,OAAO,IAAI;EACX,YAAY,IAAI;EAChB,SAAS,OAAO,aAAqB,WAAoC;AACvE,OAAI;AAEF,WAAO,GADQ,MAAM,IAAI,QAAQ,OAAO,CACvB;YACV,GAAG;AACV,WAAO,UAAW,EAAY,QAAQ;;;EAG3C;;AAKH,IAAI,SAAS;AACb,IAAI,SAAS;AAEb,eAAe,YACb,QACA,MACA,MACkC;CAClC,MAAM,MAAM,GAAG,SAAS;CACxB,MAAM,UAAkC;EACtC,gBAAgB;EAChB,eAAe,UAAU;EAC1B;CAED,MAAM,MAAM,MAAM,MAAM,KAAK;EAC3B;EACA;EACA,MAAM,OAAO,KAAK,UAAU,KAAK,GAAG,KAAA;EACrC,CAAC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