Notionのデイリーページを自動作成
概要
Notionのあるデータベース内に、今日の日付をタイトルに含むページが存在しなければ作成する。最後に、そのページのURLを返す。
NotionのAPIとGoogle Apps Scriptは久しぶりに使うので思い出すところから。更新による差分めっちゃありそう。
GASじゃなくてGitHub Actionsを使ってやってみることにした。
- claspの再導入
- プロジェクトの作成
- Hello, worldを返すやつ作る
-
NotionのAPIでデータベースと連携する
- データベースの情報だけ返すやつ作る
- データベースから今日の日付をタイトルに含むページが存在するか返す
-
データベースに今日のデイリーページが存在しなければそのページを作成
してURLを返す - デイリーページの作成中に日付も設定する
- デイリーページの作成中にアイコンも設定する
- 毎日0:01に叩くようにする
以下はできないのでやらない
-
任意の端末から叩いてURLを取得できることを確認PCのブラウザiPadのショートカット
clasp再導入とプロジェクトの作成
以下に従ってGoogle Apps Script APIをオンにした。
リポジトリを作成してブランチ切った。
パッケージの準備
npm init --yes
npm install typescript --save-dev
mkdir src
{
"compilerOptions": {
"lib": ["esnext"],
"experimentalDecorators": true,
"target": "ES2019",
"outDir": "./dist"
},
"include": ["src/**/*"]
}
参考:
npm install clasp @types/google-apps-script
npx clasp login
~/.clasprc.jsonが作成される。
npm install --save-dev\
eslint \
prettier \
eslint-config-prettier \
eslint-import-resolver-typescript \
eslint-plugin-{import,prettier,unused-imports} \
@trivago/prettier-plugin-sort-imports \
@typescript-eslint/parser \
@typescript-eslint/eslint-plugin
いつもの。
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: "latest",
sourceType: "module",
},
plugins: ["@typescript-eslint", "unused-imports"],
rules: {
"@typescript-eslint/no-unused-vars": "off",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"warn",
{
vars: "all",
varsIgnorePattern: "^_",
args: "after-used",
argsIgnorePattern: "^_",
},
],
},
overrides: [],
};
以下もあとで追加するかも?
npm install --save-dev eslint-plugin-googleappsscript
実際にスクリプトを作成
npx clasp create --type webapp --title notion-daily-generator
src/.clasp.jsonのrootDirを./distに変更する。
また、src/.clasp.jsonはGitHubで管理したくない&GitHub Actionsでsecretsとして使えばいいので.gitignoreに加えた。
生成されたappsscript.jsonのtimeZoneをAsia/Tokyoに変更した。
package.jsonのscriptsに以下を加えた(まだ使ってない=未検証)。
"lint": "eslint src && prettier --check src",
"deploy": "cp appsscript.json dist/appsscript.json && clasp push",
参考:Google Apps Script開発環境をVisual Studio Codeで構築する | たぬきうどんTechBlog
claspの--rootDirは文字通りプロジェクトとしてのルートディレクトリを指定するので、デフォルトのまま(カレントディレクトリ)でOK。
--rootDir ./srcとかしたらファイルの移動とか面倒。
npm install @notionhq/client

GitHub Actioinsにおけるcronの参考
👇公式ドキュメント
パブリックリポジトリでは、60日間にリポジトリにアクティビティがなかった場合、スケジュールされたワークフローは自動的に無効化されます
今回はプライベートリポジトリなので、60日の制約は無さそう?
記事にした