Files
hereconnect/services/tg-bot/old/setup/setupWebHook.ts
T

24 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-12-10 13:21:38 +02:00
import { setupWebhookDesignDocument } from "./setupWebhookDesignDocument";
import { TG_BOT_DB_NAME, tgBotDesignDocumentsDb } from "dbs";
import { bot } from "old_bot";
const PUBLIC_API_URL = process.env.PUBLIC_API_URL;
if (!PUBLIC_API_URL) {
throw new Error("Необходимо указать PUBLIC_API_URL в переменных окружения.");
}
export const setupWebHook = async () => {
const designDocument = await setupWebhookDesignDocument();
const update: keyof typeof designDocument.updates = "saveUpdate";
const webhookUrl = `${process.env.PUBLIC_API_URL}/${TG_BOT_DB_NAME}/${designDocument._id}/_update/${update}`;
if (designDocument.constants.telegram_webhook_url !== webhookUrl) {
await bot.api.setWebhook(webhookUrl, {
secret_token: designDocument.constants.telegram_secret_token,
});
designDocument.constants.telegram_webhook_url = webhookUrl;
const { rev } = await tgBotDesignDocumentsDb.put(designDocument);
designDocument._rev = rev;
}
return designDocument;
};