-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.ts
More file actions
52 lines (47 loc) · 1.63 KB
/
main.ts
File metadata and controls
52 lines (47 loc) · 1.63 KB
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
import { Application } from "@oak/oak";
import { Bot, Context } from "grammy/mod.ts";
import { ParseModeFlavor } from "grammy_parse_mode/mod.ts";
import { messages } from "./messages.ts";
import { autoRetry } from "grammy_auto_retry/mod.ts";
const app = new Application();
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.response.status = 500;
console.error(err);
}
});
app.use(async (ctx) => {
const event = ctx.request.headers.get("x-github-event");
if (!event) {
return ctx.response.redirect("https://github.com/rojvv/ghb");
}
const payload = await ctx.request.body.json();
const token = ctx.request.url.searchParams.get("token");
const chatId = ctx.request.url.searchParams.get("chatId");
const messageThreadId = ctx.request.url.searchParams.get("messageThreadId") ??
undefined;
if (token && chatId) {
const bot = new Bot<ParseModeFlavor<Context>>(token);
bot.api.config.use(autoRetry());
const formattedString = messages[event]?.(payload);
const text = formattedString?.toString();
const entities = formattedString?.entities;
if (text != undefined) {
await bot.api.sendMessage(
chatId,
text.toString().substring(0, 4093) + (text.length > 4093 ? "..." : ""),
{
entities: entities?.filter((v) => !((v.offset + v.length) > 4093)),
link_preview_options: { is_disabled: true },
...(messageThreadId
? { reply_parameters: { message_id: Number(messageThreadId) } }
: {}),
},
);
}
}
ctx.response.status = 200;
});
app.listen({ port: +(Deno.env.get("PORT") + "") || 8000 });