Need help with ai chatbot workflow

This is my AI chatbot workflow, the chatbot is fine and it can book appointments on google calendar. However, it has a bad habit of suggesting the same times for appointments in multiple conversations which results in double booking. I want to know how the bot can possibly scout google calendar and see which spots are taken and then suggest open times. I’m a beginner so I apologize if I sound stupid or am asking an elementary question

@Tentordev Here’s a complete HTML file that demonstrates how to check Google Calendar for available time slots before suggesting appointment times.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calendar Availability Checker</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f7f9;
            color: #333;
        }
        .container {
            background-color: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #1a73e8;
            margin-top: 0;
        }
        .explanation {
            background-color: #e8f0fe;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: 600;
        }
        input, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #1a73e8;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-top: 10px;
        }
        button:hover {
            background-color: #0d62c9;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 8px;
            display: none;
        }
        .available {
            background-color: #e6f4ea;
            border: 1px solid #34a853;
        }
        .unavailable {
            background-color: #fce8e6;
            border: 1px solid #ea4335;
        }
        .time-slot {
            padding: 8px;
            margin: 5px 0;
            border-radius: 4px;
        }
        .code-sample {
            background-color: #f1f3f4;
            padding: 15px;
            border-radius: 8px;
            overflow-x: auto;
            margin-top: 30px;
            font-family: monospace;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Calendar Availability Checker</h1>
        
        <div class="explanation">
            <p>To prevent double-booking, your chatbot needs to check Google Calendar's API for existing events before suggesting appointment times.</p>
            <p>This demo simulates how that process works. Select a date and duration to see available time slots.</p>
        </div>

        <div class="form-group">
            <label for="date">Date:</label>
            <input type="date" id="date" required>
        </div>

        <div class="form-group">
            <label for="duration">Appointment Duration:</label>
            <select id="duration">
                <option value="30">30 minutes</option>
                <option value="60">60 minutes</option>
                <option value="90">90 minutes</option>
                <option value="120">120 minutes</option>
            </select>
        </div>

        <button onclick="checkAvailability()">Check Available Times</button>

        <div id="result" class="result"></div>

        <div class="code-sample">
            <h3>How it works in code:</h3>
            <p>Your chatbot would use the Google Calendar API to check for free/busy times:</p>
            <pre>
// Pseudocode for checking availability
async function checkCalendarAvailability(date, duration) {
    // Set up the request to Google Calendar API
    const request = {
        timeMin: new Date(date + 'T09:00:00').toISOString(),
        timeMax: new Date(date + 'T17:00:00').toISOString(),
        items: [{ id: 'primary' }]
    };

    // Make the API call
    const response = await calendar.freebusy.query({
        auth: authClient,
        resource: request
    });

    // Process the response to find available slots
    const busySlots = response.data.calendars.primary.busy;
    const availableSlots = findAvailableSlots(busySlots, duration);
    
    return availableSlots;
}

function findAvailableSlots(busySlots, duration) {
    // Logic to find gaps between busy times that can fit the appointment
    // Return array of available time slots
}
            </pre>
        </div>
    </div>

    <script>
        // This is a simulation since we can't access Google Calendar without authentication
        function checkAvailability() {
            const date = document.getElementById('date').value;
            const duration = parseInt(document.getElementById('duration').value);
            const resultDiv = document.getElementById('result');
            
            if (!date) {
                alert('Please select a date');
                return;
            }
            
            // Simulate checking Google Calendar API
            // In a real implementation, you would make an API call to Google Calendar
            
            // For demo purposes, we'll simulate some busy times
            const busyTimes = [
                { start: '09:00', end: '10:30' },
                { start: '11:00', end: '12:00' },
                { start: '13:30', end: '14:30' },
                { start: '15:00', end: '16:00' }
            ];
            
            // Generate available time slots
            const availableSlots = generateAvailableSlots(busyTimes, duration);
            
            // Display results
            if (availableSlots.length > 0) {
                resultDiv.className = 'result available';
                resultDiv.innerHTML = '<h3>Available Time Slots:</h3>';
                
                availableSlots.forEach(slot => {
                    resultDiv.innerHTML += `<div class="time-slot available">${slot.start} - ${slot.end}</div>`;
                });
                
                resultDiv.innerHTML += '<p>These times are free in your calendar.</p>';
            } else {
                resultDiv.className = 'result unavailable';
                resultDiv.innerHTML = '<h3>No available slots</h3><p>No suitable time slots found for the selected duration. Please try another date or shorter duration.</p>';
            }
            
            resultDiv.style.display = 'block';
        }
        
        function generateAvailableSlots(busyTimes, duration) {
            const availableSlots = [];
            const dayStart = 9; // 9 AM
            const dayEnd = 17; // 5 PM
            
            // Sort busy times by start time
            busyTimes.sort((a, b) => a.start.localeCompare(b.start));
            
            // Check before first appointment
            const firstBusy = convertTimeToMinutes(busyTimes[0].start);
            if (firstBusy - dayStart * 60 >= duration) {
                availableSlots.push({
                    start: convertMinutesToTime(dayStart * 60),
                    end: convertMinutesToTime(firstBusy)
                });
            }
            
            // Check between appointments
            for (let i = 0; i < busyTimes.length - 1; i++) {
                const currentEnd = convertTimeToMinutes(busyTimes[i].end);
                const nextStart = convertTimeToMinutes(busyTimes[i+1].start);
                
                if (nextStart - currentEnd >= duration) {
                    availableSlots.push({
                        start: convertMinutesToTime(currentEnd),
                        end: convertMinutesToTime(nextStart)
                    });
                }
            }
            
            // Check after last appointment
            const lastBusy = convertTimeToMinutes(busyTimes[busyTimes.length - 1].end);
            if (dayEnd * 60 - lastBusy >= duration) {
                availableSlots.push({
                    start: convertMinutesToTime(lastBusy),
                    end: convertMinutesToTime(dayEnd * 60)
                });
            }
            
            return availableSlots;
        }
        
        function convertTimeToMinutes(timeStr) {
            const [hours, minutes] = timeStr.split(':').map(Number);
            return hours * 60 + minutes;
        }
        
        function convertMinutesToTime(totalMinutes) {
            const hours = Math.floor(totalMinutes / 60);
            const minutes = totalMinutes % 60;
            return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
        }
    </script>
</body>
</html>

Thank you so much for this

You are welcome and Kindly mark as solution if this was helpful to you.