I have a google sheet which contains a commodity & the fulfilment date. I need a way that I can receive an automated reminder at least 7 days prior to the fulfilment date, so that I can minimise delay/shortage.
This is a snippet of the spreadsheet:
Currently I have this code which is able to send whatsapp messages using Twilio sandbox number, when I manually run it:
// A Message Sender
const { google } = require('googleapis');
// configure a JWT auth client
const privatekey = require("./privatekey.json");
const authClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/spreadsheets.readonly']);
// authentication
authClient.authorize()
.then(function (tokens) {
// console.log(tokens);
console.log("Authentication successfull.n");
})
.catch(function (error) {
throw (error);
});
// things we shouldn’t share with our code
const secrets = require("./secrets.json");
const sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
// ID of the spreadsheet to retrieve data from
// A1 notation of the values to retrieve
// authorized client
spreadsheetId: secrets.spreadsheet_id,
range: 'Sheet1!A2:E',
auth: authClient
})
.then(function (response) {
const rows = response.data.values || [];
if (rows.length) {
sendMessage(rows);
}
})
.catch(function (err) {
console.log(err);
});
const accountSid = secrets.account_sid;
const authToken = secrets.auth_token;
const client = require('twilio')(accountSid, authToken);
const sandboxNumber = secrets.sandbox_number;
function sendMessage(rows) {
// stop condition
if (!rows.length) {
console.log("---------------------------------");
return;
}
// take information from the first row
const firstRow = rows.shift();
const fulfilment_date = firstRow[0];
const commodity = firstRow[1];
const quantity = firstRow[2];
const recipient = firstRow[3];
const phone_number = firstRow[4];
// send the message
client.messages
.create({
from: 'whatsapp:' + sandboxNumber,
to: 'whatsapp:' + phone_number,
body: `TEST: Next fulfilment is coming up on ${fulfilment_date}nfor ${commodity} - ${quantity} .` +
` A message for ${recipient}`
})
.then(function (message) {
console.log(message.sid + 'n');
sendMessage(rows);
})
.catch(function (err) {
console.log(err);
});
}
Can someone please help me out with automating this in a way that I can receive the reminders 7 days before fulfilment date?