DevCEDTeam / CED

0 stars 0 forks source link

Instructions #13

Open DevCEDTeam opened 1 year ago

DevCEDTeam commented 1 year ago

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

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' }, });

console.log(response); // Optional: Log the response from Mautic

res.status(200).send('Automation triggered successfully.');

} 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({}, {});

console.log('Mautic automation triggered at:', now.toISOString());

});

job.start(); };


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:

gcloud functions deploy mauticAutomation \ --runtime nodejs14