mirror of
https://github.com/hempyhemp/hh-auto-reply.git
synced 2026-06-09 02:15:34 +00:00
🛠️ refactor(file/topic): Рефакторинг кода для показа меню сообщений.
This commit is contained in:
@@ -3,6 +3,10 @@ import prisma from '@prisma'
|
||||
import cron, { type ScheduledTask } from 'node-cron'
|
||||
import { applyToJobs, checkIsAuth, listResumes, login, type ResumeListItem, saveResume } from './scraper.js'
|
||||
|
||||
function escapeHtml(text: string): string {
|
||||
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
||||
}
|
||||
|
||||
interface UserState {
|
||||
autoCron: ScheduledTask | null
|
||||
awaitingEmail: boolean
|
||||
@@ -10,6 +14,8 @@ interface UserState {
|
||||
awaitingMax: boolean
|
||||
tempEmail: string
|
||||
pendingResumes: ResumeListItem[]
|
||||
menuMessageId: number | null
|
||||
loginPromptMessageId: number | null
|
||||
}
|
||||
|
||||
function makeUserState(): UserState {
|
||||
@@ -20,6 +26,8 @@ function makeUserState(): UserState {
|
||||
awaitingMax: false,
|
||||
tempEmail: '',
|
||||
pendingResumes: [],
|
||||
menuMessageId: null,
|
||||
loginPromptMessageId: null,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,42 +39,114 @@ function getState(chatId: number): UserState {
|
||||
return states.get(chatId)!
|
||||
}
|
||||
|
||||
async function sendResumeSelector(chatId: number, resumes: ResumeListItem[]) {
|
||||
getState(chatId).pendingResumes = resumes
|
||||
await bot.sendMessage(chatId, '📄 Выбери резюме:', {
|
||||
const MAIN_MARKUP = {
|
||||
inline_keyboard: [
|
||||
[{ text: '🚀 Откликнуться сейчас', callback_data: 'hh_apply' }],
|
||||
[
|
||||
{ text: '🔍 Изменить запрос', callback_data: 'hh_query' },
|
||||
{ text: '🔢 Макс откликов', callback_data: 'hh_max' },
|
||||
],
|
||||
[
|
||||
{ text: '⏰ Авто вкл', callback_data: 'hh_auto_start' },
|
||||
{ text: '⛔ Авто выкл', callback_data: 'hh_auto_stop' },
|
||||
],
|
||||
[
|
||||
{ text: '🔑 Логин', callback_data: 'hh_login' },
|
||||
{ text: '⚙️ Статус', callback_data: 'hh_status' },
|
||||
],
|
||||
[
|
||||
{ text: '📄 Выбрать резюме', callback_data: 'hh_resume_list' },
|
||||
{ text: '📋 Моё резюме', callback_data: 'hh_my_resume' },
|
||||
],
|
||||
],
|
||||
}
|
||||
|
||||
const BACK_MARKUP = {
|
||||
inline_keyboard: [[{ text: '◀️ Назад', callback_data: 'hh_back' }]],
|
||||
}
|
||||
|
||||
// Редактирует существующее сообщение-меню или отправляет новое
|
||||
async function showMenu(chatId: number, messageId?: number | null): Promise<void> {
|
||||
const state = getState(chatId)
|
||||
const targetId = messageId ?? state.menuMessageId
|
||||
|
||||
if (targetId) {
|
||||
try {
|
||||
await bot.editMessageText('🤖 HH Auto-Apply', {
|
||||
chat_id: chatId,
|
||||
message_id: targetId,
|
||||
reply_markup: MAIN_MARKUP,
|
||||
})
|
||||
state.menuMessageId = targetId
|
||||
return
|
||||
}
|
||||
catch {
|
||||
// Сообщение устарело или недоступно — отправим новое
|
||||
}
|
||||
}
|
||||
|
||||
const msg = await bot.sendMessage(chatId, '🤖 HH Auto-Apply', {
|
||||
reply_markup: MAIN_MARKUP,
|
||||
})
|
||||
state.menuMessageId = msg.message_id
|
||||
}
|
||||
|
||||
// Редактирует сообщение с меню: показывает текст + кнопку "Назад"
|
||||
async function showResult(chatId: number, messageId: number, text: string): Promise<void> {
|
||||
await bot.editMessageText(text, {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
reply_markup: BACK_MARKUP,
|
||||
})
|
||||
}
|
||||
|
||||
async function sendResumeSelector(chatId: number, resumes: ResumeListItem[], messageId: number): Promise<void> {
|
||||
const state = getState(chatId)
|
||||
state.pendingResumes = resumes
|
||||
await bot.editMessageText('📄 Выбери резюме:', {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
reply_markup: {
|
||||
inline_keyboard: resumes.map((r, i) => [
|
||||
{ text: r.title, callback_data: `hh_resume_pick_${i}` },
|
||||
]),
|
||||
inline_keyboard: [
|
||||
...resumes.map((r, i) => [{ text: r.title, callback_data: `hh_resume_pick_${i}` }]),
|
||||
[{ text: '◀️ Назад', callback_data: 'hh_back' }],
|
||||
],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function triggerHHStart(chatId: number): void {
|
||||
bot.sendMessage(chatId, '🤖 HH Auto-Apply', {
|
||||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[
|
||||
{ text: '🚀 Откликнуться сейчас', callback_data: 'hh_apply' },
|
||||
],
|
||||
[
|
||||
{ text: '🔍 Изменить запрос', callback_data: 'hh_query' },
|
||||
{ text: '🔢 Макс откликов', callback_data: 'hh_max' },
|
||||
],
|
||||
[
|
||||
{ text: '⏰ Авто вкл', callback_data: 'hh_auto_start' },
|
||||
{ text: '⛔ Авто выкл', callback_data: 'hh_auto_stop' },
|
||||
],
|
||||
[
|
||||
{ text: '🔑 Логин', callback_data: 'hh_login' },
|
||||
{ text: '⚙️ Статус', callback_data: 'hh_status' },
|
||||
],
|
||||
[
|
||||
{ text: '📄 Выбрать резюме', callback_data: 'hh_resume_list' },
|
||||
],
|
||||
],
|
||||
},
|
||||
})
|
||||
async function doLogin(chatId: number, email: string): Promise<void> {
|
||||
await bot.sendMessage(chatId, '🔄 Логинюсь...')
|
||||
try {
|
||||
await login(email, chatId)
|
||||
await prisma.user.update({ where: { telegramId: chatId }, data: { hhEmail: email } })
|
||||
|
||||
const resumes = await listResumes(chatId)
|
||||
const state = getState(chatId)
|
||||
|
||||
if (resumes.length === 0) {
|
||||
await bot.sendMessage(chatId, '⚠️ Резюме не найдены. Создайте резюме на hh.ru')
|
||||
}
|
||||
else if (resumes.length === 1) {
|
||||
await saveResume(chatId, resumes[0].href)
|
||||
await bot.sendMessage(chatId, `✅ Резюме сохранено: ${resumes[0].title}`)
|
||||
}
|
||||
else if (state.menuMessageId) {
|
||||
await sendResumeSelector(chatId, resumes, state.menuMessageId)
|
||||
return
|
||||
}
|
||||
|
||||
await bot.sendMessage(chatId, '✅ Авторизован! Куки сохранены.')
|
||||
await showMenu(chatId)
|
||||
}
|
||||
catch (e) {
|
||||
await bot.sendMessage(chatId, `❌ Ошибка: ${(e as Error).message}`)
|
||||
await showMenu(chatId)
|
||||
}
|
||||
}
|
||||
|
||||
export async function triggerHHStart(chatId: number): Promise<void> {
|
||||
await showMenu(chatId)
|
||||
}
|
||||
|
||||
export function registerHHCommands() {
|
||||
@@ -77,91 +157,194 @@ export function registerHHCommands() {
|
||||
bot.on('callback_query', async (query) => {
|
||||
if (!query.message)
|
||||
return
|
||||
|
||||
const chatId = query.message.chat.id
|
||||
const messageId = query.message.message_id
|
||||
const state = getState(chatId)
|
||||
|
||||
bot.answerCallbackQuery(query.id)
|
||||
await bot.answerCallbackQuery(query.id)
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { telegramId: chatId },
|
||||
include: { Settings: true },
|
||||
})
|
||||
|
||||
const settings = user!.Settings!
|
||||
|
||||
switch (query.data) {
|
||||
case 'hh_apply':
|
||||
await bot.sendMessage(chatId, `🚀 Ищу: "${settings.searchQuery}"...`)
|
||||
applyToJobs({ query: settings.searchQuery, maxApplies: settings.maxApplies }, {
|
||||
chatId,
|
||||
}).then((result) => {
|
||||
if (result.error)
|
||||
return bot.sendMessage(chatId, `❌ ${result.error}`)
|
||||
const lines = result.applied.map((v, i) => `${i + 1}. ${v}`).join('\n')
|
||||
bot.sendMessage(chatId, `✅ Откликнулся: ${result.applied.length}\n${lines}\n\n`
|
||||
+ `⏭ Пропущено: ${result.skipped.length}\n${
|
||||
result.errors.length ? `❌ Ошибок: ${result.errors.length}` : ''}`)
|
||||
})
|
||||
case 'hh_back':
|
||||
await showMenu(chatId, messageId)
|
||||
break
|
||||
|
||||
case 'hh_status':
|
||||
await bot.sendMessage(chatId, `⚙️ Настройки:\n
|
||||
Запрос: ${settings.searchQuery}\n
|
||||
Макс откликов: ${settings.maxApplies}\n
|
||||
Авто: ${state.autoCron ? '✅ включено' : '❌ выключено'}\n
|
||||
Авторизован: ${await checkIsAuth(chatId)}`)
|
||||
case 'hh_apply': {
|
||||
await bot.editMessageText(`🔄 Ищу вакансии по запросу "${settings.searchQuery}"...`, {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
reply_markup: { inline_keyboard: [] },
|
||||
})
|
||||
state.menuMessageId = null
|
||||
|
||||
applyToJobs({ query: settings.searchQuery, maxApplies: settings.maxApplies }, { chatId })
|
||||
.then(async (result) => {
|
||||
if (result.error) {
|
||||
await bot.sendMessage(chatId, `❌ ${result.error}`)
|
||||
}
|
||||
else {
|
||||
const lines: string[] = []
|
||||
|
||||
lines.push(`📊 <b>Итого по запросу «${settings.searchQuery}»</b>`)
|
||||
lines.push(`✅ Откликнулся: ${result.applied.length}`)
|
||||
lines.push(`⏭ Пропущено: ${result.skipped.length}`)
|
||||
if (result.errors.length)
|
||||
lines.push(`❌ Ошибок: ${result.errors.length}`)
|
||||
|
||||
if (result.skipped.length) {
|
||||
lines.push('')
|
||||
lines.push('⏭ <b>Пропущенные:</b>')
|
||||
result.skipped.forEach(v => lines.push(`• <a href="${v.href}">${v.title}</a>`))
|
||||
}
|
||||
|
||||
if (result.errors.length) {
|
||||
lines.push('')
|
||||
lines.push('❌ <b>Ошибки:</b>')
|
||||
result.errors.forEach(v => lines.push(`• <a href="${v.href}">${v.title}</a> — ${v.message}`))
|
||||
}
|
||||
|
||||
await bot.sendMessage(chatId, lines.join('\n'), {
|
||||
parse_mode: 'HTML',
|
||||
disable_web_page_preview: true,
|
||||
})
|
||||
}
|
||||
await showMenu(chatId)
|
||||
})
|
||||
break
|
||||
}
|
||||
|
||||
case 'hh_status': {
|
||||
const isAuth = await checkIsAuth(chatId)
|
||||
await showResult(
|
||||
chatId,
|
||||
messageId,
|
||||
`⚙️ Настройки:\n\nЗапрос: ${settings.searchQuery}\nМакс откликов: ${settings.maxApplies}\nАвто: ${state.autoCron ? '✅ включено' : '😬 выключено'}\nАвторизован: ${isAuth}`,
|
||||
)
|
||||
break
|
||||
}
|
||||
|
||||
case 'hh_my_resume': {
|
||||
const resume = await prisma.resume.findFirst({
|
||||
where: { telegramId: chatId },
|
||||
orderBy: { id: 'asc' },
|
||||
})
|
||||
if (!resume) {
|
||||
await showResult(chatId, messageId, '📋 Резюме не найдено.\n\nВыбери резюме через кнопку 📄 Выбрать резюме.')
|
||||
break
|
||||
}
|
||||
|
||||
const MAX = 3800
|
||||
const text = resume.data.length > MAX
|
||||
? `${resume.data.slice(0, MAX)}\n\n… (текст обрезан)`
|
||||
: resume.data
|
||||
|
||||
await bot.editMessageText(
|
||||
`📋 <b>Твоё резюме</b>\n<pre>${escapeHtml(text)}</pre>`,
|
||||
{
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
parse_mode: 'HTML',
|
||||
reply_markup: BACK_MARKUP,
|
||||
},
|
||||
)
|
||||
break
|
||||
}
|
||||
|
||||
case 'hh_login':
|
||||
state.awaitingEmail = true
|
||||
state.menuMessageId = messageId
|
||||
if (!user?.hhEmail) {
|
||||
state.awaitingEmail = true
|
||||
await bot.sendMessage(chatId, '📧 Введи email от hh.ru:')
|
||||
}
|
||||
else {
|
||||
await bot.sendMessage(chatId, `📧 Email от hh.ru: ${user?.hhEmail}`)
|
||||
state.awaitingEmail = true
|
||||
const prompt = await bot.sendMessage(
|
||||
chatId,
|
||||
`📧 Текущий email: <b>${user.hhEmail}</b>\n\nИспользовать его или введи другой:`,
|
||||
{
|
||||
parse_mode: 'HTML',
|
||||
reply_markup: {
|
||||
inline_keyboard: [[
|
||||
{ text: `✅ Войти как ${user.hhEmail}`, callback_data: 'hh_login_use_current' },
|
||||
]],
|
||||
},
|
||||
},
|
||||
)
|
||||
state.loginPromptMessageId = prompt.message_id
|
||||
}
|
||||
break
|
||||
|
||||
case 'hh_login_use_current': {
|
||||
state.awaitingEmail = false
|
||||
await bot.deleteMessage(chatId, messageId).catch(() => {})
|
||||
state.loginPromptMessageId = null
|
||||
const email = user?.hhEmail
|
||||
if (!email) {
|
||||
await bot.sendMessage(chatId, '❌ Email не найден, введи вручную')
|
||||
state.awaitingEmail = true
|
||||
break
|
||||
}
|
||||
await doLogin(chatId, email)
|
||||
break
|
||||
}
|
||||
|
||||
case 'hh_query':
|
||||
state.awaitingQuery = true
|
||||
state.menuMessageId = messageId
|
||||
await bot.sendMessage(chatId, '🔍 Введи поисковый запрос:')
|
||||
break
|
||||
|
||||
case 'hh_max':
|
||||
state.awaitingMax = true
|
||||
state.menuMessageId = messageId
|
||||
await bot.sendMessage(chatId, '🔢 Введи максимальное количество откликов (1-50):')
|
||||
break
|
||||
|
||||
case 'hh_auto_start':
|
||||
if (state.autoCron) {
|
||||
await bot.sendMessage(chatId, 'Уже запущено!')
|
||||
await showResult(chatId, messageId, '⚠️ Авто уже запущено')
|
||||
break
|
||||
}
|
||||
state.autoCron = cron.schedule('0 10 * * 1-5', async () => {
|
||||
await bot.sendMessage(chatId, '⏰ Авто-отклик...')
|
||||
})
|
||||
await bot.sendMessage(chatId, '✅ Авто включён (пн-пт, 10:00)')
|
||||
await showResult(chatId, messageId, '✅ Авто включён (пн-пт, 10:00)')
|
||||
break
|
||||
|
||||
case 'hh_auto_stop':
|
||||
state.autoCron?.stop()
|
||||
state.autoCron = null
|
||||
await bot.sendMessage(chatId, '⛔ Авто остановлен')
|
||||
await showResult(chatId, messageId, '⛔ Авто остановлен')
|
||||
break
|
||||
|
||||
case 'hh_resume_list': {
|
||||
await bot.sendMessage(chatId, '🔄 Загружаю список резюме...')
|
||||
await bot.editMessageText('🔄 Загружаю список резюме...', {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
reply_markup: { inline_keyboard: [] },
|
||||
})
|
||||
const resumes = await listResumes(chatId)
|
||||
if (resumes.length === 0) {
|
||||
await bot.sendMessage(chatId, '❌ Резюме не найдены. Создайте резюме на hh.ru')
|
||||
await showResult(chatId, messageId, '😬 Резюме не найдены. Создайте резюме на hh.ru')
|
||||
}
|
||||
else if (resumes.length === 1) {
|
||||
await bot.sendMessage(chatId, '🔄 Сохраняю резюме...')
|
||||
await bot.editMessageText('🔄 Сохраняю резюме...', {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
reply_markup: { inline_keyboard: [] },
|
||||
})
|
||||
await saveResume(chatId, resumes[0].href)
|
||||
await bot.sendMessage(chatId, `✅ Резюме сохранено: ${resumes[0].title}`)
|
||||
await showResult(chatId, messageId, `✅ Резюме сохранено: ${resumes[0].title}`)
|
||||
}
|
||||
else {
|
||||
await sendResumeSelector(chatId, resumes)
|
||||
state.menuMessageId = messageId
|
||||
await sendResumeSelector(chatId, resumes, messageId)
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -171,13 +354,17 @@ export function registerHHCommands() {
|
||||
const idx = Number(query.data.replace('hh_resume_pick_', ''))
|
||||
const resume = state.pendingResumes[idx]
|
||||
if (!resume) {
|
||||
await bot.sendMessage(chatId, '❌ Резюме не найдено, попробуйте снова')
|
||||
await showResult(chatId, messageId, '😬 Резюме не найдено, попробуйте снова')
|
||||
break
|
||||
}
|
||||
await bot.sendMessage(chatId, '🔄 Сохраняю резюме...')
|
||||
await bot.editMessageText('🔄 Сохраняю резюме...', {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
reply_markup: { inline_keyboard: [] },
|
||||
})
|
||||
await saveResume(chatId, resume.href)
|
||||
await bot.sendMessage(chatId, `✅ Резюме выбрано: ${resume.title}`)
|
||||
state.pendingResumes = []
|
||||
await showResult(chatId, messageId, `✅ Резюме выбрано: ${resume.title}`)
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -198,62 +385,42 @@ export function registerHHCommands() {
|
||||
})
|
||||
|
||||
if (state.awaitingEmail) {
|
||||
state.tempEmail = user?.hhEmail || msg.text
|
||||
state.awaitingEmail = false
|
||||
|
||||
await bot.deleteMessage(chatId, msg.message_id).catch(() => {})
|
||||
await bot.sendMessage(chatId, '🔄 Логинюсь...')
|
||||
try {
|
||||
await login(state.tempEmail, chatId)
|
||||
await bot.sendMessage(chatId, '✅ Авторизован! Куки сохранены.')
|
||||
|
||||
await prisma.user.update({
|
||||
where: { telegramId: chatId },
|
||||
data: { hhEmail: state.tempEmail },
|
||||
})
|
||||
|
||||
const resumes = await listResumes(chatId)
|
||||
if (resumes.length === 0) {
|
||||
await bot.sendMessage(chatId, '❌ Резюме не найдены. Создайте резюме на hh.ru')
|
||||
}
|
||||
else if (resumes.length === 1) {
|
||||
await saveResume(chatId, resumes[0].href)
|
||||
await bot.sendMessage(chatId, `✅ Резюме сохранено: ${resumes[0].title}`)
|
||||
}
|
||||
else {
|
||||
await sendResumeSelector(chatId, resumes)
|
||||
}
|
||||
|
||||
triggerHHStart(chatId)
|
||||
}
|
||||
catch (e) {
|
||||
await bot.sendMessage(chatId, `😬 Ошибка: ${(e as Error).message}`)
|
||||
if (state.loginPromptMessageId) {
|
||||
await bot.deleteMessage(chatId, state.loginPromptMessageId).catch(() => {})
|
||||
state.loginPromptMessageId = null
|
||||
}
|
||||
await doLogin(chatId, msg.text)
|
||||
return
|
||||
}
|
||||
|
||||
if (state.awaitingQuery) {
|
||||
state.awaitingQuery = false
|
||||
await bot.deleteMessage(chatId, msg.message_id).catch(() => {})
|
||||
const updated = await prisma.settings.update({
|
||||
where: { telegramId: chatId },
|
||||
data: { searchQuery: msg.text },
|
||||
})
|
||||
await bot.sendMessage(chatId, `✅ Запрос: "${updated.searchQuery}"`)
|
||||
await showMenu(chatId)
|
||||
return
|
||||
}
|
||||
|
||||
if (state.awaitingMax) {
|
||||
state.awaitingMax = false
|
||||
const num = Number(msg.text)
|
||||
if (num < 1 || num > 50) {
|
||||
await bot.sendMessage(chatId, '❌ Число от 1 до 50')
|
||||
if (Number.isNaN(num) || num < 1 || num > 50) {
|
||||
await bot.sendMessage(chatId, '😬 Введи число от 1 до 50:')
|
||||
return
|
||||
}
|
||||
state.awaitingMax = false
|
||||
await bot.deleteMessage(chatId, msg.message_id).catch(() => {})
|
||||
const updated = await prisma.settings.update({
|
||||
where: { telegramId: chatId },
|
||||
data: { maxApplies: num },
|
||||
})
|
||||
await bot.sendMessage(chatId, `✅ Макс откликов: ${updated.maxApplies}`)
|
||||
await showMenu(chatId)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user