Skip to main content
Automate debt collection with personalized WhatsApp messages using context-aware AI.
import { Agent, openai, Runflow } from '@runflow-ai/sdk';
import { createTool } from '@runflow-ai/sdk';
import { z } from 'zod';

// Collections agent with empathy and context
const collectionsAgent = new Agent({
  name: 'Collections AI',
  instructions: `You are an empathetic debt collection agent.
  - Analyze customer history and payment patterns
  - Create personalized, respectful messages
  - Offer payment plan options
  - Never be aggressive or rude`,

  model: openai('gpt-4o'),

  memory: {
    maxTurns: 10,
  },

  tools: {
    sendWhatsApp: createTool({
      id: 'send-whatsapp',
      description: 'Send WhatsApp message',
      inputSchema: z.object({
        phone: z.string(),
        message: z.string(),
      }),
      execute: async ({ context }) => {
        // Send WhatsApp logic
        return { sent: true };
      },
    }),
    getPaymentHistory: createTool({
      id: 'get-payment-history',
      description: 'Get customer payment history',
      inputSchema: z.object({
        customerId: z.string(),
      }),
      execute: async ({ context }) => {
        const history = await fetchPaymentHistory(context.customerId);
        return { history };
      },
    }),
  },
});

// Process overdue invoice
async function processOverdueInvoice(invoice: any) {
  // Set customer context
  Runflow.identify({
    type: 'customer',
    value: invoice.customerId,
    userId: invoice.customerId,
  });

  const result = await collectionsAgent.process({
    message: `Create a personalized WhatsApp message for customer ${invoice.customerName}.
    Invoice: ${invoice.id}
    Amount: $${invoice.amount}
    Days overdue: ${invoice.daysOverdue}
    Previous payment history: Check using the tool.

    If history is good, offer a payment plan. Send via WhatsApp.`,
    sessionId: `collection_${invoice.id}`,
  });

  console.log('Collection message sent:', result);
}

Features

  • Empathetic: Respectful and professional tone
  • Context-Aware: Analyzes payment history
  • Personalized: Custom messages per customer
  • WhatsApp Integration: Sends messages automatically

Next Steps