A plugin for integrating WhatsApp Cloud API with your application.
npm install @eliza/plugin-whatsapp
typescript import { WhatsAppPlugin } from '@eliza/plugin-whatsapp'; const whatsappPlugin = new WhatsAppPlugin({ accessToken: 'your_access_token', phoneNumberId: 'your_phone_number_id', webhookVerifyToken: 'your_webhook_verify_token', businessAccountId: 'your_business_account_id' });
typescript // Send a text message await whatsappPlugin.sendMessage({ type: 'text', to: '1234567890', content: 'Hello from WhatsApp!' }); // Send a template message await whatsappPlugin.sendMessage({ type: 'template', to: '1234567890', content: { name: 'hello_world', language: { code: 'en' } } });
typescript // Verify webhook app.get('/webhook', (req, res) => { const verified = await whatsappPlugin.verifyWebhook(req.query['hub.verify_token']); if (verified) { res.send(req.query['hub.challenge']); } else { res.sendStatus(403); } }); // Handle webhook events app.post('/webhook', (req, res) => { await whatsappPlugin.handleWebhook(req.body); res.sendStatus(200); });
- Send text messages
- Send template messages
- Webhook verification
- Webhook event handling
- Message status updates
config: WhatsAppConfig
- Configuration object for the plugin
sendMessage(message: WhatsAppMessage): Promise<any>
- Send a WhatsApp messagehandleWebhook(event: WhatsAppWebhookEvent): Promise<void>
- Process incoming webhook eventsverifyWebhook(token: string): Promise<boolean>
- Verify webhook token
typescript interface WhatsAppConfig { accessToken: string; phoneNumberId: string; webhookVerifyToken?: string; businessAccountId?: string; } interface WhatsAppMessage { type: 'text' | 'template'; to: string; content: string | WhatsAppTemplate; } interface WhatsAppTemplate { name: string; language: { code: string; }; components?: Array<{ type: string; parameters: Array<{ type: string; text?: string; }>; }>; }
The plugin throws errors in the following cases:
- Invalid configuration
- Failed message sending
- Webhook verification failure
- Invalid webhook payload
Example error handling:
typescript try { await whatsappPlugin.sendMessage({ type: 'text', to: '1234567890', content: 'Hello!' }); } catch (error) { console.error('Failed to send message:', error.message); }
- Always validate phone numbers before sending messages
- Use template messages for first-time messages to users
- Store message IDs for tracking delivery status
- Implement proper error handling
- Set up webhook retry mechanisms
- Keep your access tokens secure
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT