-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
68 lines (55 loc) · 2.18 KB
/
index.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
import { Telegraf } from 'telegraf';
import dotenv from 'dotenv';
import { fetchAllIssues, odHackRegex, type Issue, labelName, findIssuesAfterTS, findLatestIssueTS } from './issues';
dotenv.config();
const bot = new Telegraf(process.env.BOT_TOKEN as string);
bot.start((ctx) => ctx.reply('Welcome! I can fetch ODHack Github /issues for you.'));
bot.launch();
// State
let issues = new Map<string, Issue[]>();
let latestIssueTS = 0;
const mainLoop = async () => {
// fetch issues
console.log('Fetching all issues...');
issues = await fetchAllIssues();
// if latest ts is not 0, find new issues
if (latestIssueTS !== 0) {
const newIssues = findIssuesAfterTS(issues, latestIssueTS);
if (newIssues.size > 0) {
console.log('New issues found!');
for (const [key, value] of newIssues.entries()) {
console.log(`New issues for ${key}:`);
for (const issue of value) {
console.log(`- ${issue.title}`);
}
}
} else {
console.log('No new issues found.');
}
}
// update latest ts
latestIssueTS = findLatestIssueTS(issues);
setTimeout(mainLoop, 60000);
};
function escapeMarkdownV2(text: string): string {
return text.replace(/[_*[\]()~`>#+\-=|{}.!\\]/g, '\\$&');
}
bot.command('issues', async (ctx) => {
for (const [key, value] of issues.entries()) {
const project = escapeMarkdownV2(key);
const issuesMsg = value.map(issue => {
const labels = issue.labels.map(labelName)
.filter((name): name is string => name?.length != 0 && !odHackRegex.test(name ?? ''))
.map(escapeMarkdownV2);
const formattedLabels = labels.length ? ` \\(${labels.join(', ')}\\)` : '';
const issueTitle = escapeMarkdownV2(issue.title);
const htmlUrl = escapeMarkdownV2(issue.html_url);
const issueLink = `[${issueTitle}](${htmlUrl})${formattedLabels}`;
return `• ${issueLink}`;
}).join('\n')
await ctx.replyWithMarkdownV2(`Issues for ${project}:\n${issuesMsg}`, {
disable_web_page_preview: true,
});
}
})
mainLoop();