The setReminder
function schedules a Google Calendar event to send a Twilio message at a given time, handling OAuth authentication and calendar ID resolution.
npm run import -- "twilio reminder"
var importer = require('../Core');
var google = require('googleapis');
var calendar = google.calendar('v3');
var util = require('util');
var chrono = require('chrono-node');
var insertEvent = util.promisify(calendar.events.insert);
var {
getOauthClient,
correctCalendarId
} = importer.import("lookup calendar name",
"import google calendar api");
var options = {
calendarId: 'aws'
};
function setReminder(to, message, time) {
const time = chrono.parseDate(time);
const event = {
summary: 'send twilio message',
description: JSON.stringify({
message,
to
}),
start: {dateTime: new Date(time.getTime())},
end: {dateTime: new Date(time.getTime() + 30 * 60 * 1000)}
}
return (options.auth ? Promise.resolve([]) : getOauthClient(options))
.then(() => correctCalendarId(options))
.then(() => insertEvent({
calendarId: options.calendarId,
auth: options.auth,
resource: event
}))
}
module.exports = incomingTwilio;
// Import required modules
const { google } = require('googleapis');
const { chronoNode } = require('chrono-node');
const util = require('util');
const importer = require('../Core');
// Create a Google Calendar API client
const calendar = google.calendar('v3');
// Util functions
const promisify = util.promisify;
const parseDate = chronoNode.parseDate;
// API constants
const MIN_EVENT_DURATION = 30 * 60 * 1000; // 30 minutes in milliseconds
// Get OAuth client and correct calendar ID
const { getOauthClient, correctCalendarId } = importer.import([
'lookup calendar name',
'import google calendar api',
]);
// Default calendar ID
const DEFAULT_CALENDAR_ID = 'aws';
// Set reminder function
/**
* Set a reminder in the Google Calendar.
*
* @param {string} to - The recipient's phone number.
* @param {string} message - The message to be sent.
* @param {string} time - The reminder time in a format that can be parsed by chrono-node.
* @returns {Promise} - A promise that resolves when the reminder is set.
*/
function setReminder(to, message, time) {
// Parse the reminder time
const eventTime = parseDate(time).getTime();
// Create the event
const event = {
summary: 'Send Twilio message',
description: JSON.stringify({ message, to }),
start: { dateTime: new Date(eventTime) },
end: { dateTime: new Date(eventTime + MIN_EVENT_DURATION) },
};
// Set the calendar ID
const options = { calendarId: DEFAULT_CALENDAR_ID, auth: true };
// Set the reminder
return Promise.resolve(options)
.then((authOptions) => getOauthClient(authOptions))
.then((oauthClient) => correctCalendarId({...options, oauthClient }))
.then((calendarId) => promisify(calendar.events.insert)({
calendarId,
auth: oauthClient,
resource: event,
}));
}
module.exports = setReminder;
This code defines a function setReminder
that schedules a Google Calendar event to send a Twilio message at a specified time.
Functionality:
Dependencies:
googleapis
for Google Calendar API, util
for promisifying functions, chrono-node
for parsing dates, and custom modules from ../Core
.Google Calendar Setup:
calendar.events.insert
function for creating events.Oauth and Calendar ID Handling:
getOauthClient
and correctCalendarId
from a custom module to handle OAuth authentication and calendar ID resolution.setReminder
Function:
to
, message
, and time
as input.time
string using chrono-node
.getOauthClient
and correctCalendarId
to handle authentication and calendar ID.insertEvent
to create the event on the specified calendar.Export:
setReminder
function.Purpose:
The code provides a way to schedule Twilio messages as Google Calendar events, likely for reminders or automated notifications.