Skip to content
askdareDocumentation

    Booking a consultation

    A visitor on an agency's site wants to talk to someone. The bot shows the next free consultation slots, the visitor clicks one, fills in a short form, and your code books the slot and emails the invitation. Nobody types a date.

    Visitor: Can I book a call about a new website?
    Bot:     Sure, here are the next free slots.
    
             [ Tuesday 10:00, 8 Dec ]  [ Wednesday 14:00, 9 Dec ]
             [ Thursday 10:00, 10 Dec ]  [ None of these work ]
    
    Visitor: Tuesday 10:00, 8 Dec
    Bot:     Great. Leave your details and I'll book it.
             ┌ Book a consultation ──────────────┐
             │ Name, Email, Time, Topic, Consent │
             └───────────────────────────────────┘
    FeatureGuide
    A tool that finds free slots, with a cardTools: cards
    The slots as buttonsReply options: picking a time slot
    A form for the visitor's details and the timeForms and leads
    onLead books the slot and sends the emailForms: what onLead receives

    The code is in site/recipes/booking/, with a calendar in memory in calendar.ts.

    Why the time is in the form

    The model can't fill in a form: every value is typed or picked by the visitor, so a prompt injection can't put another address or a link in your site's own form (Forms). And a click on an option is only a message: it doesn't carry a slot ID to your code.

    So the consultations here happen at three fixed weekly times, and both the slots tool and the form name them. The tool tells the visitor which date each time falls on next; the form's select offers the same three times; onLead books the next free occurrence of the one picked. If your slots don't follow a weekly grid, see Make it yours.

    // calendar.ts
    export const WEEKLY_TIMES = [
      { value: 'tue-10', label: 'Tuesday 10:00', weekday: 2, hour: 10 },
      { value: 'wed-14', label: 'Wednesday 14:00', weekday: 3, hour: 14 },
      { value: 'thu-10', label: 'Thursday 10:00', weekday: 4, hour: 10 },
    ] as const;

    1. The slots tool

    From tools.ts:

    export const findSlots = defineTool({
      name: 'find_slots',
      description:
        'The next free consultation slots. Use it when the visitor wants a consultation, before you suggest a time.',
      input: z.object({}),
      execute: async () => ({ slots: await calendar.freeSlots() }),
      ui: ({ slots }) => ({ slots: slots.map(({ time, label }) => ({ time, label })) }),
      replies: ({ slots }) =>
        slots.length > 0 ? [...slots.map((slot) => slot.label), 'None of these work'] : undefined,
    });

    Three slots and a way out fit the limit of four options. In the chat inside your page, slot-card.tsx draws the slots as a card as well; in the script-tag widget, the buttons are enough.

    2. The booking form

    From forms.ts:

    export const bookingForm = defineForm({
      name: 'booking',
      description:
        'Books a consultation. Show it once the visitor has picked a time from find_slots, or asks to book.',
      title: 'Book a consultation',
      fields: [
        { name: 'name', type: 'text', label: 'Name', required: true },
        { name: 'email', type: 'email', label: 'Email', required: true },
        {
          name: 'time',
          type: 'select',
          label: 'Time',
          options: WEEKLY_TIMES.map(({ value, label }) => ({ value, label })),
          required: true,
          // The bot sees the time, so it can say which slot was booked. Never the contact details.
          toModel: true,
        },
        { name: 'topic', type: 'textarea', label: 'What would you like to talk about?', maxLength: 1000 },
        { name: 'consent', type: 'consent', label: 'You may email me about this consultation.', required: true },
      ],
      submit: 'Book',
      done: 'Thanks! The invitation is on its way to your inbox.',
    });

    The bot's instructions in bot.ts say when to call the tool, when to show the form, and never to ask for a name or an email in the chat.

    3. onLead books it

    onLead gets the values once askdare has validated them against the form: time is one of the three values, email an email address. It runs after the response, so the visitor already sees the form's done text; when the slot was taken in the meantime, the email says so. From route.ts:

    const onLead = async ({ form, values }: ChatLeadReport): Promise<void> => {
      if (form !== 'booking') return;
      const { name, email, time } = values;
      // askdare validated the values against the form: these checks only narrow the types.
      if (typeof name !== 'string' || typeof email !== 'string' || !isWeeklyTime(time)) return;
      const slot = await calendar.book(time, { name, email });
      await sendEmail(
        slot
          ? { to: email, subject: `Your consultation on ${slot.label}`, text: `Hello ${name},\n\nsee you on ${slot.label}. …` }
          : { to: email, subject: 'Your consultation', text: `Hello ${name},\n\nthat time was just taken. …` },
      );
    };
    
    export const { GET, POST, DELETE } = createChatHandler({ bots, onLead });

    A throw in onLead loses the lead (askdare keeps no values), and onError hears the error's class. Retry inside it if your calendar is flaky.

    Make it yours

    ReplaceWithGuide
    calendar in calendar.tsYour calendar or booking system's API: free slots, and a booking that fails when the slot is gone
    sendEmailYour mail provider, or let your booking system send the invitation
    The weekly gridWith slots that move from day to day, drop time from the form and offer the slot through a tool with approval: the visitor approves "Book Tuesday 8 Dec at 10:00 for ana@example.com", and the call runs with the arguments the server storedTools: approving a call
    The form's fieldsUp to 8, of six types. Mark toModel only what the bot needs to go on helpingForms
    Signed-in visitorsTake the name and email from identity in onLead, and leave those fields outSigned-in visitors