-
Notifications
You must be signed in to change notification settings - Fork 1
/
openai-tomorrow-summary.ts
101 lines (96 loc) · 3.04 KB
/
openai-tomorrow-summary.ts
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
// import cw, { morgen } from "morgen-cw-sdk";
import cw, { sandbox } from "..";
const { fetchMorgen, log, fetch } = sandbox.util;
const { luxon } = sandbox.deps;
const wf = cw.workflow(
{
name: "SDK Test: OpenAI Tomorrow Summary",
},
async function run(trigger) {
const slackToken = "xoxb-...";
const openaiToken = "sk-...";
log("Remember to set tokens here before testing!");
async function getOpenAIResponse(prompt: string) {
let resp = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer " + openaiToken,
Accepts: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: prompt,
},
],
temperature: 0.7,
}),
});
const parsed = JSON.parse(resp);
const error = parsed?.error;
if (error) {
throw new Error("Error with OpenAI: " + JSON.stringify(error));
}
return parsed.choices[0].message.content;
}
if (!trigger.accounts?.calendar?.[0]?.calendarId)
throw new Error("No calendar configured!");
// Fetch events from tomorrow
const calId = trigger.accounts.calendar?.[0].calendarId;
const start = luxon.DateTime.now();
const startTs = start
.startOf("day")
.plus({ days: 1 })
.toISO({ includeOffset: false, suppressMilliseconds: true });
const endTs = start
.startOf("day")
.plus({ days: 2 })
.toISO({ includeOffset: false, suppressMilliseconds: true });
const mresp = await fetchMorgen(
`https://sync.morgen.so/v1/events/list` +
`?calendarIds=${calId}` +
`&start=${startTs}` +
`&end=${endTs}`,
{
method: "GET",
}
);
const evs = mresp.body;
// Get all busy events and format for ChatGPT
const list: any[] = [];
for (let ev of evs.data.events) {
if (ev.freeBusyStatus == "free") continue;
list.push({
title: ev.title,
start: ev.start,
duration: ev.duration,
});
}
// ChatGPT gets confused if we don't order things chronologically
list.sort((a, b) => (a.start > b.start ? -1 : 1));
const summary = await getOpenAIResponse(
"Summarise the following calendar events in an easily digestable chronological bullet list: " +
JSON.stringify(list) +
"\nExample: Events today:\n\t9:00 –\tStandup 15 minutes\n\t12:00 –\tLunch\n\t15:00 –\tCatchup" +
"\nResponse: "
);
log(summary);
// Send summary to Slack user
await fetch("https://api.slack.com/api/chat.postMessage", {
method: "POST",
headers: {
Authorization: `Bearer ${slackToken}`,
Accept: "application/json",
["Content-Type"]: "application/json",
},
body: JSON.stringify({
channel: "@Luke",
text: summary,
}),
});
}
);
wf.upload().then(() => wf.trigger());