Last active
December 31, 2025 14:25
-
-
Save 5ouma/554551fd9edb34cb218ad41e8350fcc9 to your computer and use it in GitHub Desktop.
πͺ Compose periodical messages with Deno Cron
This file contains hidden or 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 { api as Misskey } from "npm:misskey-js"; | |
| import { | |
| type Zodiac, | |
| zodiac, | |
| type ZodiacAnimalName, | |
| } from "npm:@kabeep/zodiac-animals"; | |
| const etoEmojis: Record<ZodiacAnimalName, string> = { | |
| rat: "π", | |
| ox: "π", | |
| tiger: "π ", | |
| rabbit: "π", | |
| dragon: "π", | |
| snake: "π", | |
| horse: "π", | |
| sheep: "π", | |
| monkey: "π", | |
| rooster: "π", | |
| dog: "π", | |
| pig: "π", | |
| }; | |
| type Meta = { | |
| misskey: Readonly<{ origin: string; credential: string }>; | |
| }; | |
| const getMeta = (): Meta => { | |
| const meta = { | |
| misskey: { | |
| origin: Deno.env.get("MISSKEY_ORIGIN"), | |
| credential: Deno.env.get("MISSKEY_CREDENTIAL"), | |
| }, | |
| }; | |
| const missedEnv: string[] = []; | |
| for (const [prefix, props] of Object.entries(meta)) { | |
| for (const [suffix, val] of Object.entries(props)) { | |
| if (!val) missedEnv.push(`$${`${prefix}_${suffix}`.toUpperCase()}`); | |
| } | |
| } | |
| if (missedEnv.length > 0) { | |
| throw new Error(`Missed env: ${missedEnv.join(", ")}`); | |
| } | |
| return meta as Meta; | |
| }; | |
| const postMisskey = async ( | |
| opts: { origin: string; credential: string }, | |
| text: string, | |
| ): Promise<string> => { | |
| const cli = new Misskey.APIClient(opts); | |
| const meta = await cli.request("notes/create", { text }); | |
| return `${opts.origin}/notes/${meta.createdNote.id}`; | |
| }; | |
| try { | |
| const { misskey } = getMeta(); | |
| Deno.cron("New Year", "0 15 31 12 *", async (): Promise<void> => { | |
| const year: string = new Date().toLocaleString("en", { | |
| year: "numeric", | |
| timeZone: "JST", | |
| }); | |
| const z = zodiac(new Date(parseInt(year), 2, 1)) as Zodiac; | |
| const text = `${etoEmojis[z.name]} Happy New Year ${year}!`; | |
| console.log(`πͺ Misskey: ${await postMisskey(misskey, text)}`); | |
| }); | |
| console.log("π All periodical messages are ready!"); | |
| } catch (error: unknown) { | |
| console.error(`π¨ ${(error as Error).message}`); | |
| Deno.exit(1); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace the default code with this
Set the environment variables
MISSKEY_ORIGINMISSKEY_CREDENTIAL