Files
tg_tinder_bot/src/handlers/callbackHandlers.ts.backup-1758166633763
2025-09-18 13:46:35 +09:00

607 lines
28 KiB
Plaintext
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 TelegramBot, { CallbackQuery, InlineKeyboardMarkup } from 'node-telegram-bot-api';
import { ProfileService } from '../services/profileService';
import { MatchingService } from '../services/matchingService';
import { ChatService } from '../services/chatService';
import { Profile } from '../models/Profile';
import { MessageHandlers } from './messageHandlers';
import { ProfileEditController } from '../controllers/profileEditController';
import { EnhancedChatHandlers } from './enhancedChatHandlers';
import { VipController } from '../controllers/vipController';
import { VipService } from '../services/vipService';
import { TranslationController } from '../controllers/translationController';
import { t } from '../services/localizationService';
import { LikeBackHandler } from './likeBackHandler';
import { NotificationHandlers } from './notificationHandlers';
export class CallbackHandlers {
private bot: TelegramBot;
private profileService: ProfileService;
private matchingService: MatchingService;
private chatService: ChatService;
private messageHandlers: MessageHandlers;
private profileEditController: ProfileEditController;
private enhancedChatHandlers: EnhancedChatHandlers;
private vipController: VipController;
private vipService: VipService;
private translationController: TranslationController;
private likeBackHandler: LikeBackHandler;
private notificationHandlers?: NotificationHandlers;
constructor(bot: TelegramBot, messageHandlers: MessageHandlers) {
this.bot = bot;
this.profileService = new ProfileService();
this.matchingService = new MatchingService();
this.chatService = new ChatService();
this.messageHandlers = messageHandlers;
this.profileEditController = new ProfileEditController(this.profileService);
this.enhancedChatHandlers = new EnhancedChatHandlers(bot);
this.vipController = new VipController(bot);
this.vipService = new VipService();
this.translationController = new TranslationController();
this.likeBackHandler = new LikeBackHandler(bot);
// Создаем экземпляр NotificationHandlers
try {
this.notificationHandlers = new NotificationHandlers(bot);
} catch (error) {
console.error('Failed to initialize NotificationHandlers:', error);
}
}
register(): void {
this.bot.on('callback_query', (query) => this.handleCallback(query));
}
async handleCallback(query: CallbackQuery): Promise<void> {
if (!query.data || !query.from || !query.message) return;
const telegramId = query.from.id.toString();
const chatId = query.message.chat.id;
const data = query.data;
try {
// Основные действия профиля
if (data === 'create_profile') {
await this.handleCreateProfile(chatId, telegramId);
} else if (data.startsWith('gender_')) {
const gender = data.replace('gender_', '');
await this.handleGenderSelection(chatId, telegramId, gender);
} else if (data === 'view_my_profile') {
await this.handleViewMyProfile(chatId, telegramId);
} else if (data === 'edit_profile') {
await this.handleEditProfile(chatId, telegramId);
} else if (data === 'manage_photos') {
await this.handleManagePhotos(chatId, telegramId);
} else if (data === 'preview_profile') {
await this.handlePreviewProfile(chatId, telegramId);
}
// Редактирование полей профиля
else if (data === 'edit_name') {
await this.handleEditName(chatId, telegramId);
} else if (data === 'edit_age') {
await this.handleEditAge(chatId, telegramId);
} else if (data === 'edit_bio') {
await this.handleEditBio(chatId, telegramId);
} else if (data === 'edit_hobbies') {
await this.handleEditHobbies(chatId, telegramId);
} else if (data === 'edit_city') {
await this.handleEditCity(chatId, telegramId);
} else if (data === 'edit_job') {
await this.handleEditJob(chatId, telegramId);
} else if (data === 'edit_education') {
await this.handleEditEducation(chatId, telegramId);
} else if (data === 'edit_height') {
await this.handleEditHeight(chatId, telegramId);
} else if (data === 'edit_religion') {
await this.handleEditReligion(chatId, telegramId);
} else if (data === 'edit_dating_goal') {
await this.handleEditDatingGoal(chatId, telegramId);
} else if (data === 'edit_lifestyle') {
await this.handleEditLifestyle(chatId, telegramId);
} else if (data === 'edit_search_preferences') {
await this.handleEditSearchPreferences(chatId, telegramId);
}
// Управление фотографиями
else if (data === 'add_photo') {
await this.handleAddPhoto(chatId, telegramId);
} else if (data === 'delete_photo') {
await this.handleDeletePhoto(chatId, telegramId);
} else if (data === 'set_main_photo') {
await this.handleSetMainPhoto(chatId, telegramId);
} else if (data.startsWith('delete_photo_')) {
const photoIndex = parseInt(data.replace('delete_photo_', ''));
await this.handleDeletePhotoByIndex(chatId, telegramId, photoIndex);
} else if (data.startsWith('set_main_photo_')) {
const photoIndex = parseInt(data.replace('set_main_photo_', ''));
await this.handleSetMainPhotoByIndex(chatId, telegramId, photoIndex);
}
// Цели знакомства
else if (data.startsWith('set_dating_goal_')) {
const goal = data.replace('set_dating_goal_', '');
await this.handleSetDatingGoal(chatId, telegramId, goal);
}
// Образ жизни
else if (data === 'edit_smoking') {
await this.handleEditSmoking(chatId, telegramId);
} else if (data === 'edit_drinking') {
await this.handleEditDrinking(chatId, telegramId);
} else if (data === 'edit_kids') {
await this.handleEditKids(chatId, telegramId);
} else if (data.startsWith('set_smoking_')) {
const value = data.replace('set_smoking_', '');
await this.handleSetLifestyle(chatId, telegramId, 'smoking', value);
} else if (data.startsWith('set_drinking_')) {
const value = data.replace('set_drinking_', '');
await this.handleSetLifestyle(chatId, telegramId, 'drinking', value);
} else if (data.startsWith('set_kids_')) {
const value = data.replace('set_kids_', '');
await this.handleSetLifestyle(chatId, telegramId, 'kids', value);
}
// Настройки поиска
else if (data === 'edit_age_range') {
await this.handleEditAgeRange(chatId, telegramId);
} else if (data === 'edit_distance') {
await this.handleEditDistance(chatId, telegramId);
}
// Просмотр анкет и свайпы
else if (data === 'start_browsing') {
await this.handleStartBrowsing(chatId, telegramId, false);
} else if (data === 'start_browsing_first') {
// Показываем всех пользователей для нового пользователя
await this.handleStartBrowsing(chatId, telegramId, true);
} else if (data === 'vip_search') {
await this.handleVipSearch(chatId, telegramId);
} else if (data.startsWith('search_by_goal_')) {
const goal = data.replace('search_by_goal_', '');
await this.handleSearchByGoal(chatId, telegramId, goal);
} else if (data === 'next_candidate') {
await this.handleNextCandidate(chatId, telegramId);
} else if (data.startsWith('like_')) {
const targetUserId = data.replace('like_', '');
await this.handleLike(chatId, telegramId, targetUserId);
} else if (data.startsWith('dislike_')) {
const targetUserId = data.replace('dislike_', '');
await this.handleDislike(chatId, telegramId, targetUserId);
} else if (data.startsWith('superlike_')) {
const targetUserId = data.replace('superlike_', '');
await this.handleSuperlike(chatId, telegramId, targetUserId);
} else if (data.startsWith('view_profile_')) {
const targetUserId = data.replace('view_profile_', '');
await this.handleViewProfile(chatId, telegramId, targetUserId);
} else if (data.startsWith('more_photos_')) {
const targetUserId = data.replace('more_photos_', '');
await this.handleMorePhotos(chatId, telegramId, targetUserId);
}
// Обработка лайков и ответных лайков из уведомлений
else if (data.startsWith('like_back:')) {
const targetUserId = data.replace('like_back:', '');
await this.likeBackHandler.handleLikeBack(chatId, telegramId, targetUserId);
}
// Матчи и чаты
else if (data === 'view_matches') {
await this.handleViewMatches(chatId, telegramId);
} else if (data === 'open_chats') {
await this.handleOpenChats(chatId, telegramId);
} else if (data === 'native_chats') {
await this.enhancedChatHandlers.showChatsNative(chatId, telegramId);
} else if (data.startsWith('open_native_chat_')) {
const matchId = data.replace('open_native_chat_', '');
await this.enhancedChatHandlers.openNativeChat(chatId, telegramId, matchId);
} else if (data.startsWith('chat_history_')) {
const matchId = data.replace('chat_history_', '');
await this.enhancedChatHandlers.showChatHistory(chatId, telegramId, matchId);
} else if (data.startsWith('chat_')) {
const matchId = data.replace('chat_', '');
await this.handleOpenChat(chatId, telegramId, matchId);
} else if (data.startsWith('send_message_')) {
const matchId = data.replace('send_message_', '');
await this.handleSendMessage(chatId, telegramId, matchId);
} else if (data.startsWith('view_chat_profile_')) {
const matchId = data.replace('view_chat_profile_', '');
await this.handleViewChatProfile(chatId, telegramId, matchId);
} else if (data.startsWith('unmatch_')) {
const matchId = data.replace('unmatch_', '');
await this.handleUnmatch(chatId, telegramId, matchId);
} else if (data.startsWith('confirm_unmatch_')) {
const matchId = data.replace('confirm_unmatch_', '');
await this.handleConfirmUnmatch(chatId, telegramId, matchId);
}
// Настройки
else if (data === 'settings') {
await this.handleSettings(chatId, telegramId);
} else if (data === 'search_settings') {
await this.handleSearchSettings(chatId, telegramId);
} else if (data === 'notification_settings') {
await this.handleNotificationSettings(chatId, telegramId);
} else if (data === 'view_stats') {
await this.handleViewStats(chatId, telegramId);
} else if (data === 'view_profile_viewers') {
await this.handleViewProfileViewers(chatId, telegramId);
} else if (data === 'hide_profile') {
await this.handleHideProfile(chatId, telegramId);
} else if (data === 'delete_profile') {
await this.handleDeleteProfile(chatId, telegramId);
} else if (data === 'main_menu') {
await this.handleMainMenu(chatId, telegramId);
} else if (data === 'confirm_delete_profile') {
await this.handleConfirmDeleteProfile(chatId, telegramId);
}
// Информация
else if (data === 'how_it_works') {
await this.handleHowItWorks(chatId);
} else if (data === 'back_to_browsing') {
await this.handleStartBrowsing(chatId, telegramId);
} else if (data === 'get_vip') {
await this.vipController.showVipSearch(chatId, telegramId);
}
// VIP функции
else if (data === 'vip_search') {
await this.vipController.showVipSearch(chatId, telegramId);
} else if (data === 'vip_quick_search') {
await this.vipController.performQuickVipSearch(chatId, telegramId);
} else if (data === 'vip_advanced_search') {
await this.vipController.startAdvancedSearch(chatId, telegramId);
} else if (data === 'vip_dating_goal_search') {
await this.vipController.showDatingGoalSearch(chatId, telegramId);
} else if (data.startsWith('vip_goal_')) {
const goal = data.replace('vip_goal_', '');
await this.vipController.performDatingGoalSearch(chatId, telegramId, goal);
} else if (data.startsWith('vip_like_')) {
const targetTelegramId = data.replace('vip_like_', '');
await this.handleVipLike(chatId, telegramId, targetTelegramId);
} else if (data.startsWith('vip_superlike_')) {
const targetTelegramId = data.replace('vip_superlike_', '');
await this.handleVipSuperlike(chatId, telegramId, targetTelegramId);
} else if (data.startsWith('vip_dislike_')) {
const targetTelegramId = data.replace('vip_dislike_', '');
await this.handleVipDislike(chatId, telegramId, targetTelegramId);
}
// Настройки языка и переводы
else if (data === 'language_settings') {
await this.handleLanguageSettings(chatId, telegramId);
} else if (data.startsWith('set_language_')) {
const languageCode = data.replace('set_language_', '');
await this.handleSetLanguage(chatId, telegramId, languageCode);
} else if (data.startsWith('translate_profile_')) {
const profileUserId = parseInt(data.replace('translate_profile_', ''));
await this.handleTranslateProfile(chatId, telegramId, profileUserId);
} else if (data === 'back_to_settings') {
await this.handleSettings(chatId, telegramId);
}
// Настройки уведомлений
else if (data === 'notifications') {
if (this.notificationHandlers) {
const userId = await this.profileService.getUserIdByTelegramId(telegramId);
if (!userId) {
await this.bot.answerCallbackQuery(query.id, { text: '❌ Вы не зарегистрированы.' });
return;
}
const settings = await this.notificationHandlers.getNotificationService().getNotificationSettings(userId);
await this.notificationHandlers.sendNotificationSettings(chatId, settings);
} else {
await this.handleNotificationSettings(chatId, telegramId);
}
}
// Обработка переключения настроек уведомлений
else if (data.startsWith('notif_toggle:') ||
data === 'notif_time' ||
data.startsWith('notif_time_set:') ||
data === 'notif_dnd' ||
data.startsWith('notif_dnd_set:') ||
data === 'notif_dnd_time' ||
data.startsWith('notif_dnd_time_set:') ||
data === 'notif_dnd_time_custom') {
// Делегируем обработку в NotificationHandlers, если он доступен
if (this.notificationHandlers) {
// Эти коллбэки уже обрабатываются в NotificationHandlers, поэтому здесь ничего не делаем
// NotificationHandlers уже зарегистрировал свои обработчики в register()
} else {
await this.bot.answerCallbackQuery(query.id, {
text: 'Функция настройки уведомлений недоступна.',
show_alert: true
});
}
}
else {
await this.bot.answerCallbackQuery(query.id, {
text: 'Функция в разработке!',
show_alert: false
});
return;
}
await this.bot.answerCallbackQuery(query.id);
} catch (error) {
console.error('Callback handler error:', error);
await this.bot.answerCallbackQuery(query.id, {
text: 'Произошла ошибка. Попробуйте еще раз.',
show_alert: true
});
}
}
// Добавим все необходимые методы для обработки коллбэков
async handleCreateProfile(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleGenderSelection(chatId: number, telegramId: string, gender: string): Promise<void> {
// Заглушка метода
}
async handleViewMyProfile(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditProfile(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleManagePhotos(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handlePreviewProfile(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditName(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditAge(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditBio(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditHobbies(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditCity(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditJob(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditEducation(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditHeight(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditReligion(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditDatingGoal(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditLifestyle(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditSearchPreferences(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleAddPhoto(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleDeletePhoto(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleSetMainPhoto(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleDeletePhotoByIndex(chatId: number, telegramId: string, photoIndex: number): Promise<void> {
// Заглушка метода
}
async handleSetMainPhotoByIndex(chatId: number, telegramId: string, photoIndex: number): Promise<void> {
// Заглушка метода
}
async handleSetDatingGoal(chatId: number, telegramId: string, goal: string): Promise<void> {
// Заглушка метода
}
async handleEditSmoking(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditDrinking(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditKids(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleSetLifestyle(chatId: number, telegramId: string, type: string, value: string): Promise<void> {
// Заглушка метода
}
async handleEditAgeRange(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleEditDistance(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleStartBrowsing(chatId: number, telegramId: string, showAll: boolean = false): Promise<void> {
// Заглушка метода
}
async handleVipSearch(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleSearchByGoal(chatId: number, telegramId: string, goal: string): Promise<void> {
// Заглушка метода
}
async handleNextCandidate(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleLike(chatId: number, telegramId: string, targetUserId: string): Promise<void> {
// Заглушка метода
}
async handleDislike(chatId: number, telegramId: string, targetUserId: string): Promise<void> {
// Заглушка метода
}
async handleSuperlike(chatId: number, telegramId: string, targetUserId: string): Promise<void> {
// Заглушка метода
}
async handleViewProfile(chatId: number, telegramId: string, targetUserId: string): Promise<void> {
// Заглушка метода
}
async handleMorePhotos(chatId: number, telegramId: string, targetUserId: string): Promise<void> {
// Заглушка метода
}
async handleViewMatches(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleOpenChats(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleOpenChat(chatId: number, telegramId: string, matchId: string): Promise<void> {
// Заглушка метода
}
async handleSendMessage(chatId: number, telegramId: string, matchId: string): Promise<void> {
// Заглушка метода
}
async handleViewChatProfile(chatId: number, telegramId: string, matchId: string): Promise<void> {
// Заглушка метода
}
async handleUnmatch(chatId: number, telegramId: string, matchId: string): Promise<void> {
// Заглушка метода
}
async handleConfirmUnmatch(chatId: number, telegramId: string, matchId: string): Promise<void> {
// Заглушка метода
}
async handleSettings(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleSearchSettings(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleViewStats(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleViewProfileViewers(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleHideProfile(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleDeleteProfile(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleMainMenu(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleConfirmDeleteProfile(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleHowItWorks(chatId: number): Promise<void> {
// Заглушка метода
}
async handleVipLike(chatId: number, telegramId: string, targetTelegramId: string): Promise<void> {
// Заглушка метода
}
async handleVipSuperlike(chatId: number, telegramId: string, targetTelegramId: string): Promise<void> {
// Заглушка метода
}
async handleVipDislike(chatId: number, telegramId: string, targetTelegramId: string): Promise<void> {
// Заглушка метода
}
async handleLanguageSettings(chatId: number, telegramId: string): Promise<void> {
// Заглушка метода
}
async handleSetLanguage(chatId: number, telegramId: string, languageCode: string): Promise<void> {
// Заглушка метода
}
async handleTranslateProfile(chatId: number, telegramId: string, profileUserId: number): Promise<void> {
// Заглушка метода
}
// Добавим новый метод для настроек уведомлений (вызывает NotificationHandlers)
async handleNotificationSettings(chatId: number, telegramId: string): Promise<void> {
try {
if (this.notificationHandlers) {
const userId = await this.profileService.getUserIdByTelegramId(telegramId);
if (!userId) {
await this.bot.sendMessage(chatId, '❌ Вы не зарегистрированы. Используйте команду /start для регистрации.');
return;
}
// Вызываем метод из notificationHandlers для получения настроек и отображения меню
const settings = await this.notificationHandlers.getNotificationService().getNotificationSettings(userId);
await this.notificationHandlers.sendNotificationSettings(chatId, settings);
} else {
// Если NotificationHandlers недоступен, показываем сообщение об ошибке
await this.bot.sendMessage(chatId, '⚙️ Настройки уведомлений временно недоступны. Попробуйте позже.');
await this.handleSettings(chatId, telegramId);
}
} catch (error) {
console.error('Error handling notification settings:', error);
await this.bot.sendMessage(chatId, '❌ Произошла ошибка при загрузке настроек уведомлений.');
}
}
}