-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.ts
31 lines (27 loc) · 826 Bytes
/
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
import { webhookCallback } from "grammy/mod.ts";
import bot from "./bot.ts";
if (Deno.args[0] == "--polling") {
console.info(`Started as @${bot.botInfo.username} on long polling.`);
bot.start();
Deno.addSignalListener("SIGINT", () => bot.stop());
Deno.addSignalListener(
Deno.build.os != "windows" ? "SIGTERM" : "SIGINT",
() => bot.stop(),
);
} else {
console.info(`Started as @${bot.botInfo.username} on webhooks.`);
const handleUpdate = webhookCallback(bot, "std/http");
Deno.serve(async (req) => {
if (req.method === "POST") {
const url = new URL(req.url);
if (url.pathname.slice(1) === bot.token) {
try {
return await handleUpdate(req);
} catch (err) {
console.error(err);
}
}
}
return new Response("Welcome!");
});
}