-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (77 loc) · 2.93 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
require('dotenv-json')();
const createPresignedUrl = require('./s3');
const async = require('async');
const twilioClient = require('./twilio');
const Google = require('./google');
// Query spreadsheet data values for today's month and day
//
const queryDates = (data) => {
// find today's date
const today = new Date();
const this_year = today.getFullYear();
today.setYear(0);
today.setHours(0, 0, 0, 0);
// stash row numbers when we find matching dates
let date_rows = [];
data.forEach((timestamp, index) => {
const date = new Date(timestamp);
const year = date.getFullYear();
date.setYear(0);
date.setHours(0, 0, 0, 0);
if (this_year !== year && date.getTime() === today.getTime()) {
// skip the header row
date_rows.push(index + 2);
}
});
return date_rows;
}
// Fetch specific message
exports.handler = async (sheet_row, event, context) => {
// setup a google api client
const google = new Google();
const response = await google.init();
// fetch the memory dates from the sheet
console.log('ok, ready. go get the data sheet');
let date_rows;
let message_data;
try {
const data = await google.getSheetData(process.env.GOOGLE_SHEET_ID, process.env.GOOGLE_SHEET_RANGE);
// find all of the rows with a matching date unless a specific row was specified
if( sheet_row ) {
date_rows = [sheet_row];
} else{
date_rows = queryDates(data);
}
console.dir('Query rows for matchind date : ' + JSON.stringify(date_rows));
// re-fetch a random message for this date
const picker = Math.floor(Math.random() * date_rows.length);
const cell = "Form Responses 1!A" + date_rows[picker] + ":C" + date_rows[picker];
message_data = await google.getSheetData(process.env.GOOGLE_SHEET_ID, cell);
console.dir(message_data);
} catch (e) {
console.error(e);
}
// assemble the message details
const message = message_data[0][1];
const year = new Date(message_data[0][0]).getFullYear();
const sms = year + " : " + message;
console.dir('message -> ' + sms);
let media_url = undefined;
if( message_data[0][2] ) {
// example: https://memories-photo-archive.s3.us-east-2.amazonaws.com/2023-november-9.jpg
const media_cell = message_data[0][2];
const objectKey = media_cell.substring(media_cell.lastIndexOf('/') + 1);
console.log('objectKey -> ' + objectKey);
media_url = await createPresignedUrl(objectKey);
}
console.log('media -> ' + media_url);
// send out the message via SMS
try {
let twilio_result = await twilioClient.sendSMS(sms, process.env.RECIPIENT_PHONE, media_url);
console.log('Twilio send success! ' + twilio_result.accountSid);
} catch(e) {
console.log('failed to send sms');
console.dir(e);
}
};