Apologies for missing the inclusion of Cron jobs in the previous solution. Here's an updated solution that incorporates Cron jobs to automate the triggering of Mautic automations using Google Cloud Functions:
exports.scheduleAutomation = async (event, context) => {
// Modify the schedule as per your requirement
const schedule = '0 0 *'; // Runs daily at midnight (UTC)
// Check if the current time matches the schedule
const now = new Date();
const cron = require('cron').CronJob;
const job = new cron(schedule, async () => {
// Trigger the Mautic automation using the previously defined function
await exports.mauticAutomation({}, {});
Step 7: Deploy the Cloud Function
- Open your terminal and navigate to your project directory.
- Authenticate with your Google Cloud account by running the following command:
gcloud auth login
- Set your project ID by running the following command:
gcloud config set project your-project-id
- Deploy the Cloud Function by running the following command:
Apologies for missing the inclusion of Cron jobs in the previous solution. Here's an updated solution that incorporates Cron jobs to automate the triggering of Mautic automations using Google Cloud Functions:
Step 1: Install Google Cloud SDK
Step 2: Set up a Google Cloud Project
Step 3: Enable Cloud Functions API
Step 4: Set up Mautic
Step 5: Set up a Node.js project
Step 6: Write the Cloud Function code
index.js
in your project directory.index.js
and write the following code:exports.mauticAutomation = async (req, res) => { // Configure Mautic API credentials const mautic = new Mautic({ apiUrl: 'https://your-mautic-instance.com/api', username: 'your-mautic-username', password: 'your-mautic-password', });
try { // Trigger your desired Mautic automation const response = await mautic.request({ method: 'POST', path: '/automations/trigger/automation_id', data: { contactId: 'contact_id' }, });
} catch (error) { console.error(error); // Optional: Log any errors res.status(500).send('Error triggering automation.'); } };
exports.scheduleAutomation = async (event, context) => { // Modify the schedule as per your requirement const schedule = '0 0 *'; // Runs daily at midnight (UTC)
// Check if the current time matches the schedule const now = new Date(); const cron = require('cron').CronJob; const job = new cron(schedule, async () => { // Trigger the Mautic automation using the previously defined function await exports.mauticAutomation({}, {});
});
job.start(); };
gcloud auth login
gcloud config set project your-project-id
gcloud functions deploy mauticAutomation \ --runtime nodejs14