The SimpleSpa Clients API is very powerful and ideal for Scheduled Tasks; let's properly integrate the SimpleSpa Clients API with OpenAI, and also automate it on a daily basis using Node.js with CRON.
Copy mkdir simplespa-ai-messages
cd simplespa-ai-messages
npm init -y
npm install axios openai node-cron dotenv
Copy OPENAI_API_KEY=your_openai_api_key_here
CLIENTS_API_URL=https://my.simplespa.com/api/clients.php
CLIENTS_API_KEY=your_simplespa_api_key_here
Copy require('dotenv').config();
const axios = require('axios');
const { Configuration, OpenAIApi } = require('openai');
const cron = require('node-cron');
const fs = require('fs');
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY
}));
const fetchClients = async () => {
try {
const response = await axios.post(
process.env.CLIENTS_API_URL,
{
page: 1,
per_page: 50, // You can adjust this
last_visit_after: '2023-01-01' // Example filter
},
{
headers: {
'Authorization': `Bearer ${process.env.CLIENTS_API_KEY}`,
'Content-Type': 'application/json'
}
}
);
return response.data.clients || [];
} catch (error) {
console.error('Error fetching clients:', error.response?.data || error.message);
return [];
}
};
const generateMessage = async (client) => {
try {
const prompt = `Write a short, friendly SMS inviting ${client.firstname} ${client.lastname} to schedule their next appointment. Mention their last visit was on ${client.last_visit || "an unknown date"}.`;
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }]
});
return completion.data.choices[0].message.content.trim();
} catch (error) {
console.error('Error generating message:', error.response?.data || error.message);
return null;
}
};
const runScheduler = async () => {
console.log("Starting SimpleSpa AI Scheduler...");
const clients = await fetchClients();
if (clients.length === 0) {
console.log('No clients found.');
return;
}
const results = [];
for (const client of clients) {
const message = await generateMessage(client);
if (message) {
results.push({
client_id: client.client_id,
name: `${client.firstname} ${client.lastname}`,
mobile: client.mobile,
message: message
});
}
}
// Save results to a JSON file
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
fs.writeFileSync(`messages-${timestamp}.json`, JSON.stringify(results, null, 2));
console.log(`Generated ${results.length} personalized messages.`);
};
// Schedule this to run daily at 8:00 AM
cron.schedule('0 8 * * *', () => {
runScheduler();
});
runScheduler(); // Run immediately on startup
✅ fetchClients() calls from the SimpleSpa API /api/clients.php with a real API KEY