🔧 refactor(bot-commands): обновлено использование функции safeEdit в нескольких местах

This commit is contained in:
Oscar
2026-05-28 10:08:11 +03:00
parent 20a5f506da
commit 00d0a8d832
10 changed files with 363 additions and 21 deletions

View File

@@ -3,7 +3,7 @@ import bot from '@bot'
import prisma from '@prisma'
import cron, { type ScheduledTask } from 'node-cron'
import { applyToJobs, checkIsAuth, listResumes, login, saveResume } from './scraper.js'
import { BACK_MARKUP, createStatusReporter, escapeHtml, LOGIN_MARKUP, MAIN_MARKUP, showResult } from './ui.js'
import { BACK_MARKUP, createStatusReporter, escapeHtml, LOGIN_MARKUP, MAIN_MARKUP, safeEdit, showResult } from './ui.js'
interface UserState {
autoCron: ScheduledTask | null
@@ -43,7 +43,7 @@ async function showMenu(chatId: number, messageId?: number | null): Promise<void
if (targetId) {
try {
await bot.editMessageText('🤖 HH Auto-Apply', {
await safeEdit('🤖 HH Auto-Apply', {
chat_id: chatId,
message_id: targetId,
reply_markup: MAIN_MARKUP,
@@ -63,7 +63,7 @@ async function showMenu(chatId: number, messageId?: number | null): Promise<void
async function sendResumeSelector(chatId: number, resumes: ResumeListItem[], messageId: number): Promise<void> {
const state = getState(chatId)
state.pendingResumes = resumes
await bot.editMessageText('📄 Выбери резюме:', {
await safeEdit('📄 Выбери резюме:', {
chat_id: chatId,
message_id: messageId,
reply_markup: {
@@ -241,7 +241,7 @@ export function registerHHCommands() {
const text = resume.data.length > MAX
? `${resume.data.slice(0, MAX)}\n\n… (текст обрезан)`
: resume.data
await bot.editMessageText(
await safeEdit(
`📋 <b>Твоё резюме:</b>\n <b>${resume.title}</b>\n<pre>${escapeHtml(text)}</pre>`,
{
chat_id: chatId,
@@ -340,7 +340,7 @@ export function registerHHCommands() {
}
const lines = ['🚫 <b>Вакансии с опросником (бот не может откликнуться):</b>', '']
skipped.forEach(v => lines.push(`• <a href="${escapeHtml(v.href)}">${escapeHtml(v.title)}</a>`))
await bot.editMessageText(lines.join('\n'), {
await safeEdit(lines.join('\n'), {
chat_id: chatId,
message_id: messageId,
parse_mode: 'HTML',
@@ -351,7 +351,7 @@ export function registerHHCommands() {
}
case 'hh_resume_list': {
await bot.editMessageText('🔄 Загружаю список резюме...', {
await safeEdit('🔄 Загружаю список резюме...', {
chat_id: chatId,
message_id: messageId,
reply_markup: { inline_keyboard: [] },
@@ -361,7 +361,7 @@ export function registerHHCommands() {
await showResult(chatId, messageId, '⚠️ Резюме не найдены. Создайте резюме на hh.ru')
}
else if (resumes.length === 1) {
await bot.editMessageText('🔄 Сохраняю резюме...', {
await safeEdit('🔄 Сохраняю резюме...', {
chat_id: chatId,
message_id: messageId,
reply_markup: { inline_keyboard: [] },
@@ -384,7 +384,7 @@ export function registerHHCommands() {
await showResult(chatId, messageId, '❌ Резюме не найдено, попробуйте снова')
break
}
await bot.editMessageText('🔄 Сохраняю резюме...', {
await safeEdit('🔄 Сохраняю резюме...', {
chat_id: chatId,
message_id: messageId,
reply_markup: { inline_keyboard: [] },

View File

@@ -37,8 +37,19 @@ export function escapeHtml(text: string): string {
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
}
export async function safeEdit(
text: string,
options: Parameters<typeof bot.editMessageText>[1],
): Promise<void> {
await bot.editMessageText(text, options).catch((e: unknown) => {
if (e instanceof Error && e.message.includes('message is not modified'))
return
throw e
})
}
export async function showResult(chatId: number, messageId: number, text: string): Promise<void> {
await bot.editMessageText(text, {
await safeEdit(text, {
chat_id: chatId,
message_id: messageId,
reply_markup: BACK_MARKUP,

View File

@@ -1,6 +1,11 @@
import bot from '@bot'
import prisma from '@prisma'
process.on('unhandledRejection', (reason) => {
const msg = reason instanceof Error ? reason.message : String(reason)
console.error('[unhandledRejection]', msg)
})
import { registerHHCommands, triggerHHStart } from './hh/bot-commands.js'
registerHHCommands()