Files
hereconnect/services/tg-bot/src/bot/index.ts
T
ti d71b405277
Deploy app frontend / app frontend (push) Successful in 2m43s
Deploy db-migrations / db-migrations (push) Successful in 1m8s
Deploy service message-delivery-method-web-push / service message-delivery-method-web-push (push) Successful in 1m30s
Deploy service tg-bot / service tg-bot (push) Failing after 1m54s
send messages to telegram from RQCP (ReadQrcodeChatPage) with link to MQCP (ManageQrcodeChatPage)
2024-12-14 23:58:29 +02:00

60 lines
2.0 KiB
TypeScript

import { Bot, session } from "grammy";
import { conversations, createConversation } from "@grammyjs/conversations";
import { MyContext } from "../types/myContext";
import { env } from "../config/env";
import { CouchDBStorageAdapter } from "./storageAdapters/CouchDBStorageAdapter";
import { loadUserMiddleware } from "./middleware/loadUser";
import { tgBotSessionsDb } from "../dbs";
import { startCommand } from "./commands/start";
import { helpCommand } from "./commands/help";
import { newCommand } from "./commands/new";
import { manageCommand } from "./commands/manage";
import { callbackQueryHandler } from "./callbackHandlers";
import { newQRCodeConversation } from "./conversations/newQRCodeConversation";
import { hydrateReply } from "@grammyjs/parse-mode";
export { setupCommands } from "./commands/setupCommands";
export const createBot = ({
abortController,
}: {
abortController: AbortController;
}) => {
const storage = new CouchDBStorageAdapter(tgBotSessionsDb, {
softDelete: true,
type: "tg_bot_session",
});
// Инициализируем бота с нужным типом контекста
const bot = new Bot<MyContext>(env.TELEGRAM_BOT_TOKEN, {
client: {
baseFetchConfig: {
signal: abortController.signal,
},
},
});
bot.use(hydrateReply);
// Подключаем сессии. По необходимости определите initial state для сессии
bot.use(session({ storage, initial: () => ({}) }));
// Подключаем conversations для многошаговых сценариев
bot.use(conversations());
bot.use(createConversation(newQRCodeConversation));
// Middleware для загрузки или создания пользователя
bot.use(loadUserMiddleware);
// Регистрация команд
bot.command("start", startCommand);
bot.command("help", helpCommand);
bot.command("new", newCommand);
bot.command("manage", manageCommand);
// Обработка callback_query
bot.on("callback_query:data", callbackQueryHandler);
return bot;
};