forked from tanaikech/ToolsForMCPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement_calendar.js
More file actions
327 lines (314 loc) · 15 KB
/
management_calendar.js
File metadata and controls
327 lines (314 loc) · 15 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* Management of Google Calendar
* Updated on 20250812 10:26
*/
/**
* This function retrieves schedules (events) from the specific dates on Google Calendar.
* @private
*/
function search_schedule_on_Google_Calendar(object = {}) {
const { calendarId, start, end, search = "" } = object;
let result;
try {
let startDate = new Date(start);
let endDate = new Date(end);
const st = startDate.getTime();
const et = endDate.getTime();
if (st == et) {
startDate.setHours(0, 0, 0, 0);
endDate.setHours(0, 0, 0, 0);
endDate.setDate(endDate.getDate() + 1);
} else if (st > et) {
startDate = new Date(et);
endDate = new Date(st);
}
const cal = calendarId ? CalendarApp.getCalendarById(calendarId) : defaultCalendarId ? CalendarApp.getCalendarById(defaultCalendarId) : CalendarApp.getDefaultCalendar();
const events = cal.getEvents(startDate, endDate, search ? { search } : null);
let messagesStr, jsonSchemaStr, forTable;
if (events.length > 0) {
const timezone = cal.getTimeZone();
const coundEvent = events.length;
const eventObj = events.map(e => ({
title: e.getTitle(),
startTime: Utilities.formatDate(e.getStartTime(), timezone, "yyyy-MM-dd HH:mm:ss"),
endTime: Utilities.formatDate(e.getEndTime(), timezone, "yyyy-MM-dd HH:mm:ss"),
location: e.getLocation() || "No location",
description: e.getDescription() || "No description",
eventId: e.getId(),
}));
const jsonSchema = {
type: "array",
items: {
type: object,
properties: {
title: {
type: "string",
description: "Event title."
},
startTime: {
type: "string",
description: "Start time of the event. The date format is 'yyyy-MM-dd HH:mm:ss'."
},
endTime: {
type: "string",
description: "End time of the event. The date format is 'yyyy-MM-dd HH:mm:ss'."
},
location: {
type: "string",
description: "Location of the event."
},
description: {
type: "string",
description: "Description of the event."
},
eventId: {
type: "string",
description: "Event ID."
},
},
required: ["title", "startTime", "endTime", "description", "eventId"]
}
};
messagesStr = [
`${coundEvent} events were found.`,
`The events from Google Calendar are put in "Data" of a JSON array.`,
`<Data>${JSON.stringify(eventObj)}</Data>`,
];
jsonSchemaStr = [
`The JSON schema of "Data" is as follows. Understand "Data" using this JSON schema.`,
`<JSONSchema>${JSON.stringify(jsonSchema)}</JSONSchema>`,
];
forTable = [
`Final display is as follows.`,
`<FinalDisplay>`,
`As an additional mission, create the retrieved events in the month, the week, or the day calendar as a 2-dimensional array.`,
`In the case of the month calendar, the 1st row is the day of the week from Sunday to Saturday. After the 2nd row, those are the days in a month. Each day is required to be included in each cell.`,
`In the case of the week calendar, the 1st row is the day of the week from Sunday to Saturday. The 2nd row is the days of the week. Each day is required to be included in each cell.`,
`In the case of the day calendar, the 1st row is the day. After the 2nd row, the hours with 24 h.`,
`Use the simple identification name of each event in the calendar. Additionally, display the detailed information about each event, which corresponds to the simple identification name of each event, below the calendar.`,
`The names of the day of the week should be omitted like "Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", and "Sat.".`,
`In order to keep the visibility, adjust the visibility by adding spaces, and use the colorful texts.`,
`Display the 2-dimensional array in a table with the box-drawing characters, and all rows are required to be split with the horizontal lines using the box-drawing characters.`,
`</FinalDisplay>`,
];
} else {
messagesStr = ["New events were not found."];
jsonSchemaStr = [];
forTable = [];
}
const text = [...messagesStr, ...jsonSchemaStr, ...forTable].join("\n");
result = { content: [{ type: "text", text }], isError: false };
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function create a schedule (event) on Google Calendar.
* @private
*/
function create_schedule_on_Google_Calendar(object = {}) {
const { calendarId, startDatetime, endDatetime, title, description, location, guests, googleMeet = false } = object;
let result;
/**
* Check API.
*/
const apiName = "Calendar";
if (isAPIAtAdvancedGoogleServices_(apiName).api == "disable") {
result = { content: [{ type: "text", text: `${apiName} API is disabled. Please enable ${apiName} API in the Advanced Google services.` }], isError: true };
return { jsonrpc: "2.0", result };
}
try {
const cal = calendarId ? CalendarApp.getCalendarById(calendarId) : defaultCalendarId ? CalendarApp.getCalendarById(defaultCalendarId) : CalendarApp.getDefaultCalendar();
const calId = cal.getId();
const timeZone = Session.getScriptTimeZone();
const requestBody = {};
const optionalArgs = { sendUpdates: "all" };
if (startDatetime) {
requestBody.start = { dateTime: Utilities.parseDate(startDatetime, timeZone, "yyyy-MM-dd HH:mm:ss").toISOString() };
}
if (endDatetime) {
requestBody.end = { dateTime: Utilities.parseDate(endDatetime, timeZone, "yyyy-MM-dd HH:mm:ss").toISOString() };
}
if (title) {
requestBody.summary = title;
}
if (description) {
requestBody.description = description;
}
if (location) {
requestBody.location = location;
}
if (guests) {
requestBody.attendees = guests.map(email => ({ email }));
}
if (googleMeet) {
requestBody.conferenceData = { createRequest: { requestId: Utilities.getUuid(), conferenceSolutionKey: { type: "hangoutsMeet" } } };
optionalArgs.conferenceDataVersion = 1;
}
const res = Calendar.Events.insert(requestBody, calId, optionalArgs);
result = { content: [{ type: "text", text: `An event (event id is "${res.id}") was created on the calendar ${cal.getName()}.` }], isError: false };
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function deletes schedules (events) from Google Calendar.
* @private
*/
function delete_schedules_on_Google_Calendar(object = {}) {
const { calendarId, eventIds = [] } = object;
let result;
try {
if (eventIds.length > 0) {
const cal = calendarId ? CalendarApp.getCalendarById(calendarId) : defaultCalendarId ? CalendarApp.getCalendarById(defaultCalendarId) : CalendarApp.getDefaultCalendar();
const n = eventIds.reduce((ar, id) => {
const event = cal.getEventById(id);
if (event) {
event.deleteEvent();
ar.push(id);
}
return ar;
}, []);
result = { content: [{ type: "text", text: `${n.length} events were deleted from the calendar "${cal.getName()}".` }], isError: false };
} else {
result = { content: [{ type: "text", text: "No event IDs." }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function updates schedule (event) from Google Calendar.
* @private
*/
function update_schedule_on_Google_Calendar(object = {}) {
const { calendarId, eventId, startDatetime, endDatetime, title, description, location, guests, googleMeet = false, removeGuests } = object;
let result;
/**
* Check API.
*/
const apiName = "Calendar";
if (isAPIAtAdvancedGoogleServices_(apiName).api == "disable") {
result = { content: [{ type: "text", text: `${apiName} API is disabled. Please enable ${apiName} API in the Advanced Google services.` }], isError: true };
return { jsonrpc: "2.0", result };
}
try {
if (eventId) {
const cal = calendarId ? CalendarApp.getCalendarById(calendarId) : defaultCalendarId ? CalendarApp.getCalendarById(defaultCalendarId) : CalendarApp.getDefaultCalendar();
const event = cal.getEventById(eventId);
if (event) {
const calId = cal.getId();
const eventId = event.getId().split("@")[0];
const timeZone = Session.getScriptTimeZone();
const requestBody = {};
const optionalArgs = { sendUpdates: "all" };
if (startDatetime) {
requestBody.start = { dateTime: Utilities.parseDate(startDatetime, timeZone, "yyyy-MM-dd HH:mm:ss").toISOString() };
}
if (endDatetime) {
requestBody.end = { dateTime: Utilities.parseDate(endDatetime, timeZone, "yyyy-MM-dd HH:mm:ss").toISOString() };
}
if (title) {
requestBody.summary = title;
}
if (description) {
requestBody.description = description;
}
if (location) {
requestBody.location = location;
}
if (guests) {
requestBody.attendees = guests.map(email => ({ email }));
}
if (removeGuests) {
const guests = event.getGuestList().map(e => e.getEmail());
const newGuests = guests.filter(e => !removeGuests.includes(e));
requestBody.attendees = newGuests.map(email => ({ email }));
}
if (googleMeet) {
requestBody.conferenceData = { createRequest: { requestId: Utilities.getUuid(), conferenceSolutionKey: { type: "hangoutsMeet" } } };
optionalArgs.conferenceDataVersion = 1;
}
Calendar.Events.patch(requestBody, calId, eventId, optionalArgs);
result = { content: [{ type: "text", text: `An event (event id is "${event.getId()}") was updated on the calendar ${cal.getName()}.` }], isError: false };
} else {
result = { content: [{ type: "text", text: `No event of the event ID ${eventId} on the calendar ${cal.getName()}.` }], isError: true };
}
} else {
result = { content: [{ type: "text", text: "No eventId." }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
// Descriptions of the functions.
const descriptions_management_calendar = {
search_schedule_on_Google_Calendar: {
description: "Use to search the schedules (events) on Google Calendar by providing the date range.",
parameters: {
type: "object",
properties: {
calendarId: { description: "Calendar ID.", type: "string" },
start: { description: "Start date for searching the schedule and events on Google Calendar. The format of the date should be ISO format (yyyy-MM-dd).", type: "string" },
end: { description: "End date for searching the schedule and events on Google Calendar. The format of the date should be ISO format (yyyy-MM-dd).", type: "string" },
search: { description: "Search string for searching the schedule and events on Google Calendar. Even only when the start and end are provided, the correct results are returned.", type: "string" },
},
required: ["start", "end"]
}
},
create_schedule_on_Google_Calendar: {
description: "Use to create a new schedule (event) on Google Calendar.",
parameters: {
type: "object",
properties: {
calendarId: { description: "Calendar ID.", type: "string" },
startDatetime: { description: `Start datetime of the schedule (event). The format of the date should be ISO format ("yyyy-MM-dd HH:mm:ss").`, type: "string" },
endDatetime: { description: `End datetime of the schedule (event). The format of the date should be ISO format ("yyyy-MM-dd HH:mm:ss").`, type: "string" },
title: { description: `Title of schedule (event).`, type: "string" },
description: { description: `Location of the schedule (event).`, type: "string" },
location: { description: `Description of schedule (event).`, type: "string" },
guests: { description: `Email addresses that should be added as guests.`, type: "array", items: { type: "string", description: "Guest email." } },
googleMeet: { description: `The default is false. When Google Meet is used, set this as true.`, type: "boolean" },
},
required: ["startDatetime", "endDatetime", "title", "description"]
}
},
delete_schedules_on_Google_Calendar: {
description: "Use to delete schedules (events) from Google Calendar.",
parameters: {
type: "object",
properties: {
calendarId: { description: "Calendar ID.", type: "string" },
eventIds: { description: "Event IDs on Google Calendar.", type: "array", items: { description: "Event ID on Google Calendar.", type: "string" } },
},
required: ["eventIds"]
}
},
update_schedule_on_Google_Calendar: {
description: "Use to update the schedule (event) on Google Calendar.",
parameters: {
type: "object",
properties: {
calendarId: { description: "Calendar ID.", type: "string" },
eventId: { description: "Event ID of the schedule (event).", type: "string" },
startDatetime: { description: `Start datetime of the schedule (event). The format of the date should be ISO format ("yyyy-MM-dd HH:mm:ss").`, type: "string" },
endDatetime: { description: `End datetime of the schedule (event). The format of the date should be ISO format ("yyyy-MM-dd HH:mm:ss").`, type: "string" },
title: { description: `Title of schedule (event).`, type: "string" },
description: { description: `Location of the schedule (event).`, type: "string" },
location: { description: `Description of schedule (event).`, type: "string" },
guests: { description: `Email addresses that should be added as guests.`, type: "array", items: { type: "string", description: "Guest email." } },
googleMeet: { description: `The default is false. When Google Meet is used, set this as true.`, type: "boolean" },
removeGuests: { description: `Email addresses that should be removed from guests.`, type: "array", items: { type: "string", description: "Guest email." } },
},
required: ["eventId"]
}
},
};