-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
92 lines (83 loc) · 2.71 KB
/
index.js
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
const axios = require("axios");
require("dotenv").config();
const { randomSelect, getNotionProperty } = require("./utils");
// envirment val
const DATABASE_ID = process.env.DATABASE_ID;
const TOKEN = process.env.TOKEN;
const BASE_URL = "https://api.notion.com/";
// query settings
// QUERY_NUM: amount of notes query per time
// QUERY_KEYS: notion database header(column name) to query
const QUERY_NUM = 2;
const QUERY_KEYS = ["Name", "Intro", "Created"];
(async () => {
// Query
let queryURL = `${BASE_URL}v1/databases/${DATABASE_ID}/query`;
// set notion API integration auth header
let headers = {
Authorization: `Bearer ${TOKEN}`,
"Notion-Version": "2022-02-22",
"Content-Type": "application/json",
};
// update your sort setting in here
let sorts = [
{
property: "Created",
direction: "descending",
},
];
// create a axios session
let reqInstance = axios.create({
headers,
});
// Database Query POST Request
// see https://developers.notion.com/reference/post-database-query
let results;
results = await reqInstance
.post(queryURL, { sorts })
.then((response) => {
// console.log(response.data);
let queryRes = [];
if (typeof response.data.results !== "undefined") {
for (let i = 0; i < QUERY_NUM; i++) {
// get random selected item from query
let t = randomSelect(response.data.results);
// extract wanted values
QUERY_KEYS.forEach((key) => {
// console.log(`(${i}, ${key}), key type:${typeof key}`);
if (queryRes[i] === undefined) queryRes[i] = {};
queryRes[i][key] = getNotionProperty(t, key);
});
// add url in every item
queryRes[i].url = t.url;
}
}
// console.log(queryRes);
return queryRes;
})
.catch((error) => {
console.log(error);
process.exit(1);
});
// generate email content
// console.log(results);
// set output for other jobs
// see https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs
console.log(`::set-output name=message::` + genMail(results));
// console.log(`::set-output name=message::`+'<h1>test html message</h1></br><p>here is the context</p>');
// console.log('Wanted Review Reminder Content Generated.');
})();
function genMail(ResultArray) {
let message = "";
// This Email message will be sent using https://github.com/dawidd6/action-send-mail
message += "# It's Time to Review Your Notion Notes!%0A";
for (let i = 0; i < ResultArray.length; i++) {
const note = ResultArray[i];
// console.log(note);
message += `%0A${i + 1}. `;
for (k in note) {
message += `${k}: ${note[k]}%0A`;
}
}
return message;
}