2025-11-22
admin

Panduan Prompt AI: Migrasi Bot WhatsApp Node.js ke Go (100% Akurat)

Ingin memigrasi bot WhatsApp Node.js Anda ke Go? Gunakan prompt AI ini untuk konversi kode otomatis! Dapatkan kode Go lengkap, struktur proyek, dan penanganan API yang siap pakai.
Panduan Prompt AI: Migrasi Bot WhatsApp Node.js ke Go (100% Akurat)

Berikut adalah Prompt Engineering yang dirancang khusus agar AI (seperti ChatGPT, Claude, atau Cursor) dapat memigrasi kode Node.js (Baileys) Anda ke Go (whatsmeow) dengan akurasi tinggi.

Prompt ini mencakup struktur proyek, penanganan API server (pengganti Express), dan logika bot (pengganti Handler).

Copy & Paste Prompt Ini:

Markdown


I have a Node.js WhatsApp Bot project using 'Baileys' library and 'Express.js' for the API Gateway. I need to migrate this entire project to **Golang** using the **whatsmeow** library.

**Source Code Context (Node.js):**
1. **index.js**: Main entry point. It runs an Express server for API endpoints (send-message, send-media) and initializes the Baileys socket connection. It handles QR code generation and file uploads (using Multer).
2. **handler.js**: Handles incoming messages (bot commands). Current commands: `!ping`, `!say`, `!resend` (media echo).
3. **lib/Messages.js**: Helper for serializing messages (I want to simplify this in Go using whatsmeow's native types).
4. **.env**: Configuration for ports and API keys.

**Target Tech Stack (Golang):**
- **Language**: Go (Golang)
- **WhatsApp Library**: `go.mau.fi/whatsmeow`
- **Database/Session**: SQLite (using `mattn/go-sqlite3` via whatsmeow's sqlstore) to replace the file-based session folder.
- **HTTP Router**: `github.com/gin-gonic/gin` (to replace Express.js).
- **Config**: `github.com/joho/godotenv`.

**Specific Migration Requirements:**

1. **Project Structure**: Create a clean Go project structure (e.g., `main.go`, `handlers/bot.go`, `handlers/api.go`, `config/config.go`).

2. **The HTTP API (Gin)**:
   - Replicate the following endpoints from the Node.js code:
     - `POST /api/send-message` (JSON body)
     - `POST /api/send-image` (Multipart form)
     - `POST /api/send-document` (Multipart form)
     - `POST /api/send-video` (Multipart form)
     - `GET /api/status`
     - `GET /api/qr` (Return the QR string if not logged in).
   - Implement API Key middleware (check `X-API-KEY` header).
   - Handle file uploads properly (similar to Multer logic).

3. **The Bot Logic (Whatsmeow)**:
   - Implement the Event Handler `client.AddEventHandler`.
   - Replicate the commands:
     - `!ping` -> Reply "Pong!" with speed calculation.
     - `!say [text]` -> Echo the text.
     - `!resend` -> If replying to media, download it and send it back (echo media).

4. **Connection Logic**:
   - Auto-connect on startup.
   - Print QR code to terminal using `github.com/mdp/qrterminal`.
   - Handle reconnection signals (SIGINT/SIGTERM).

5. **Helper Functions**:
   - Create a helper to parse JIDs (e.g., convert `0812...` to `[email protected]`).

**Please generate the full Go code separated by files. Ensure proper error handling and concurrency safety.**

Penjelasan Kenapa Prompt Ini Efektif:

  1. Definisi Stack yang Jelas: Saya secara eksplisit meminta Gin Gonic sebagai pengganti Express. Gin sangat mirip dengan Express (memiliki middleware, routing group, context) sehingga struktur logika API lama Anda akan tetap terjaga.
  2. Manajemen Sesi: Di Baileys (Node.js), sesi biasanya berupa folder banyak file JSON (useMultiFileAuthState). Di Whatsmeow (Go), standar industri adalah menggunakan SQLite. Prompt di atas memerintahkan AI untuk langsung menggunakan SQLite agar Anda tidak pusing mengurus ribuan file kecil.
  3. Fokus Fitur: Prompt ini menyoroti fitur spesifik yang ada di script Anda (!resend, !ping, API Uploads) agar AI tidak hanya membuat kode "Hello World" kosong.
  4. Penanganan Media: Bagian tersulit migrasi dari Baileys ke Whatsmeow adalah cara menangani download/upload media. Prompt di atas secara spesifik meminta logika !resend (download message -> upload back) diimplementasikan.
blog artificial-intelligence programming web-development tutorial admin

Artikel Terkait