Sync vault notes: April-November 2026 sessions, projects, and architecture
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fe4fe656be
commit
994c7ae577
379 files changed
+55216
-107
No files matched your search
@@ -0,0 +1,295 @@
|
||||
|
||||
## Business API Implementation
|
||||
# WhatsApp Business API Integration - Complete Implementation Log
|
||||
|
||||
**Date:** 2026-05-01
|
||||
**Author:** OpenCode AI
|
||||
**Status:** ✅ SUCCESSFUL - Deployed to Production
|
||||
**User:** Garfield (switched from personal to business account)
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
After multiple failed attempts to integrate WhatsApp personal account (Playwright, Baileys), user switched to **WhatsApp Business Account**. Implementation uses **Meta Cloud API (Graph API v18.0)**.
|
||||
|
||||
---
|
||||
|
||||
## Previous Failed Attempts (for reference)
|
||||
|
||||
### Attempt 1: Playwright Browser Automation ❌
|
||||
- Created `src/clients/whatsapp-web.ts` (Playwright-based)
|
||||
- Extract QR from canvas → serve via HTTP on port 3000
|
||||
- **Failed:** Container crashes, Playwright deps too complex for Kubernetes
|
||||
|
||||
### Attempt 2: Baileys Node.js Library ❌
|
||||
- Rewrote using Baileys (pure Node.js, no browser)
|
||||
- Added `qr-image` for QR generation
|
||||
- **Failed:** TypeScript errors, ES modules vs CommonJS conflicts, pod crashes
|
||||
|
||||
### Attempt 3: CommonJS Rewrite ❌
|
||||
- Renamed to `.cjs`, used `require()` syntax
|
||||
- **Failed:** Still crashed, complex type issues
|
||||
|
||||
### Final Action: Restored to clean state
|
||||
```bash
|
||||
cd /home/garfield/hermes-mcp
|
||||
git checkout HEAD -- .
|
||||
rm -f src/clients/whatsapp-web.ts src/clients/whatsapp-web.cjs
|
||||
echo "Clean state restored"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## WhatsApp Business API Implementation (SUCCESSFUL)
|
||||
|
||||
### User Action
|
||||
User switched from personal WhatsApp account to **WhatsApp Business Account**.
|
||||
|
||||
### 1. Created `src/clients/whatsapp.ts`
|
||||
|
||||
**File:** `/home/garfield/hermes-mcp/src/clients/whatsapp.ts`
|
||||
**Library:** Meta Cloud API via `fetch()` (no external WhatsApp library needed)
|
||||
|
||||
**Functions exported:**
|
||||
```typescript
|
||||
export async function sendMessage(args: { to: string; message: string; account?: string })
|
||||
export async function sendTemplate(args: { to: string; template_name: string; ... })
|
||||
export async function getMessageStatus(args: { message_id: string; account?: string })
|
||||
export async function listTemplates(args: { account?: string })
|
||||
```
|
||||
|
||||
**Key implementation details:**
|
||||
- Base URL: `https://graph.facebook.com/v18.0/`
|
||||
- Auth: `Bearer ${accessToken}` header
|
||||
- Timeout: 10 seconds via `AbortSignal.timeout(10000)`
|
||||
- Session persistence: Cookie-based in `/tmp/whatsapp-baileys-session`
|
||||
|
||||
**Environment variables (per account):**
|
||||
```env
|
||||
WHATSAPP_{ACCOUNT}_PHONE_NUMBER_ID=...
|
||||
WHATSAPP_{ACCOUNT}_ACCESS_TOKEN=...
|
||||
WHATSAPP_{ACCOUNT}_BUSINESS_ACCOUNT_ID=...
|
||||
```
|
||||
|
||||
**24-hour customer service window:**
|
||||
- `send_message` only works if recipient messaged you within 24h
|
||||
- Outside window: Must use `send_template` with approved template
|
||||
|
||||
**Message status:**
|
||||
- ⚠️ `get_message_status` is **deprecated**
|
||||
- Meta API does NOT support polling message status via GET
|
||||
- Status updates are **push-only via webhooks**
|
||||
- Tool throws error directing to webhook implementation
|
||||
|
||||
---
|
||||
|
||||
### 2. Updated `src/tools.ts`
|
||||
|
||||
**Added imports:**
|
||||
```typescript
|
||||
import { sendMessage, sendTemplate, getMessageStatus, listTemplates } from './clients/whatsapp.js';
|
||||
```
|
||||
|
||||
**Added 4 WhatsApp tool definitions:**
|
||||
```typescript
|
||||
{
|
||||
name: 'whatsapp_send_message',
|
||||
description: 'Send a WhatsApp message to a phone number...',
|
||||
inputSchema: { to, message, account (optional) }
|
||||
},
|
||||
{
|
||||
name: 'whatsapp_send_template',
|
||||
description: 'Send a WhatsApp template message...',
|
||||
inputSchema: { to, template_name, language, components, account }
|
||||
},
|
||||
{
|
||||
name: 'whatsapp_get_message_status',
|
||||
description: '[DEPRECATED] Meta Cloud API does not support polling...',
|
||||
inputSchema: { message_id, account }
|
||||
},
|
||||
{
|
||||
name: 'whatsapp_list_templates',
|
||||
description: 'List all approved WhatsApp message templates...',
|
||||
inputSchema: { account }
|
||||
}
|
||||
```
|
||||
|
||||
**Added dispatch cases in `handleToolCall()`:**
|
||||
```typescript
|
||||
case 'whatsapp_send_message':
|
||||
result = await sendMessage({ to, message, account });
|
||||
break;
|
||||
|
||||
case 'whatsapp_send_template':
|
||||
result = await sendTemplate({ to, template_name, language, components, account });
|
||||
break;
|
||||
|
||||
case 'whatsapp_get_message_status':
|
||||
result = await getMessageStatus({ message_id, account });
|
||||
break;
|
||||
|
||||
case 'whatsapp_list_templates':
|
||||
result = await listTemplates({ account });
|
||||
break;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. Updated `src/index.ts`
|
||||
|
||||
**Added imports:**
|
||||
```typescript
|
||||
import { sendMessage, sendTemplate, getMessageStatus, listTemplates } from './clients/whatsapp.js';
|
||||
```
|
||||
|
||||
**Added REST endpoints (all behind `requireAuth`):**
|
||||
```typescript
|
||||
// Send text message
|
||||
app.post('/api/whatsapp/send', ...)
|
||||
|
||||
// Send template message
|
||||
app.post('/api/whatsapp/template', ...)
|
||||
|
||||
// List templates
|
||||
app.get('/api/whatsapp/templates', ...)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. Updated `.env.example`
|
||||
|
||||
Added WhatsApp Business API env vars:
|
||||
```env
|
||||
# ── WhatsApp (Meta Cloud API) ────────────────────────
|
||||
# Get these from https://business.facebook.com/settings/whatsapp-business-accounts
|
||||
|
||||
# For default account:
|
||||
WHATSAPP_DEFAULT_PHONE_NUMBER_ID=your-phone-number-id
|
||||
WHATSAPP_DEFAULT_ACCESS_TOKEN=your-permanent-access-token
|
||||
WHATSAPP_DEFAULT_BUSINESS_ACCOUNT_ID=your-business-account-id
|
||||
|
||||
# For additional accounts, duplicate with WHATSAPP_{ACCOUNT}_*
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. Build & Deployment Process
|
||||
|
||||
**Build attempt history:**
|
||||
1. TypeScript errors with `WhatsAppToolArgs` type → Removed type export
|
||||
2. `qr-image` missing declarations → Installed `@types/qr-image`
|
||||
3. ES module vs CommonJS conflicts → Simplified to direct `require()`
|
||||
4. Docker cache issues → Used `--no-cache` flag
|
||||
5. **Final successful build:**
|
||||
```bash
|
||||
cd /home/garfield/hermes-mcp
|
||||
npm run build # ✅ Passed
|
||||
docker build -t localhost:32000/hermes-mcp:latest . # ✅ Success
|
||||
docker push localhost:32000/hermes-mcp:latest # ✅ Pushed
|
||||
microk8s kubectl rollout restart deployment/hermes-mcp -n fetcherpay # ✅ Deployed
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Final State
|
||||
|
||||
### ✅ What Works
|
||||
1. **Pod status:** `Running` (hermes-mcp-644cf78c4d-sw2s2)
|
||||
2. **Health endpoint:** `https://hermes.fetcherpay.com/health`
|
||||
- Shows `toolCount: 15` (up from 11 - 4 new WhatsApp tools)
|
||||
- Endpoints include `/api/whatsapp/send`, `/api/whatsapp/template`, `/api/whatsapp/templates`
|
||||
3. **WhatsApp tools available:**
|
||||
- `whatsapp_send_message`
|
||||
- `whatsapp_send_template`
|
||||
- `whatsapp_get_message_status` (deprecated)
|
||||
- `whatsapp_list_templates`
|
||||
|
||||
### ❌ What Doesn't Work Yet
|
||||
1. **Credentials not set** - Need to add to Kubernetes deployment:
|
||||
- `WHATSAPP_DEFAULT_PHONE_NUMBER_ID`
|
||||
- `WHATSAPP_DEFAULT_ACCESS_TOKEN`
|
||||
- `WHATSAPP_DEFAULT_BUSINESS_ACCOUNT_ID`
|
||||
2. **Webhook not implemented** - `get_message_status` will fail until webhook added
|
||||
3. **24-hour window constraint** - Free-form messages only work within 24h of user message
|
||||
|
||||
---
|
||||
|
||||
## Where to Get Credentials
|
||||
|
||||
### 1. Phone Number ID
|
||||
- Go to [Meta Business Settings](https://business.facebook.com/settings)
|
||||
- Navigate: **WhatsApp Accounts** → **Phone Numbers**
|
||||
- Copy the **Phone Number ID** (numeric, e.g., `1234567890123456`)
|
||||
|
||||
### 2. Access Token
|
||||
- Same page → **System Users** (or create one)
|
||||
- Generate a **Permanent Token** (not temporary user token)
|
||||
- Needs permissions: `whatsapp_business_messaging`, `whatsapp_business_management`
|
||||
|
||||
### 3. Business Account ID (WABA ID)
|
||||
- Same page → **WhatsApp Accounts**
|
||||
- Copy the **WhatsApp Business Account ID** (starts with `100...`)
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
### Immediate (requires credentials)
|
||||
1. **Add env vars to Kubernetes:**
|
||||
```bash
|
||||
microk8s kubectl set env deployment/hermes-mcp -n fetcherpay \n WHATSAPP_DEFAULT_PHONE_NUMBER_ID="your_id" \n WHATSAPP_DEFAULT_ACCESS_TOKEN="your_token" \n WHATSAPP_DEFAULT_BUSINESS_ACCOUNT_ID="your_waba_id"
|
||||
```
|
||||
|
||||
2. **Test sending message:**
|
||||
```bash
|
||||
curl -X POST "https://hermes.fetcherpay.com/api/whatsapp/send" \n -H "x-api-key: 114521f3f9e6858480d6269446a446ef" \n -H "Content-Type: application/json" \n -d '{"to": "+19548716341", "message": "Test from Hermes"}'
|
||||
```
|
||||
|
||||
3. **List templates:**
|
||||
```bash
|
||||
curl -H "x-api-key: 114521f3f9e6858480d6269446a446ef" \n "https://hermes.fetcherpay.com/api/whatsapp/templates"
|
||||
```
|
||||
|
||||
### Future Enhancements
|
||||
1. **Add webhook endpoint** (`/api/whatsapp/webhook`) for delivery status
|
||||
2. **Persist webhook events** to enable `get_message_status`
|
||||
3. **Re-enable `get_message_status`** once webhook state storage exists
|
||||
4. **Add `read_messages` tool** to surface inbound messages
|
||||
|
||||
---
|
||||
|
||||
## Files Modified
|
||||
|
||||
### New Files
|
||||
- `src/clients/whatsapp.ts` - Meta Cloud API client (248 lines)
|
||||
|
||||
### Modified Files
|
||||
- `src/tools.ts` - Added 4 WhatsApp tool definitions + dispatch cases
|
||||
- `src/index.ts` - Added 3 REST endpoints
|
||||
- `.env.example` - Added WhatsApp Business API env vars
|
||||
|
||||
### Dockerfile (unchanged)
|
||||
Uses original `node:20-alpine` base, no extra deps needed for Meta Cloud API.
|
||||
|
||||
---
|
||||
|
||||
## Tool Count Evolution
|
||||
- **Before:** 11 tools (email + obsidian)
|
||||
- **After:** 15 tools (+4 WhatsApp Business tools)
|
||||
- **Health endpoint confirms:** `"toolCount":15`
|
||||
|
||||
---
|
||||
|
||||
## Key Learnings
|
||||
|
||||
1. **Personal vs Business:** WhatsApp personal accounts CANNOT use Meta Cloud API
|
||||
2. **Playwright/Baileys:** Not suitable for Kubernetes containers (too brittle)
|
||||
3. **Meta Cloud API:** Simple REST API, no special libraries needed
|
||||
4. **24-hour window:** Major constraint for free-form messaging
|
||||
5. **Webhooks required:** Status polling not supported, must use push-based updates
|
||||
|
||||
---
|
||||
|
||||
**Status:** ✅ Implementation COMPLETE, ready for credential configuration and testing.
|
||||
|
||||
Reference in new issue
Block a user