Skip to content

Instantly share code, notes, and snippets.

@5ouma
Last active May 9, 2025 12:42
Show Gist options
  • Save 5ouma/554551fd9edb34cb218ad41e8350fcc9 to your computer and use it in GitHub Desktop.
Save 5ouma/554551fd9edb34cb218ad41e8350fcc9 to your computer and use it in GitHub Desktop.
πŸŽͺ Compose periodical messages with Deno Cron
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);
}
@5ouma
Copy link
Author

5ouma commented Dec 26, 2024

  1. Replace the default code with this

    import "https://gist.githubusercontent.com/5ouma/554551fd9edb34cb218ad41e8350fcc9/raw/periodic-message.ts";
  2. Set the environment variables

    • MISSKEY_ORIGIN
    • MISSKEY_CREDENTIAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment