UNPKG

@akson/chatsuite-sdk

Version:

Production-ready TypeScript SDK for ChatSuite - WhatsApp automation with built-in session management, message queuing, webhook server, and database sync

78 lines (63 loc) 2.38 kB
import { WhatsAppClient } from '@whatsapp-baileys/sdk'; async function main() { // Initialize client const client = new WhatsAppClient({ apiToken: process.env.WHATSAPP_API_TOKEN || 'wa_your_token_here', baseUrl: process.env.WHATSAPP_API_URL || 'https://whatsapp-api.akson.ch' }); try { // Check API health const health = await client.health(); console.log('API Status:', health); // List existing sessions const { sessions } = await client.sessions.list(); console.log('Existing sessions:', sessions.length); // Create a new session if none exist if (sessions.length === 0) { const phoneNumber = '+1234567890'; // Replace with your number console.log('Creating new session...'); const session = await client.sessions.create({ tel: phoneNumber, name: 'My WhatsApp Bot', autoRestart: true }); console.log('Session created:', session.tel); // Initialize the session console.log('Initializing session...'); await client.sessions.initialize(phoneNumber); // Wait for QR code console.log('Waiting for QR code...'); const qrSession = await client.sessions.pollForQR(phoneNumber); console.log('\n=== SCAN THIS QR CODE WITH WHATSAPP ==='); console.log(qrSession.qr); console.log('=======================================\n'); // Wait for connection console.log('Waiting for connection...'); const readySession = await client.sessions.waitForReady(phoneNumber); console.log('Connected successfully!'); // Set up webhook for receiving messages (optional) const webhookUrl = process.env.WEBHOOK_URL; if (webhookUrl) { console.log('Setting up webhook...'); const webhook = await client.webhooks.setupMessageWebhook( phoneNumber, webhookUrl, 'Message Webhook' ); console.log('Webhook configured:', webhook.name); } // Send a test message const testNumber = '1234567890@c.us'; // Replace with recipient const message = await client.messages.sendText( phoneNumber, testNumber, 'Hello from WhatsApp SDK! 🚀' ); console.log('Message sent:', message.id); } } catch (error) { console.error('Error:', error); } } // Run the example main();