Last active
April 4, 2024 05:59
-
-
Save baruchiro/86d716d5a27afcb9f4c7d9fc97aac65f to your computer and use it in GitHub Desktop.
Read all Notion Database items and parse them to Whatsapp message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.env | |
data.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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