| import { BotContext } from '../types/botTypes'; |
| import { Markup } from 'telegraf'; |
| import { getLanguageSelectionKeyboard, getMainMenuKeyboard, getLoggedInMenuKeyboard } from '../utils/keyboardUtils'; |
| import { authService } from '../services/auth'; |
| import { messageManager } from '../utils/messageManager'; |
| import { supabase } from '../../db/supabase'; |
|
|
| export const setupLanguageHandlers = (bot: any) => { |
| |
| |
| |
| bot.action('change_language', async (ctx) => { |
| const result = await handleLanguageSelection(ctx); |
| await ctx.editMessageText(result.message, result.options); |
| }); |
| |
| bot.action('set_language_en', async (ctx) => { |
| const result = await handleLanguageChange(ctx, 'en'); |
| await ctx.editMessageText(result.message, result.options); |
| }); |
| |
| bot.action('set_language_ar', async (ctx) => { |
| const result = await handleLanguageChange(ctx, 'ar'); |
| await ctx.editMessageText(result.message, result.options); |
| }); |
| }; |
|
|
| export const handleLanguageSelection = async (ctx: BotContext) => { |
| const telegramId = ctx.from?.id; |
| const isLoggedIn = authService.isUserLoggedIn(telegramId,ctx) ; |
|
|
| return { |
| message: "🌐 Choose your preferred language:\nاختر لغتك المفضلة:", |
| options: getLanguageSelectionKeyboard(isLoggedIn) |
| }; |
| }; |
|
|
| export const handleLanguageChange = async (ctx: BotContext, language: 'en' | 'ar') => { |
| const telegramId = ctx.from?.id; |
| const isLoggedIn = telegramId ? authService.isUserLoggedIn(telegramId,ctx) : false; |
|
|
| try { |
| |
| messageManager.setLanguage(language); |
|
|
| |
| if (isLoggedIn && telegramId) { |
| const { error } = await supabase |
| .from('users_bot_telegram') |
| .update({ language }) |
| .eq('telegram_id', telegramId); |
|
|
| if (error) throw error; |
| } |
|
|
| const successMessage = language === 'en' |
| ? "✅ Language changed to English" |
| : "✅ تم تغيير اللغة إلى العربية"; |
|
|
| return { |
| message: successMessage, |
| options: isLoggedIn ? getLoggedInMenuKeyboard() : getMainMenuKeyboard() |
| }; |
| } catch (error: any) { |
| const errorMessage = language === 'en' |
| ? "❌ Failed to change language" |
| : "❌ فشل تغيير اللغة"; |
| |
| return { |
| message: errorMessage, |
| options: isLoggedIn ? getLoggedInMenuKeyboard() : getMainMenuKeyboard() |
| }; |
| } |
| }; |