-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ts
59 lines (54 loc) · 1.53 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { chunk } from "@std/collections/chunk";
import type { RESTPostAPIWebhookWithTokenJSONBody } from "discord_api_types/rest/v10/webhook.ts";
import type { APIEmbed } from "discord_api_types/payloads/v10/channel.ts";
import type { FeedEntry } from "./shared.ts";
import { DenoFeeder } from "./deno_feeder.ts";
if (import.meta.main) {
const url = Deno.env.get("DISCORD_WEBHOOK_URL");
if (!url) {
throw new Error("DISCORD_WEBHOOK_URL is not set");
}
const kv = await Deno.openKv();
const feeder = new DenoFeeder(kv, ["feeder"]);
await feeder.cron(
"https://fartlabs.org/feed.xml",
"FartLabs Blog",
"0 * * * *", // https://crontab.guru/#0_*_*_*_*
async (entries) => {
for (const entriesChunk of chunk(entries, 10)) {
await executeWebhook(url, renderEmbeds(entriesChunk));
}
},
);
}
async function executeWebhook(
url: string,
body: RESTPostAPIWebhookWithTokenJSONBody,
) {
await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
}
function renderEmbeds(
entries: FeedEntry[],
): RESTPostAPIWebhookWithTokenJSONBody {
return {
embeds: entries.map((entry) => renderEmbed(entry)),
};
}
function renderEmbed(entry: FeedEntry): APIEmbed {
return {
title: entry.title?.value,
description: entry.description?.value,
color: 0xc3ef3c,
url: entry.links?.[0].href,
footer: {
text: Intl.DateTimeFormat("en-US", { dateStyle: "full" })
.format(entry.published),
},
};
}