pre=deploy

This commit is contained in:
2025-11-24 17:00:20 +09:00
parent 240864617f
commit 9106af4f8e
14 changed files with 1979 additions and 26 deletions

View File

@@ -9,6 +9,7 @@ import { CommandHandlers } from './handlers/commandHandlers';
import { CallbackHandlers } from './handlers/callbackHandlers';
import { MessageHandlers } from './handlers/messageHandlers';
import { NotificationHandlers } from './handlers/notificationHandlers';
import { LanguageHandlers } from './handlers/languageHandlers';
class TelegramTinderBot {
@@ -21,6 +22,7 @@ class TelegramTinderBot {
private callbackHandlers: CallbackHandlers;
private messageHandlers: MessageHandlers;
private notificationHandlers: NotificationHandlers;
private languageHandlers: LanguageHandlers;
constructor() {
const token = process.env.TELEGRAM_BOT_TOKEN;
if (!token) {
@@ -37,6 +39,7 @@ class TelegramTinderBot {
this.messageHandlers = new MessageHandlers(this.bot, this.notificationService);
this.callbackHandlers = new CallbackHandlers(this.bot, this.messageHandlers);
this.notificationHandlers = new NotificationHandlers(this.bot);
this.languageHandlers = new LanguageHandlers(this.bot);
this.setupErrorHandling();
this.setupPeriodicTasks();

View File

@@ -49,6 +49,21 @@ export class CallbackHandlers {
this.likeBackHandler = new LikeBackHandler(bot);
}
// Вспомогательный метод для получения перевода с учетом языка пользователя
private async getTranslation(userId: string, key: string, options?: any): Promise<string> {
try {
const lang = await this.profileService.getUserLanguage(userId);
const LocalizationService = require('../services/localizationService').default;
const locService = LocalizationService.getInstance();
locService.setLanguage(lang);
return locService.t(key, options);
} catch (error) {
console.error('Translation error:', error);
// Возвращаем ключ как fallback
return key;
}
}
register(): void {
this.bot.on('callback_query', (query) => this.handleCallback(query));
}
@@ -61,6 +76,14 @@ export class CallbackHandlers {
const data = query.data;
try {
// Обработка выбора языка
if (data.startsWith('set_lang_')) {
const LanguageHandlers = require('./languageHandlers').LanguageHandlers;
const languageHandlers = new LanguageHandlers(this.bot);
await languageHandlers.handleSetLanguage(query);
return;
}
// Основные действия профиля
if (data === 'create_profile') {
await this.handleCreateProfile(chatId, telegramId);
@@ -105,11 +128,13 @@ export class CallbackHandlers {
}
await this.bot.sendMessage(chatId, `✅ Город подтверждён: *${userState.data.city}*\n\n📝 Теперь расскажите немного о себе (био):`, { parse_mode: 'Markdown' });
} else {
await this.bot.answerCallbackQuery(query.id, { text: 'Контекст не найден. Повторите, пожалуйста.' });
const errorText = await this.getTranslation(telegramId, 'errors.contextNotFound');
await this.bot.answerCallbackQuery(query.id, { text: errorText });
}
} catch (error) {
console.error('Error confirming city via callback:', error);
await this.bot.answerCallbackQuery(query.id, { text: 'Ошибка при подтверждении города' });
const errorText = await this.getTranslation(telegramId, 'errors.cityConfirmError');
await this.bot.answerCallbackQuery(query.id, { text: errorText });
}
} else if (data === 'edit_city_manual') {
try {
@@ -124,11 +149,13 @@ export class CallbackHandlers {
} catch (e) { }
await this.bot.sendMessage(chatId, '✏️ Введите название вашего города вручную:', { reply_markup: { remove_keyboard: true } });
} else {
await this.bot.answerCallbackQuery(query.id, { text: 'Контекст не найден. Повторите, пожалуйста.' });
const errorText = await this.getTranslation(telegramId, 'errors.contextNotFound');
await this.bot.answerCallbackQuery(query.id, { text: errorText });
}
} catch (error) {
console.error('Error switching to manual city input via callback:', error);
await this.bot.answerCallbackQuery(query.id, { text: 'Ошибка' });
const errorText = await this.getTranslation(telegramId, 'errors.generalError');
await this.bot.answerCallbackQuery(query.id, { text: errorText });
}
} else if (data === 'confirm_city_edit') {
try {
@@ -159,14 +186,17 @@ export class CallbackHandlers {
[{ text: '🏠 Главное меню', callback_data: 'main_menu' }]
]
};
await this.bot.sendMessage(chatId, 'Выберите действие:', { reply_markup: keyboard });
const selectActionText = await this.getTranslation(telegramId, 'buttons.selectAction');
await this.bot.sendMessage(chatId, selectActionText, { reply_markup: keyboard });
}, 500);
} else {
await this.bot.answerCallbackQuery(query.id, { text: 'Контекст не найден. Повторите, пожалуйста.' });
const errorText = await this.getTranslation(telegramId, 'errors.contextNotFound');
await this.bot.answerCallbackQuery(query.id, { text: errorText });
}
} catch (error) {
console.error('Error confirming city edit via callback:', error);
await this.bot.answerCallbackQuery(query.id, { text: 'Ошибка при подтверждении города' });
const errorText = await this.getTranslation(telegramId, 'errors.cityConfirmError');
await this.bot.answerCallbackQuery(query.id, { text: errorText });
}
} else if (data === 'edit_city_manual_edit') {
try {
@@ -423,15 +453,17 @@ export class CallbackHandlers {
// Эти коллбэки уже обрабатываются в NotificationHandlers, поэтому здесь ничего не делаем
// NotificationHandlers уже зарегистрировал свои обработчики в register()
} else {
const errorText = await this.getTranslation(telegramId, 'notifications.unavailable');
await this.bot.answerCallbackQuery(query.id, {
text: 'Функция настройки уведомлений недоступна.',
text: errorText,
show_alert: true
});
}
}
else {
const devText = await this.getTranslation(telegramId, 'notifications.inDevelopment');
await this.bot.answerCallbackQuery(query.id, {
text: 'Функция в разработке!',
text: devText,
show_alert: false
});
return;
@@ -441,8 +473,9 @@ export class CallbackHandlers {
} catch (error) {
console.error('Callback handler error:', error);
const errorText = await this.getTranslation(telegramId, 'errors.tryAgain');
await this.bot.answerCallbackQuery(query.id, {
text: 'Произошла ошибка. Попробуйте еще раз.',
text: errorText,
show_alert: true
});
}
@@ -540,11 +573,12 @@ export class CallbackHandlers {
]
};
const matchText = await this.getTranslation(telegramId, 'matches.mutualLike', {
name: targetProfile?.name || await this.getTranslation(telegramId, 'common.thisUser')
});
await this.bot.sendMessage(
chatId,
'🎉 ЭТО МАТЧ! 💕\n\n' +
'Вы понравились друг другу с ' + (targetProfile?.name || 'этим пользователем') + '!\n\n' +
'Теперь вы можете начать общение!',
'🎉 ЭТО МАТЧ! 💕\n\n' + matchText,
{ reply_markup: keyboard }
);
} else {
@@ -610,11 +644,12 @@ export class CallbackHandlers {
]
};
const superMatchText = await this.getTranslation(telegramId, 'matches.superLikeMatch', {
name: targetProfile?.name || await this.getTranslation(telegramId, 'common.thisUser')
});
await this.bot.sendMessage(
chatId,
'💖 СУПЕР МАТЧ! ⭐\n\n' +
'Ваш супер лайк произвел впечатление на ' + (targetProfile?.name || 'этого пользователя') + '!\n\n' +
'Начните общение первыми!',
'💖 СУПЕР МАТЧ! ⭐\n\n' + superMatchText,
{ reply_markup: keyboard }
);
} else {

View File

@@ -4,10 +4,12 @@ import { MatchingService } from '../services/matchingService';
import { Profile } from '../models/Profile';
import { getUserTranslation } from '../services/localizationService';
import { NotificationHandlers } from './notificationHandlers';
import { LanguageHandlers } from './languageHandlers';
export class CommandHandlers {
private bot: TelegramBot;
private profileService: ProfileService;
private languageHandlers: LanguageHandlers;
private matchingService: MatchingService;
private notificationHandlers: NotificationHandlers;
@@ -16,6 +18,7 @@ export class CommandHandlers {
this.profileService = new ProfileService();
this.matchingService = new MatchingService();
this.notificationHandlers = new NotificationHandlers(bot);
this.languageHandlers = new LanguageHandlers(bot);
}
register(): void {
@@ -38,6 +41,12 @@ export class CommandHandlers {
const userId = msg.from?.id.toString();
if (!userId) return;
// Проверяем, нужно ли показать выбор языка новому пользователю
const languageSelectionShown = await this.languageHandlers.checkAndShowLanguageSelection(userId, msg.chat.id);
if (languageSelectionShown) {
return; // Показали выбор языка, ждем ответа пользователя
}
// Проверяем есть ли у пользователя профиль
const existingProfile = await this.profileService.getProfileByTelegramId(userId);

View File

@@ -0,0 +1,167 @@
import TelegramBot, { CallbackQuery, InlineKeyboardMarkup } from 'node-telegram-bot-api';
import { ProfileService } from '../services/profileService';
import LocalizationService from '../services/localizationService';
export class LanguageHandlers {
private bot: TelegramBot;
private profileService: ProfileService;
private localizationService: LocalizationService;
constructor(bot: TelegramBot) {
this.bot = bot;
this.profileService = new ProfileService();
this.localizationService = LocalizationService.getInstance();
}
/**
* Показать меню выбора языка
*/
async showLanguageSelection(chatId: number, messageId?: number): Promise<void> {
const keyboard: InlineKeyboardMarkup = {
inline_keyboard: [
[
{ text: '🇷🇺 Русский', callback_data: 'set_lang_ru' },
{ text: '🇬🇧 English', callback_data: 'set_lang_en' }
],
[
{ text: '🇪🇸 Español', callback_data: 'set_lang_es' },
{ text: '🇫🇷 Français', callback_data: 'set_lang_fr' }
],
[
{ text: '🇩🇪 Deutsch', callback_data: 'set_lang_de' },
{ text: '🇮🇹 Italiano', callback_data: 'set_lang_it' }
],
[
{ text: '🇵🇹 Português', callback_data: 'set_lang_pt' },
{ text: '🇰🇷 한국어', callback_data: 'set_lang_ko' }
],
[
{ text: '🇨🇳 中文', callback_data: 'set_lang_zh' },
{ text: '🇯🇵 日本語', callback_data: 'set_lang_ja' }
]
]
};
const text =
'🌍 Choose your language / Выберите язык:\n\n' +
'🇷🇺 Русский\n' +
'🇬🇧 English\n' +
'🇪🇸 Español\n' +
'🇫🇷 Français\n' +
'🇩🇪 Deutsch\n' +
'🇮🇹 Italiano\n' +
'🇵🇹 Português\n' +
'🇰🇷 한국어\n' +
'🇨🇳 中文\n' +
'🇯🇵 日本語';
if (messageId) {
// Обновляем существующее сообщение
await this.bot.editMessageText(text, {
chat_id: chatId,
message_id: messageId,
reply_markup: keyboard
});
} else {
// Отправляем новое сообщение
await this.bot.sendMessage(chatId, text, { reply_markup: keyboard });
}
}
/**
* Обработать установку языка
*/
async handleSetLanguage(query: CallbackQuery): Promise<void> {
const chatId = query.message?.chat.id;
const userId = query.from.id.toString();
const messageId = query.message?.message_id;
if (!chatId || !userId) return;
// Извлекаем код языка из callback_data (например, 'set_lang_ru' -> 'ru')
const langCode = query.data?.replace('set_lang_', '');
if (!langCode) return;
try {
// Проверяем, поддерживается ли язык
const supportedLanguages = this.localizationService.getSupportedLanguages();
if (!supportedLanguages.includes(langCode)) {
await this.bot.answerCallbackQuery(query.id, {
text: '❌ Unsupported language / Язык не поддерживается'
});
return;
}
// Обновляем язык пользователя в базе данных
await this.profileService.updateUserLanguage(userId, langCode);
// Устанавливаем язык в сервисе локализации
this.localizationService.setLanguage(langCode);
// Получаем переведенное сообщение об успехе
const successMessage = this.localizationService.t('language.changed');
// Показываем уведомление об успехе
await this.bot.answerCallbackQuery(query.id, {
text: successMessage,
show_alert: false
});
// Удаляем сообщение с выбором языка
if (messageId) {
await this.bot.deleteMessage(chatId, messageId);
}
// Показываем приветственное сообщение на выбранном языке
const keyboard: InlineKeyboardMarkup = {
inline_keyboard: [
[{ text: this.localizationService.t('start.createProfile'), callback_data: 'create_profile' }],
[{ text: this.localizationService.t('start.howItWorks'), callback_data: 'how_it_works' }]
]
};
await this.bot.sendMessage(
chatId,
this.localizationService.t('start.welcomeNew'),
{ reply_markup: keyboard }
);
} catch (error) {
console.error('Error setting language:', error);
await this.bot.answerCallbackQuery(query.id, {
text: '❌ Error / Ошибка'
});
}
}
/**
* Проверить, нужно ли показать выбор языка новому пользователю
*/
async checkAndShowLanguageSelection(userId: string, chatId: number): Promise<boolean> {
try {
// Получаем текущий язык пользователя
const currentLang = await this.profileService.getUserLanguage(userId);
// Если язык уже установлен, не показываем выбор
if (currentLang && currentLang !== 'ru') {
return false;
}
// Проверяем, есть ли у пользователя профиль
const profile = await this.profileService.getProfileByTelegramId(userId);
// Показываем выбор языка только новым пользователям без профиля
if (!profile) {
await this.showLanguageSelection(chatId);
return true;
}
return false;
} catch (error) {
console.error('Error checking language selection:', error);
return false;
}
}
}
export default LanguageHandlers;

View File

@@ -1,4 +1,18 @@
{
"language": {
"select": "🌍 Select interface language:\n\nYou can change the language later in settings.",
"changed": "✅ Language changed to English",
"ru": "🇷🇺 Русский",
"en": "🇬🇧 English",
"es": "🇪🇸 Español",
"fr": "🇫🇷 Français",
"de": "🇩🇪 Deutsch",
"it": "🇮🇹 Italiano",
"pt": "🇵🇹 Português",
"zh": "🇨🇳 中文",
"ja": "🇯🇵 日本語",
"ko": "🇰🇷 한국어"
},
"welcome": {
"greeting": "Welcome to Telegram Tinder Bot! 💕",
"description": "Find your soulmate right here!",

View File

@@ -1,4 +1,18 @@
{
"language": {
"select": "🌍 Выберите язык интерфейса:\n\nВы сможете изменить язык позже в настройках.",
"changed": "✅ Язык изменен на Русский",
"ru": "🇷🇺 Русский",
"en": "🇬🇧 English",
"es": "🇪🇸 Español",
"fr": "🇫🇷 Français",
"de": "🇩🇪 Deutsch",
"it": "🇮🇹 Italiano",
"pt": "🇵🇹 Português",
"zh": "🇨🇳 中文",
"ja": "🇯🇵 日本語",
"ko": "🇰🇷 한국어"
},
"welcome": {
"greeting": "Добро пожаловать в Telegram Tinder Bot! 💕",
"description": "Найди свою вторую половинку прямо здесь!",
@@ -83,7 +97,12 @@
"matches": "Взаимности",
"premium": "Премиум",
"settings": "Настройки",
"help": "Помощь"
"help": "Помощь",
"notifications": "Уведомления"
},
"notifications": {
"unavailable": "Функция настройки уведомлений недоступна.",
"inDevelopment": "Функция в разработке!"
},
"buttons": {
"back": "« Назад",
@@ -94,7 +113,8 @@
"edit": "Редактировать",
"delete": "Удалить",
"yes": "Да",
"no": "Нет"
"no": "Нет",
"selectAction": "Выберите действие:"
},
"errors": {
"profileNotFound": "Анкета не найдена",
@@ -102,13 +122,29 @@
"ageInvalid": "Введите корректный возраст (18-100)",
"photoRequired": "Добавьте хотя бы одну фотографию",
"networkError": "Ошибка сети. Попробуйте позже.",
"serverError": "Ошибка сервера. Попробуйте позже."
"serverError": "Ошибка сервера. Попробуйте позже.",
"contextNotFound": "Контекст не найден. Повторите, пожалуйста.",
"cityConfirmError": "Ошибка при подтверждении города",
"generalError": "Ошибка",
"tryAgain": "Произошла ошибка. Попробуйте еще раз."
},
"common": {
"back": "👈 Назад"
"back": "👈 Назад",
"thisUser": "этим пользователем"
},
"matches": {
"noMatches": "✨ У вас пока нет матчей.\n\n🔍 Попробуйте просмотреть больше анкет!\nИспользуйте /browse для поиска."
"noMatches": "✨ У вас пока нет матчей.\n\n🔍 Попробуйте просмотреть больше анкет!\nИспользуйте /browse для поиска.",
"title": "Ваши матчи ({count})",
"mutualLike": "Вы понравились друг другу с {name}!\n\nТеперь вы можете начать общение!",
"superLikeMatch": "Ваш супер лайк произвел впечатление на {name}!\n\nНачните общение первыми!",
"likeBackMatch": "Теперь вы можете начать общение.",
"likeNotification": "Если вы также понравитесь этому пользователю, будет создан матч.",
"tryMoreProfiles": "Попробуйте просмотреть больше анкет!",
"startBrowsing": "Начните просматривать анкеты и получите первые матчи!",
"newMatch": "Новый матч",
"youSaid": "Вы",
"unmatchConfirm": "Вы больше не увидите этого пользователя в своих матчах.",
"bioMissing": "Описание отсутствует"
},
"start": {
"welcomeBack": "🎉 С возвращением, {name}!\n\n💖 Telegram Tinder Bot готов к работе!\n\nЧто хотите сделать?",

View File

@@ -147,26 +147,44 @@ export class ProfileService {
}
// Создание пользователя если не существует
async ensureUser(telegramId: string, userData: any): Promise<string> {
async ensureUser(telegramId: string, userData: any, language: string = 'ru'): Promise<string> {
// Используем UPSERT для избежания дублирования
const result = await query(`
INSERT INTO users (telegram_id, username, first_name, last_name)
VALUES ($1, $2, $3, $4)
INSERT INTO users (telegram_id, username, first_name, last_name, lang)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (telegram_id) DO UPDATE SET
username = EXCLUDED.username,
first_name = EXCLUDED.first_name,
last_name = EXCLUDED.last_name
last_name = EXCLUDED.last_name,
lang = COALESCE(users.lang, EXCLUDED.lang)
RETURNING id
`, [
parseInt(telegramId),
userData.username || null,
userData.first_name || null,
userData.last_name || null
userData.last_name || null,
language
]);
return result.rows[0].id;
}
// Обновление языка пользователя
async updateUserLanguage(telegramId: string, language: string): Promise<void> {
await query(`
UPDATE users SET lang = $1 WHERE telegram_id = $2
`, [language, parseInt(telegramId)]);
}
// Получение языка пользователя
async getUserLanguage(telegramId: string): Promise<string> {
const result = await query(`
SELECT lang FROM users WHERE telegram_id = $1
`, [parseInt(telegramId)]);
return result.rows.length > 0 ? result.rows[0].lang : 'ru';
}
// Обновление профиля
async updateProfile(userId: string, updates: Partial<ProfileData>): Promise<Profile> {
const existingProfile = await this.getProfileByUserId(userId);