aboutsummaryrefslogtreecommitdiffstats
path: root/src/index.ts
blob: 3f0c7ef98add27dfa92ffe7ec2a4b17659a9b34e (plain)
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
import { App } from "@tinyhttp/app";
import { logger } from "@tinyhttp/logger";
import config from "./config";
import { broadcast } from "./sheets";
import { bot } from "./tg";

const app = new App({
  onError: (err, _req, res) => {
    console.log(err);
    res.status(500).send("Something bad happened");
  },
});

app
  .use(logger({
    timestamp: {
      format: "YYYY-MM-DDTHH:mm:ssZ[Z]",
    },
  }))
  .use(bot.webhookCallback("/webhook/telegram"))
  .get("/", async (_, res) => {
    res.send({ service: "hommabot2 API" });
  })
  .post("/scheduler/trigger", async (_req, res) => {
    await broadcast();
    res.send({ scheduler: "trigger" });
  });

console.log(`Setting up webhook on ${config.tgWebhookUrl}`);
bot.telegram.setWebhook(config.tgWebhookUrl);

console.log(`Serving on http://localhost:${config.port}`);
app.listen(config.port);

// Enable graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));