Last active
May 9, 2025 12:42
-
-
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"; | |
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 text = `π 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); | |
} |
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_ORIGIN
MISSKEY_CREDENTIAL