-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwilo.js
More file actions
168 lines (138 loc) · 4.97 KB
/
Copy pathtwilo.js
File metadata and controls
168 lines (138 loc) · 4.97 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// this file will not work on local system
// this file is meant for twilo functions
require('dotenv').config();
const twilio = require('twilio');
const { google } = require('googleapis');
const imaps = require('imap-simple');
const { simpleParser } = require('mailparser');
const chrono = require('chrono-node');
const { EMAIL_USER, EMAIL_PASSWORD, TWILIO_SID, TWILIO_AUTH_TOKEN, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REFRESH_TOKEN } = process.env;
const client = new twilio(TWILIO_SID, TWILIO_AUTH_TOKEN);
exports.handler = async function (context, event, callback) {
const twiml = new twilio.twiml.MessagingResponse();
const userMessage = event.Body.trim().toLowerCase();
if (userMessage === 'hello') {
// Initial greeting and menu options
twiml.message('Hello! Please choose an option by replying with the number:\n1. Fetch Unseen Mail\n2. Fetch Unseen Mail and Create Event\n3. Create Event\n4. Reply to Mail');
} else if (['1', '2', '3', '4'].includes(userMessage)) {
// Handle the user's choice
await handleUserChoice(userMessage, twiml);
} else {
twiml.message('Invalid option. Please reply with "hello" to start or choose an option (1-4).');
}
callback(null, twiml);
};
async function handleUserChoice(choice, twiml) {
switch (choice) {
case '1':
await fetchUnseenMail(twiml);
break;
case '2':
await fetchUnseenMailAndCreateEvent(twiml);
break;
case '3':
await createEvent(twiml);
break;
case '4':
twiml.message('Replying to email... (This feature is not yet implemented)');
break;
}
}
async function fetchUnseenMail(twiml) {
const imapConfig = {
imap: {
user: EMAIL_USER,
password: EMAIL_PASSWORD,
host: 'imap.gmail.com',
port: 993,
tls: true,
}
};
try {
const connection = await imaps.connect(imapConfig);
await connection.openBox('INBOX');
const criteria = ['UNSEEN'];
const options = { bodies: '' };
const emails = await connection.search(criteria, options);
const formattedEmails = await Promise.all(emails.map(async (email) => {
const fullMessage = email.parts.find(part => part.which === '');
const parsedEmail = await simpleParser(fullMessage.body);
let output = From: ${parsedEmail.from.text}\n;
output += Subject: ${parsedEmail.subject}\n;
output += Date: ${parsedEmail.date}\n;
output += Body: ${parsedEmail.text.substring(0, 100)}...\n\n;
return output;
}));
twiml.message(Unseen emails fetched:\n\n${formattedEmails.join('\n')});
} catch (error) {
console.error('Error fetching emails:', error);
twiml.message('Error fetching emails.');
}
}
async function fetchUnseenMailAndCreateEvent(twiml) {
await fetchUnseenMail(twiml);
await createEvent(twiml);
}
async function createEvent(twiml) {
try {
const oauth2Client = new google.auth.OAuth2(
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
'http://localhost:3000/oauth2callback'
);
oauth2Client.setCredentials({ refresh_token: GOOGLE_REFRESH_TOKEN });
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
const event = {
summary: 'New Event',
location: 'Virtual',
description: 'This is a sample event created via Twilio.',
start: {
dateTime: '2025-01-05T09:00:00-07:00',
timeZone: 'America/Los_Angeles',
},
end: {
dateTime: '2025-01-05T10:00:00-07:00',
timeZone: 'America/Los_Angeles',
},
};
const createdEvent = await calendar.events.insert({
calendarId: 'primary',
resource: event,
});
console.log('Event created:', createdEvent.data.summary);
twiml.message('Event created successfully!');
} catch (error) {
console.error('Error creating event:', error);
twiml.message('Error creating event.');
}
}
function parseDateTime(text) {
const results = chrono.parse(text);
if (results.length > 0) {
const startDate = results[0].start.date();
let endDate;
if (results[0].end) {
endDate = results[0].end.date();
} else {
endDate = new Date(startDate.getTime() + 60 * 60 * 1000);
}
const now = new Date();
if (startDate < now) {
const tomorrow = new Date(now);
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(startDate.getHours(), startDate.getMinutes(), 0, 0);
endDate = new Date(tomorrow.getTime() + (endDate.getTime() - startDate.getTime()));
startDate.setTime(tomorrow.getTime());
}
return { startDateTime: startDate, endDateTime: endDate };
}
return null;
}
// For testing purposes
const mockEvent = { Body: 'hello' };
const mockContext = {};
const mockCallback = (err, result) => {
if (err) console.error(err);
else console.log(result.toString());
};
exports.handler(mockContext, mockEvent, mockCallback);