Telegraf Simple Webhook

mail@pastecode.io avatar
unknown
typescript
a year ago
842 B
12
Indexable
Never
import express from "express";
import { Telegraf } from "telegraf";
import "dotenv/config";

const env = process.env;
const token = env.BOT_TOKEN;

if (!token) {
  throw Error("Provide BOT_TOKEN");
}
const app = new Telegraf(token);
const server = express();
const port = env.PORT || 8080;

app.start(async (ctx) => {
  await ctx.reply("Hello");
});

if (env.DEVELOPMENT) {
  app.telegram.getMe().then((me) => {
    console.log(`Successfully logged in as ${me.username}`);
  });
  app.launch();
} else {
  const domain = env.WEBHOOK_DOMAIN;

  if (!domain) {
    throw Error("Please provide WEBHOOK_DOMAIN");
  }
  server.use(await app.createWebhook({ domain }));
  server.listen(port, () => console.log(`Server listening on ${port}`));
}

process.once("SIGINT", () => app.stop("SIGINT"));
process.once("SIGTERM", () => app.stop("SIGTERM"));