Files
simstudioai--sim/apps/sim/tools/whatsapp/send_interactive.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

140 lines
4.6 KiB
TypeScript

import type { ToolConfig } from '@/tools/types'
import type { WhatsAppSendInteractiveParams, WhatsAppSendResponse } from '@/tools/whatsapp/types'
import {
buildAuthHeaders,
buildMessagesUrl,
transformWhatsAppSendResponse,
whatsappSendOutputs,
} from '@/tools/whatsapp/utils'
function coerceArray(value: unknown): unknown[] | undefined {
if (value == null || value === '') return undefined
const parsed = typeof value === 'string' ? (JSON.parse(value) as unknown) : value
if (!Array.isArray(parsed)) {
throw new Error('Interactive buttons and sections must be JSON arrays')
}
return parsed.length > 0 ? parsed : undefined
}
export const sendInteractiveTool: ToolConfig<WhatsAppSendInteractiveParams, WhatsAppSendResponse> =
{
id: 'whatsapp_send_interactive',
name: 'WhatsApp Send Interactive',
description: 'Send an interactive WhatsApp message with reply buttons or a selectable list.',
version: '1.0.0',
params: {
phoneNumber: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Recipient phone number with country code (e.g., +14155552671)',
},
bodyText: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Main body text of the interactive message',
},
headerText: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Optional plain-text header shown above the body',
},
footerText: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Optional footer text shown below the body',
},
buttons: {
type: 'json',
required: false,
visibility: 'user-or-llm',
description:
'Reply buttons array (max 3), each item: { "type": "reply", "reply": { "id": "...", "title": "..." } }. Provide buttons or sections.',
},
listButtonText: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Label for the menu button that opens the list (required when sending a list)',
},
sections: {
type: 'json',
required: false,
visibility: 'user-or-llm',
description:
'List sections array, each item: { "title": "...", "rows": [{ "id": "...", "title": "...", "description": "..." }] }. Provide sections or buttons.',
},
phoneNumberId: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'WhatsApp Business Phone Number ID (from Meta Business Suite)',
},
accessToken: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'WhatsApp Business API Access Token (from Meta Developer Portal)',
},
},
request: {
url: (params) => buildMessagesUrl(params.phoneNumberId),
method: 'POST',
headers: (params) => buildAuthHeaders(params.accessToken),
body: (params) => {
if (!params.phoneNumber) {
throw new Error('Phone number is required but was not provided')
}
if (!params.bodyText) {
throw new Error('Body text is required but was not provided')
}
const buttons = coerceArray(params.buttons)
const sections = coerceArray(params.sections)
if (!buttons && !sections) {
throw new Error('Provide either buttons (reply buttons) or sections (list)')
}
if (buttons && sections) {
throw new Error('Provide either buttons or sections, not both')
}
const interactive: Record<string, unknown> = {
type: buttons ? 'button' : 'list',
body: { text: params.bodyText },
}
if (params.headerText) {
interactive.header = { type: 'text', text: params.headerText }
}
if (params.footerText) {
interactive.footer = { text: params.footerText }
}
if (buttons) {
interactive.action = { buttons }
} else {
const listButton = params.listButtonText?.trim()
if (!listButton) {
throw new Error('listButtonText is required when sending a list')
}
interactive.action = { button: listButton, sections }
}
return {
messaging_product: 'whatsapp',
recipient_type: 'individual',
to: params.phoneNumber.trim(),
type: 'interactive',
interactive,
}
},
},
transformResponse: transformWhatsAppSendResponse,
outputs: whatsappSendOutputs,
}