https://discord.gg/botpress logo
// actions/getFreeBusySlots.js const axios = requi...
# 💻developers
g
// actions/getFreeBusySlots.js const axios = require('axios'); const googleCalendarApiKey = 'YOUR_API_KEY'; // Replace with your actual API key const calendarId = 'YOUR_CALENDER_ID'; // Replace with your actual calender ID async function getFreeBusySlots(workflow, bp, event) { const { DateTime } = require('luxon'); if (workflow.formatdate) { try { const startDate = DateTime.fromFormat(workflow.formatdate, 'yyyy-MM-dd'); if (!startDate.isValid) { throw new Error('Invalid date format'); } const startTime = startDate.set({ hour: 10, minute: 0, second: 0, millisecond: 0 }).toISO(); const endTime = startDate.set({ hour: 18, minute: 0, second: 0, millisecond: 0 }).toISO(); const requestBody = { timeMin: startTime, timeMax: endTime, timeZone: 'GMT+5', items: [{ id: calendarId }] }; const headers = { 'Content-Type': 'application/json', Authorization:
Bearer ${googleCalendarApiKey}
}; const url = `https://www.googleapis.com/calendar/v3/freeBusy`; const response = await axios.post(url, requestBody, { headers }); if (!response.data.calendars || !response.data.calendars[calendarId]) { throw new Error('Invalid API response structure'); } workflow.freeBusySlots = response.data.calendars[calendarId].busy; } catch (error) { bp.logger.error('Error:', error.message); bp.logger.error('Full Error:', error); if (error.response) { bp.logger.error('Response Data:', error.response.data); bp.logger.error('Response Status:', error.response.status); bp.logger.error('Response Headers:', error.response.headers); } workflow.freeBusySlots = []; } } else { workflow.freeBusySlots = []; } } return getFreeBusySlots(workflow, bp, event);