diff options
| author | Jan Tuomi <jan.tuomi@valuemotive.com> | 2021-04-18 18:07:54 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan.tuomi@valuemotive.com> | 2021-04-18 18:07:54 +0300 |
| commit | baacc7676c688cd2728033e9051208d655255ee5 (patch) | |
| tree | 5a3819b1c5f6ad616d82f84b3a62cca20d54c0f7 /src/index.ts | |
| parent | 12f3de7a9ec911cf5945eee31c7680aa75c4f8af (diff) | |
Work on tg bot
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/index.ts b/src/index.ts index 3ff5de7..71e239f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,16 @@ import { App } from "@tinyhttp/app"; import { logger } from "@tinyhttp/logger"; -import { auth } from "./middleware"; +import { Telegraf } from "telegraf"; +import config from "./config"; +import { tgMiddleware } from "./middleware"; const app = new App(); -const port = process.env.PORT ? Number(process.env.PORT) : 3000; +console.log(`Setting up Telegram bot with token ${config.tgBotToken}`); +const bot = new Telegraf(config.tgBotToken); + +bot.use(tgMiddleware.auth); +bot.command("/start", (ctx) => ctx.reply("started")); +bot.command("/stop", (ctx) => ctx.reply("stopped")); app .use(logger({ @@ -11,19 +18,20 @@ app format: "YYYY-MM-DDTHH:mm:ssZ[Z]", }, })) - .use(auth) + .use(bot.webhookCallback("/webhook/telegram")) .get("/", (_, res) => { - res.send({ foo: "bar" }); - }) - .post("/webhook/start", (_req, res) => { - res.send({ webhook: "start" }); - }) - .post("/webhook/stop", (_req, res) => { - res.send({ webhook: "stop" }); + res.send({ service: "hommabot2 API" }); }) .post("/scheduler/trigger", (_req, res) => { res.send({ scheduler: "trigger" }); }); -console.log(`Serving on http://localhost:${port}`); -app.listen(port); +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")); |
