Hono と cloudflare workers で Notion のカバーを定期的に変える
Notion のカバーを定期的に変えるための Cloudflare Workers のコードです。少し前は GAS で雑に実装していましたが、cloudflare にワークフローを統一しました。この例ではデータベース内のページのカバーをランダムで取り出してデータベースに設定しています。
import { Client } from "@notionhq/client";
export const changeCover = async (
NOTION_TOKEN: string,
NOTION_DATABASE_ID: string
) => {
const notion = new Client({
auth: NOTION_TOKEN,
});
const res = await notion.databases.query({
database_id: NOTION_DATABASE_ID,
});
const index = Math.floor(Math.random() * res.results.length);
const randomPage = res.results[index];
// @ts-ignore
const coverUrl = randomPage.cover?.external?.url;
if (coverUrl) {
await notion.databases.update({
database_id: NOTION_DATABASE_ID,
cover: {
type: "external",
external: {
url: coverUrl,
},
},
});
} else {
console.error("No cover found for the selected page");
}
};