recruter/src/index.ts

57 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { buildApp } from "./app.js";
import { handleUpdate } from "./bot.js";
import { HttpTelegramApi, pollUpdates } from "./telegram.js";
import { openDb } from "./db.js";
import { startDailyScheduler } from "./scheduler.js";
import type { SourceAdapter } from "./sources.js";
import { runDailySearch } from "./sourcing.js";
import { listVacancies } from "./vacancies.js";
import { runDigestJob } from "./digest.js";
import { HhResumeSource } from "./resume-sources.js";
import type { TelegramApi } from "./telegram.js";
const db = openDb(process.env.DB_PATH ?? "data.db");
if (!process.env.API_TOKEN) console.warn("API_TOKEN не задан — закрытая часть (/api) отклоняет все запросы");
let botUsername: string | undefined;
const botToken = process.env.TELEGRAM_BOT_TOKEN;
const tg: TelegramApi | undefined = botToken ? new HttpTelegramApi(botToken) : undefined;
// RCT-4: поиск опубликованных резюме (hh.kz — официальный кабинет работодателя) и подборка в Telegram.
const hhToken = process.env.HH_ACCESS_TOKEN;
if (!hhToken) console.warn("HH_ACCESS_TOKEN не задан — поиск резюме на hh.kz не работает");
const resumeSources = [new HhResumeSource(hhToken)];
const app = buildApp({
db, apiToken: process.env.API_TOKEN ?? "", botUsername: () => botUsername,
digest: { sources: resumeSources, configured: hhToken ? ["hh"] : [], tg: () => (botUsername ? tg : undefined) },
});
// Бот откликов RCT-3: без TELEGRAM_BOT_TOKEN не запускается, остальное работает.
if (tg) {
void (async () => {
for (;;) {
try { botUsername = (await tg.getMe()).username; break; } catch (e) {
console.warn(`telegram: не удалось подключить бота: ${(e as Error).message}, повтор через 60 с`);
await new Promise((r) => setTimeout(r, 60_000));
}
}
await pollUpdates(tg, { onUpdate: (u) => handleUpdate(db, tg, u) });
})();
} else {
console.warn("TELEGRAM_BOT_TOKEN не задан — бот откликов не запущен");
}
// Реальные адаптеры площадок подключаются здесь, когда PM выдаст доступы (см. RCT-1 design.md).
const adapters: SourceAdapter[] = [];
startDailyScheduler(
async () => {
const r = await runDigestJob(db, resumeSources, botUsername ? tg : undefined);
console.warn(`подборка резюме: ${r.state}, резюме: ${r.sent}`);
if (adapters.length === 0) return console.warn("площадки не настроены — поиск пропущен");
for (const v of listVacancies(db).filter((x) => x.active)) await runDailySearch(db, v.id, adapters);
},
{ hourUtc: Number(process.env.SEARCH_HOUR_UTC ?? 6) },
);
await app.listen({ port: Number(process.env.PORT ?? 3000), host: "0.0.0.0" });