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
import { trackSmallStepNotionExport } from "https://esm.town/v/thomasatflexos/trackSmallStepNotionExport";
import { createSmallStepAsNotionPage } from "https://esm.town/v/thomasatflexos/createSmallStepAsNotionPage";
import { createSmallStepNotionDatabase } from "https://esm.town/v/thomasatflexos/createSmallStepNotionDatabase";
import { getSmallStepPage } from "https://esm.town/v/thomasatflexos/getSmallStepPage";
import { getAccessTokenUsingBotId } from "https://esm.town/v/thomasatflexos/getAccessTokenUsingBotId";
export let importSmallStepsIntoNotion = async (
req: express.Request,
res: express.Response,
) => {
let bot_id = req.body.bot_id;
let title = req.body.title;
let stepData = req.body.steps;
if (!bot_id || !title || !stepData) {
res.json({
status: 400,
message: "Missing parameters: bot_id or title or steps",
});
}
// Reverse so that it adds to Notion properly
stepData = stepData.reverse();
const accessToken = await getAccessTokenUsingBotId(
bot_id,
);
if (!accessToken) {
res.json({
status: 400,
message: "This user has not authorizen Notion integration yet",
});
}
// This is very presumptuous, but helps to set up the POC
// We're searching for a page titled "Small steps" in the Notion workspace
// And then use that as the parent page to which we'll add small steps
const smallStepPage = await getSmallStepPage(
accessToken,
);
const newSmallStepDatabase = await createSmallStepNotionDatabase(smallStepPage.id, title, accessToken);
// Loop over the Small Step data and send
const results = [];
for (const datum in stepData) {
let stepName = stepData[datum].stepName;
let content = stepData[datum].text;
let newPage = await createSmallStepAsNotionPage(
newSmallStepDatabase.id,
stepName,
content,
accessToken,
);
results.push(newPage);
}
await trackSmallStepNotionExport(bot_id, title);
res.json({
status: 200,
message: `Created ${results.length} pages in Notion`,
});
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
thomasatflexos-importsmallstepsintonotion.express.val.run
October 23, 2023