Common Automation Patterns
This page covers battle-tested patterns for common We-Link automation scenarios.
tip
For tracking replies to sent messages automatically, see Message Reply Tracking.
Pattern 1: Queue-Based Outreach
When managing large outreach campaigns, use a persistent queue to track pending, in-progress, and completed actions:
// Simple in-memory queue (use Redis/DB in production)
class OutreachQueue {
constructor(accountId) {
this.accountId = accountId;
this.pending = [];
this.inProgress = new Map();
this.completed = [];
}
add(prospect) {
this.pending.push(prospect);
}
async processNext() {
if (this.pending.length === 0) return null;
const prospect = this.pending.shift();
const response = await welinkApi.connect(this.accountId, prospect);
this.inProgress.set(response.requestId, prospect);
return response.requestId;
}
complete(requestId, result) {
const prospect = this.inProgress.get(requestId);
this.inProgress.delete(requestId);
this.completed.push({ ...prospect, result });
}
}
Pattern 2: Webhook Router
Route different event types to specialized handlers:
const handlers = {
LOGIN_SUCCESS: handleLogin,
CONNECTION_SENT: handleConnection,
MESSAGE_REPLY: handleReply,
SEARCH_COMPLETED: handleSearch,
};
app.post("/webhook", (req, res) => {
res.status(200).send("OK"); // Respond immediately
const eventType = detectEventType(req.body);
const handler = handlers[eventType];
if (handler) {
handler(req.body).catch(console.error);
}
});
Pattern 3: Batch Processing with Rate Limiting
Process large lists while respecting rate limits:
async function batchProcess(accountId, items, action) {
const BATCH_SIZE = 10;
const BATCH_DELAY_MS = 5 * 60 * 1000; // 5 minutes between batches
const ITEM_DELAY_MS = () => randomBetween(10000, 20000);
for (let i = 0; i < items.length; i += BATCH_SIZE) {
const batch = items.slice(i, i + BATCH_SIZE);
for (const item of batch) {
await action(accountId, item);
await sleep(ITEM_DELAY_MS());
}
// Pause between batches
if (i + BATCH_SIZE < items.length) {
console.log(`⏸️ Batch complete. Pausing ${BATCH_DELAY_MS / 1000}s...`);
await sleep(BATCH_DELAY_MS);
}
}
}
Pattern 4: Health Monitor
Periodically check account health and alert on issues:
async function monitorAccounts() {
const accounts = await welinkApi.getAllAccounts();
for (const account of accounts) {
try {
const result = await welinkApi.me(account.accountId);
if (result.status === "FAILED") {
alert(`⚠️ Account ${account.email} session expired`);
}
} catch (error) {
if (error.status === 429) {
alert(`🚧 Account ${account.email} rate limited`);
}
}
}
}
// Run every 6 hours with randomized offset
setInterval(monitorAccounts, 6 * 60 * 60 * 1000 + randomBetween(0, 1800000));