Text messages are the foundation of WhatsApp communication. The SDK provides a simple yet powerful interface for sending text messages with advanced features like URL previews and reply support.
// Reply to a specific messageawait client.sendText( '+1234567890', 'Thanks for your question! Here\'s the answer...', { replyToMessageId: 'wamid.HBgLM2E5YzE3...' });
const orderDetails = `📦 Order ConfirmationOrder #: 123456Date: ${new Date().toLocaleDateString()}Total: $99.99Items:• Product A - $49.99• Product B - $39.99• Shipping - $10.00Delivery Address:123 Main StNew York, NY 10001Expected delivery: 3-5 business days`.trim();await client.sendText('+1234567890', orderDetails);
// News article with previewawait client.sendText( '+1234567890', 'Breaking: Important announcement from our team.\n\n' + 'Read the full story: https://news.example.com/breaking-news\n\n' + 'Let us know what you think!', { previewUrl: true });// Product link with previewawait client.sendText( '+1234567890', 'Check out this amazing product we found for you!\n\n' + 'https://shop.example.com/product/amazing-widget\n\n' + '💰 Special discount code: SAVE20', { previewUrl: true });
// Single URL for clean previewawait client.sendText(to, 'Article: The Future of Technology\n\n' + 'https://blog.example.com/future-tech', { previewUrl: true });// Descriptive context before URLawait client.sendText(to, 'Your invoice is ready for download:\n' + 'https://billing.example.com/invoice/123', { previewUrl: true });
// Multiple URLs (only first gets preview)await client.sendText(to, 'Check these links:\n' + 'https://example1.com\n' + 'https://example2.com', { previewUrl: true } // Only first URL previewed);// URLs without contextawait client.sendText(to, 'https://example.com', { previewUrl: true } // No context provided);
// Support conversation exampleconst webhookProcessor = client.createWebhookProcessor({ onTextMessage: async (message) => { const userText = message.text.toLowerCase(); if (userText.includes('help')) { await client.sendText( message.from, '🆘 Our support team will assist you shortly.\n\n' + 'In the meantime, check our FAQ: https://help.example.com', { replyToMessageId: message.id, previewUrl: true } ); } else if (userText.includes('order')) { await client.sendText( message.from, '📦 Please provide your order number and I\'ll look it up for you.', { replyToMessageId: message.id } ); } }});
async function sendPersonalizedWelcome(phoneNumber: string, userName: string) { const welcomeMessage = `Hello ${userName}! 👋Welcome to our platform. Here's what you can do:🚀 Get started: Complete your profile📚 Learn more: Check out our tutorials 💬 Get help: Contact our support teamReady to begin? https://app.example.com/onboarding`.trim(); return await client.sendText(phoneNumber, welcomeMessage, { previewUrl: true });}// Usageawait sendPersonalizedWelcome('+1234567890', 'John');
async function sendOrderUpdate(phoneNumber: string, order: any) { let message = `📦 Order Update - #${order.id}\n\n`; switch (order.status) { case 'confirmed': message += '✅ Your order has been confirmed!\n'; message += `💰 Total: $${order.total}\n`; message += `📅 Expected delivery: ${order.deliveryDate}`; break; case 'shipped': message += '🚚 Your order has shipped!\n'; message += `📦 Tracking: ${order.trackingNumber}\n`; message += `🔗 Track here: ${order.trackingUrl}`; break; case 'delivered': message += '🎉 Your order has been delivered!\n'; message += 'We hope you love your purchase.\n\n'; message += '⭐ Rate your experience: https://review.example.com'; break; } return await client.sendText(phoneNumber, message, { previewUrl: true });}
// ✅ Good: Concise and clearawait client.sendText(to, 'Order #12345 confirmed! 📦\n' + 'Tracking: https://track.example.com/12345');// ❌ Avoid: Too verboseawait client.sendText(to, 'We are pleased to inform you that your order number 12345 has been successfully confirmed and is now being processed by our fulfillment team...');
// ✅ Good: Enable for important linksawait client.sendText(to, 'Your invoice is ready: https://billing.example.com/invoice/123', { previewUrl: true });// ❌ Avoid: Preview for every URLawait client.sendText(to, 'Contact us at https://contact.example.com or https://support.example.com', { previewUrl: true } // Only first URL gets preview);
// ✅ Good: Meaningful repliesawait client.sendText(to, 'Thanks for your order inquiry! Here are the details you requested...', { replyToMessageId: originalMessageId });// ❌ Avoid: Generic repliesawait client.sendText(to, 'OK', { replyToMessageId: originalMessageId });