arattai-web.js
v1.0.1
Published
A Arattai API client that operates via the Arattai Web browser app
Downloads
7
Maintainers
Readme
arattai-web.js
A Arattai API client library for Node.js that connects through the Arattai Web browser app.
Installation
npm install arattai-web.jsNote: Node.js v18 or higher is required.
Quick Start
const { Client } = require('arattai-web.js');
const client = new Client();
client.on('qr', (qr) => {
console.log('QR RECEIVED', qr);
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message_sent', (message) => {
console.log('Message sent:', message);
});
client.initialize();Features
- ✅ QR Code Authentication - Scan QR code with your Arattai mobile app
- ✅ Session Persistence - Saves sessions to avoid repeated QR scans
- ✅ Send Messages - Send text messages to chats
- ✅ Get Chats - Retrieve list of all chats
- ✅ Event-Driven - Uses events for better code organization
- ✅ TypeScript Support - Includes TypeScript definitions
Usage
Basic Example
const { Client } = require('arattai-web.js');
const client = new Client({
sessionPath: './.wwebjs_auth',
headless: false
});
client.on('qr', (qr) => {
// Display QR code to user
console.log('Scan this QR code:', qr);
});
client.on('ready', async () => {
console.log('Client is ready!');
// Get all chats
const chats = await client.getChats();
console.log('Chats:', chats);
// Send a message
if (chats.length > 0) {
await client.sendMessage(chats[0].id, 'Hello, Arattai!');
}
});
client.initialize();Advanced Example
const { Client } = require('arattai-web.js');
const client = new Client({
sessionPath: './.wwebjs_auth',
headless: true, // Run in headless mode
qrRefreshInterval: 20000 // Refresh QR every 20 seconds
});
// Listen for loading screen updates
client.on('loading_screen', (percent, message) => {
console.log(`Loading: ${percent}% - ${message}`);
});
// Listen for QR code
client.on('qr', (qr) => {
// Save QR code to file or display in UI
saveQRCodeToFile(qr);
});
// Listen for authentication
client.on('authenticated', () => {
console.log('Authenticated successfully!');
});
// Listen for ready event
client.on('ready', async () => {
console.log('Client is ready!');
// Get chats
const chats = await client.getChats();
// Send message to first chat
if (chats.length > 0) {
const result = await client.sendMessage(chats[0].id, 'Hello!');
console.log('Message sent:', result);
}
});
// Handle errors
client.on('error', (error) => {
console.error('Error:', error);
});
// Initialize
client.initialize();API Reference
Client
Constructor Options
{
sessionPath: string, // Path to save session (default: './.wwebjs_auth')
headless: boolean, // Run browser in headless mode (default: true)
userDataDir: string, // Custom user data directory
qrRefreshInterval: number // QR refresh interval in ms (default: 20000)
}Events
qr- Emitted when QR code is generatedclient.on('qr', (qrCode) => { // qrCode is a base64 data URL });ready- Emitted when client is authenticated and readyclient.on('ready', () => { // Client is ready to use });authenticated- Emitted when authentication is successfulclient.on('authenticated', () => { // User is authenticated });loading_screen- Emitted during loading processclient.on('loading_screen', (percent, message) => { console.log(`${percent}% - ${message}`); });message_sent- Emitted when a message is sentclient.on('message_sent', (message) => { console.log('Sent to', message.chatId); });disconnected- Emitted when client disconnectserror- Emitted on errors
Methods
initialize()- Initialize the client and connect to web.arattai.insendMessage(chatId, content, options)- Send a message to a chatgetChats()- Get list of all chatsgetChatById(chatId)- Get a specific chat by IDlogout()- Logout and clear sessiondestroy()- Close browser and cleanup
Examples
See example.js for a complete working example.
Session Management
Sessions are automatically saved to avoid repeated QR scans:
const client = new Client({
sessionPath: './.wwebjs_auth'
});
// First run: QR code will be shown
// Subsequent runs: Session will be restored automatically
client.initialize();Troubleshooting
QR Code Not Appearing
- Check if
web.arattai.inis accessible - Try running with
headless: falseto see what's happening - Check browser console for errors
- Verify Puppeteer can launch Chrome
Session Not Restoring
- Check if session file exists at
sessionPath - Verify file permissions
- Try deleting session file and re-authenticating
Browser Issues
- Ensure Puppeteer can download Chromium
- Check system dependencies
- Try running with
headless: falsefor debugging
TypeScript Support
The library includes TypeScript definitions:
import { Client } from 'arattai-web.js';
const client = new Client({
sessionPath: './.wwebjs_auth'
});
client.on('ready', async () => {
const chats = await client.getChats();
// chats is typed as ChatInfo[]
});Security Considerations
- Session Files: Protect session files - they contain authentication data
- Headless Mode: Use headless mode in production
- Error Handling: Implement proper error handling
- Rate Limiting: Implement rate limiting to avoid being blocked
License
Apache-2.0 License
Disclaimer
This project is not affiliated, associated, authorized, endorsed by, or in any way officially connected with Arattai or any of its subsidiaries or affiliates. Use at your own risk.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
References
Inspired by whatsapp-web.js
