> ## Documentation Index
> Fetch the complete documentation index at: https://docs.heylua.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Proactive Send Example

> Schedule agent-initiated outreach with LuaJob + Channels.send

## Overview

This recipe shows how to make your agent **reach out on a schedule** — a daily reminder, a follow-up, a status nudge — by combining a [scheduled job](/api/luajob) with the [Channels API](/api/channels). The same pattern works from [webhooks](/api/luawebhook) and tools.

## What It Does

* Runs every morning on a cron schedule
* Looks up who needs a reminder
* Sends each a WhatsApp message with `Channels.send`
* Falls back to an approved template when the recipient's 24-hour window is closed
* Every send is recorded to the recipient's conversation thread, so replies continue naturally

## Complete Code

```typescript theme={null}
import { LuaJob, Channels } from 'lua-cli';

const appointmentReminders = new LuaJob({
  name: 'appointment-reminders',
  description: 'Send next-day appointment reminders every morning',
  schedule: {
    type: 'cron',
    expression: '0 9 * * *' // every day at 9:00 AM
  },
  execute: async () => {
    const appointments = await getTomorrowsAppointments();

    for (const appt of appointments) {
      try {
        const result = await Channels.send({
          channel: 'whatsapp',
          to: { userId: appt.userId },
          text: `Hi ${appt.name}! Reminder: your appointment is tomorrow at ${appt.time}. Reply here if you need to reschedule.`,
          options: { whatsapp: { onClosedWindow: 'fail' } }
        });

        console.log(`Reminded ${appt.userId} (delivered: ${result.delivered})`);
      } catch {
        // Window closed — start the conversation with an approved template
        await Channels.whatsapp.sendTemplate({
          to: { userId: appt.userId },
          templateName: 'appointment_reminder',
          languageCode: 'en_US',
          components: [
            { type: 'BODY', parameters: [{ type: 'text', text: appt.time }] }
          ],
          messageContext: `Reminded ${appt.name} about their appointment tomorrow at ${appt.time}`
        });
      }
    }
  }
});

export default appointmentReminders;
```

## Key Concepts

<AccordionGroup>
  <Accordion title="Window-aware sending">
    Free-form WhatsApp is only allowed within 24 hours of the user's last message. Setting `onClosedWindow: 'fail'` makes `Channels.send` throw when the window is closed, so the `catch` block can send an approved template instead. (Omit the option to let it **queue** automatically — see [Proactive Messaging](/channels/proactive-messaging#the-whatsapp-24-hour-window).)
  </Accordion>

  <Accordion title="No conversational context in jobs">
    A scheduled job runs outside any conversation, so you address recipients explicitly — here by `to.userId`. You can also target a raw `phoneNumber` or `email` for cold outreach.
  </Accordion>

  <Accordion title="messageContext keeps the agent coherent">
    When you send a template, `messageContext` is the plain-text summary recorded to the thread. When the user replies, your agent sees "Reminded … about their appointment" and continues naturally — not a blank slate.
  </Accordion>
</AccordionGroup>

## Variation: confirm on a webhook event

The same `Channels.send` call works from a [webhook](/api/luawebhook) — for example, confirming a payment the moment your payment provider fires its event:

```typescript theme={null}
import { LuaWebhook, Channels } from 'lua-cli';

const paymentConfirmation = new LuaWebhook({
  name: 'payment-confirmation',
  description: 'Notify the customer when their payment succeeds',
  execute: async (event) => {
    const { customerId, amount } = event.body;

    await Channels.send({
      channel: 'whatsapp',
      to: { userId: customerId },
      text: `Payment of $${amount} received — thank you! 🎉`
    });

    return { notified: true };
  }
});

export default paymentConfirmation;
```

## Next steps

<CardGroup cols={2}>
  <Card title="Channels API" icon="paper-plane" href="/api/channels">
    Full reference for send, sendTemplate, and email.send
  </Card>

  <Card title="Proactive Messaging" icon="bell" href="/channels/proactive-messaging">
    The model behind agent-initiated messages
  </Card>

  <Card title="LuaJob" icon="calendar" href="/api/luajob">
    Define scheduled tasks
  </Card>

  <Card title="Webhooks" icon="bolt" href="/api/luawebhook">
    React to external events
  </Card>
</CardGroup>
