Skip to content

Instantly share code, notes, and snippets.

@baruchiro
Last active April 4, 2024 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baruchiro/86d716d5a27afcb9f4c7d9fc97aac65f to your computer and use it in GitHub Desktop.
Save baruchiro/86d716d5a27afcb9f4c7d9fc97aac65f to your computer and use it in GitHub Desktop.
Read all Notion Database items and parse them to Whatsapp message
.env
data.json
import { promises as fs } from "fs";
const statusToEmoji = {
"Not started": "๐Ÿ”ด",
"In progress": "๐ŸŸก",
Done: "๐ŸŸข",
};
const explainStatuses = `
ืกื˜ื˜ื•ืก:
๐Ÿ”ด ืœื ื”ืชื—ื™ืœ
๐ŸŸก ื‘ืชื”ืœื™ืš
๐ŸŸข ื”ื•ืฉืœื`
// load .env file
const env = await fs.readFile(".env", "utf8");
env.split("\n").forEach((line) => {
const [key, value] = line.split("=");
process.env[key] = value;
});
const myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer " + process.env.NOTION_API_KEY);
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Notion-Version", "2022-02-22");
const raw = JSON.stringify({
filter: {
property: "Status",
status: {
does_not_equal: "Archive",
},
},
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
const response = await fetch(
"https://api.notion.com/v1/databases/6a74506d3a6c43d680ff583527b029aa/query",
requestOptions
);
const data = await response.json();
await fs.writeFile("data.json", JSON.stringify(data, null, 2));
const taskToWhatsappMessage = (task, indent = "") => {
const title = task.properties.Name.title[0].plain_text;
const status = task.properties.Status.status.name;
const assignies = task.properties.Person.multi_select
.map((assignee) => assignee.name)
.join(", ");
const parentMessage = `${indent}${statusToEmoji[status]} ${title} - ${assignies}`;
const subMessages = task.properties["Sub-item"].relation
.map(({ id }) => data.results.find((t) => t.id === id))
.filter((subTask) => subTask)
.map((subTask) => taskToWhatsappMessage(subTask, indent + " "));
return [parentMessage, ...subMessages].join("\n");
};
const whatsappMessages = data.results
.filter((task) => task.properties["Parent item"].relation.length === 0)
.map((t) => taskToWhatsappMessage(t))
.join("\n");
console.log(whatsappMessages);
console.log(explainStatuses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment