sd-telegram-bot/index.ts

48 lines
1.4 KiB
TypeScript

import { Bot as TelegramBot, BotConfig } from "grammy";
import { Context, createContextConstructor } from "#root/context.js";
import { logger } from "#root/logger.js";
import { hydrateReply, parseMode } from "@grammyjs/parse-mode";
import { config } from "#root/config.js";
import { autoChatAction } from "@grammyjs/auto-chat-action";
import { hydrate } from "@grammyjs/hydrate";
import { updateLogger } from "#root/middlewares/index.js";
import { errorHandler } from "#root/helpers/error.js";
import { welcomeFeature } from "#root/handlers/welcome.js";
import { imageFeature } from "#root/handlers/imagine/imagine.js";
import { ignoreOld } from "#root/middlewares/ignore-old.js";
type Options = {
config?: Omit<BotConfig<Context>, "ContextConstructor">;
};
export function createBot(token: string, options: Options = {}) {
const bot = new TelegramBot(token, {
...options.config,
ContextConstructor: createContextConstructor({ logger }),
});
bot.api.config.use(parseMode("html"));
bot.use(ignoreOld());
if (config.isDev) {
bot.use(updateLogger());
}
bot.use(autoChatAction(bot.api));
bot.use(hydrateReply);
bot.use(hydrate());
// Handlers
bot.use(welcomeFeature);
bot.use(imageFeature);
// Must be the last handler
if (config.isDev) {
bot.catch(errorHandler);
}
return bot;
}
export type Bot = ReturnType<typeof createBot>;