-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
248 lines (232 loc) · 8.67 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Script for fetching Deno (https://deno.com) related RSS feeds and
* posting them in a Telegram channel. Join https://t.me/deno_news for demo.
* IV Rules are in the rules/ directory.
*
* Licensed under MIT | Copyright (c) 2022-2024 Dunkan
* GitHub Repository: https://github.com/dcdunkan/deno-news-bot
*/
import {
type FeedEntry,
parseFeed,
} from "https://deno.land/x/[email protected]/mod.ts";
import { Bot } from "https://deno.land/x/[email protected]/mod.ts";
// To avoid reposting from the beginning in case of removal of the
// last stored entry, we store last X feed entry IDs and iterate
// through the all entries until we hit the first "already sent" entry.
const LAST_X_TO_STORE = 12;
const ENV = env("BOT_TOKEN", "CHANNEL");
const kv = await Deno.openKv();
const bot = new Bot(ENV.BOT_TOKEN);
const CHANNEL = Number(ENV.CHANNEL);
if (isNaN(CHANNEL)) throw new Error("CHANNEL should be a chat (channel) ID");
// See the iv-rules/ directory for the Instant-View sources.
const RHASHES: Record<string, string> = {
"deno.news": "b5ba1c523db473", // https://deno.news/archive/...
"deno.com": "28aee3eda1037a", // https://deno.com/blog/...
"devblogs.microsoft.com": "24952bb2da22c6", // https://devblogs.microsoft.com/...
"v8.dev": "8320f1ac30d205", // https://v8.dev/{blog/,docs/}...
"bun.sh": "631ee27991e51a", // https://bun.sh/blog/...
};
const SOURCES = {
blog: "https://deno.com/feed",
news: "https://buttondown.email/denonews/rss",
typescript: "https://devblogs.microsoft.com/typescript/feed/",
v8_blog: "https://v8.dev/blog.atom",
deploy_changelog: "https://deno.com/deploy/feed",
release: "denoland/deno",
std_release: "denoland/deno_std",
bun_blog: "https://bun.sh/rss.xml",
nodejs_blog: "https://nodejs.org/en/feed/blog.xml",
} as const;
const ROUTES = Object.keys(SOURCES) as Feed[];
type Feed = keyof typeof SOURCES;
type NewsHandler = () => Promise<{ message: string; previewUrl?: string }[]>;
type FeedEntryFilter = (entry: FeedEntry) => boolean;
type FeedEntryProcessor = (entry: FeedEntry) => { title: string; url: string };
type SendMessageOptions = Parameters<typeof bot.api.sendMessage>[2];
interface Release {
name: string;
id: number;
html_url: string;
}
interface FeedHandlerOptions {
entryFilter: FeedEntryFilter;
entryProcessor: FeedEntryProcessor;
}
const DEFAULT_FEED_ENTRY_FILTER: FeedEntryFilter = () => true;
const DEFAULT_FEED_ENTRY_PROCESSOR: FeedEntryProcessor = (entry) => ({
title: entry.title?.value ?? "",
url: entry.links[0].href ?? entry.id,
});
const LINK_PREVIEW_DISABLED: Feed[] = ["release", "std_release"];
const newsHandlers: Record<Feed, NewsHandler> = {
blog: getFeedHandler("blog"),
news: getFeedHandler("news", {
entryProcessor: (entry) => ({
title: entry.title?.value ?? "",
url: (entry.links?.[0].href ?? entry.id).replace(
"buttondown.email/denonews",
"deno.news",
),
}),
}),
typescript: getFeedHandler("typescript"),
v8_blog: getFeedHandler("v8_blog"),
deploy_changelog: getFeedHandler("deploy_changelog"),
release: getReleaseHandler("release", "Deno"),
std_release: getReleaseHandler("std_release", "std"),
bun_blog: getFeedHandler("bun_blog"),
nodejs_blog: getFeedHandler("nodejs_blog", {
entryFilter: (entry) => {
// ID is supposed to be `/blog/{type}/actual-blog-id`
const parts = entry.id.split("/");
// Definitely shouldn't be included.
if (parts[1] !== "blog" || typeof parts[2] !== "string") {
return false;
}
// Everything except events!
switch (parts[2]) {
case "announcements":
case "release":
case "vulnerability":
return true;
case "events":
return false;
default:
return false;
}
},
}),
};
async function getLatestFeedEntries(page: Feed): Promise<FeedEntry[]> {
const lastChecked = await kv.get<string[]>(["denonews", page]);
const response = await fetch(SOURCES[page]);
const textFeed = await response.text();
const feed = await parseFeed(textFeed);
if (feed.entries.length === 0) return [];
if (lastChecked.value == null) {
// freshly added feed -> store the latest entry.
const entries = feed.entries
.filter((entry) => entry.id != null)
.slice(0, LAST_X_TO_STORE);
const ids = entries.map((entry) => entry.id);
await kv.set(["denonews", page], ids);
return [entries[0]]; // send latest entry -> everything works fine.
}
const entries: FeedEntry[] = [];
for (const entry of feed.entries) {
if (entry.id == null) continue; // this shouldn't be happening
if (lastChecked.value.includes(entry.id)) break;
entries.unshift(entry); // FIFO
}
if (entries.length > 0) {
const lastFew = feed.entries
.filter((entry) => entry.id != null)
.slice(0, LAST_X_TO_STORE)
.map((entry) => entry.id);
await kv.set(["denonews", page], lastFew);
}
return entries;
}
function getFeedHandler(
feed: Feed,
options?: Partial<FeedHandlerOptions>,
): NewsHandler {
const opts = {
entryFilter: DEFAULT_FEED_ENTRY_FILTER,
entryProcessor: DEFAULT_FEED_ENTRY_PROCESSOR,
...options,
};
return async () => {
const entries = await getLatestFeedEntries(feed);
return entries.filter(opts.entryFilter).map((entry) => {
const { title, url } = opts.entryProcessor(entry);
return {
message: `<b>${esc(title)}</b>\n\n${esc(url)}`,
previewUrl: iv(url),
};
});
};
}
async function getLatestRelease(repo: Feed) {
const lastSent = await kv.get<number>(["denonews", repo]);
const url = `https://api.github.com/repos/${SOURCES[repo]}/releases/latest`;
const response = await fetch(url);
if (!response.ok) return;
const release = await response.json() as Release;
if (lastSent.value != null && release.id === lastSent.value) return;
await kv.set(["denonews", repo], release.id);
return release;
}
function getReleaseHandler(repo: Feed, title: string): NewsHandler {
return async () => {
const release = await getLatestRelease(repo);
if (release == null) return [];
return [{
message: `<b>${title} ${esc(release.name)}</b>\n\n${
esc(release.html_url)
}`,
}];
};
}
Deno.cron("Fetch feeds and post news", { minute: { every: 1 } }, async () => {
const feed = selectNewsHandler();
const messages = await newsHandlers[feed]();
for (const { message, previewUrl } of messages) {
const sent = LINK_PREVIEW_DISABLED.includes(feed)
? await post(message, {
link_preview_options: { is_disabled: true },
})
: await post(
message,
previewUrl
? {
link_preview_options: {
url: previewUrl,
prefer_small_media: true,
},
}
: {},
);
if (feed !== "typescript") continue;
const lastPinned = await kv.get<number>(["denonews", "release_pin"]);
if (lastPinned.value != null) {
try { // Maybe the message was unpinned by administrators.
await bot.api.unpinChatMessage(CHANNEL, lastPinned.value);
} catch (error) {
console.error(error);
}
}
await bot.api.pinChatMessage(CHANNEL, sent.message_id, {
disable_notification: true,
});
await kv.set(["denonews", "release_pin"], sent.message_id);
}
console.log({ feed, sent: messages.length });
});
function selectNewsHandler(): Feed {
return ROUTES[new Date().getMinutes() % ROUTES.length];
}
function post(text: string, options?: SendMessageOptions) {
return bot.api.sendMessage(CHANNEL, text, {
parse_mode: "HTML",
...options,
});
}
function iv(url: string) {
const rhash = RHASHES[new URL(url).hostname];
if (rhash != null) return `https://t.me/iv?rhash=${rhash}&url=${url}`;
}
function esc(text: string) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}
function env<T extends string>(...keys: T[]): { [key in T]: string } {
return keys.reduce(
(v, k) => ({ ...v, [k]: Deno.env.get(k) as string }),
{} as { [key in T]: string },
);
}