-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.ts
55 lines (49 loc) · 1.41 KB
/
serve.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
import { bot, instance } from "./config/index.ts";
import { serve, webhookCallback } from "./deps.ts";
import "https://deno.land/std@0.201.0/dotenv/load.ts";
const handle = webhookCallback(bot, "std/http");
const webhook = async () => {
await console.log("[INFO]", `bot is starting on ${Deno.env.get("HOST")}`);
await serve(async (req) => {
const url = new URL(req.url);
if (req.method == "POST") {
switch (url.pathname) {
case "/bot":
try {
return await handle(req);
} catch (err) {
console.error(err);
return new Response("Nope, not working...");
}
default:
return new Response("What you're trying to post?");
}
}
switch (url.pathname) {
case "/webhook":
try {
await bot.api.setWebhook(`https://${url.hostname}/bot`);
return new Response("Done. Set");
} catch (_) {
return new Response("Couldn't succeed with installing webhook");
}
default:
return Response.redirect(`https://t.me/${instance.username}`, 302);
}
});
};
const polling = async () => {
await bot.start();
};
export const launch = async () => {
switch (Deno.env.get("HOST")) {
case "WEBHOOK":
await webhook();
break;
case "POLLING":
await polling();
break;
default:
throw new Error("Deploy method not validated!");
}
};