|
|
import { getWelcomeMessage , getHelpMessage, getAboutMessage, getContactMessage} from "../utils/messageUtils"; |
|
|
import { getMainMenuKeyboard, getLoggedInMenuKeyboard, getHistoryKeyboard } from "../utils/keyboardUtils"; |
|
|
import { BotContext } from "../types/botTypes"; |
|
|
import { AuthService } from '../services/auth'; |
|
|
import { PurchaseTrackingService, PurchaseState } from '../services/PurchaseTrackingService'; |
|
|
import { createLogger } from '../../utils/logger'; |
|
|
import { messageReplyHandler } from "../utils/handlerUtils"; |
|
|
import { handleLanguageSelection } from "./languageHandlers"; |
|
|
import { messageManager } from "../utils/messageManager"; |
|
|
|
|
|
|
|
|
const logger = createLogger('CommandHandlers'); |
|
|
const authService = AuthService.getInstance(); |
|
|
const purchaseTrackingService = PurchaseTrackingService.getInstance(); |
|
|
|
|
|
export const setupCommandHandlers = (bot: any) => { |
|
|
|
|
|
bot.start(messageReplyHandler(handleStartCommand)); |
|
|
|
|
|
|
|
|
bot.command('help', messageReplyHandler(handleHelpCommand)); |
|
|
bot.command('about', messageReplyHandler(handleAboutCommand)); |
|
|
bot.command('contact', messageReplyHandler(handleContactCommand)); |
|
|
bot.command('balance', messageReplyHandler(handleBalanceCommand)); |
|
|
bot.command('history', messageReplyHandler(handleHistoryCommand)); |
|
|
|
|
|
|
|
|
bot.command('change_language', messageReplyHandler(handleLanguageCommand)); |
|
|
}; |
|
|
|
|
|
const handleStartCommand = (ctx: BotContext) => { |
|
|
const name = ctx.from?.first_name || "عزيزي المستخدم"; |
|
|
const telegramId = ctx.from?.id; |
|
|
|
|
|
if (telegramId && authService.isUserLoggedIn(telegramId,ctx)) { |
|
|
return { |
|
|
message: messageManager.getMessage('start_welcome_back').replace('{name}', name), |
|
|
options: getLoggedInMenuKeyboard() |
|
|
}; |
|
|
} else { |
|
|
return { |
|
|
message: messageManager.getMessage('start_welcome_new').replace('{name}', name), |
|
|
options: getMainMenuKeyboard() |
|
|
}; |
|
|
} |
|
|
}; |
|
|
|
|
|
const handleAboutCommand = (ctx: BotContext) => { |
|
|
const botData = ctx.botData; |
|
|
|
|
|
return { |
|
|
message: [ |
|
|
messageManager.getMessage('about_title'), |
|
|
messageManager.getMessage('about_bot_name').replace('{bot_name}', botData?.name || 'بوت الأرقام الافتراضية'), |
|
|
messageManager.getMessage('about_version').replace('{version}', botData?.version || '1.0.0'), |
|
|
messageManager.getMessage('about_currency').replace('{currency}', botData?.currency || 'USD'), |
|
|
'', |
|
|
messageManager.getMessage('about_features'), |
|
|
'', |
|
|
messageManager.getMessage('about_copyright') |
|
|
].join('\n'), |
|
|
options: { parse_mode: 'HTML' } |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const handleHelpCommand = (ctx: BotContext) => { |
|
|
return { |
|
|
message: getHelpMessage(), |
|
|
options: { parse_mode: 'HTML' } |
|
|
}; |
|
|
}; |
|
|
|
|
|
const handleContactCommand = (ctx: BotContext) => { |
|
|
return { |
|
|
message: getContactMessage(), |
|
|
options: { parse_mode: 'HTML' } |
|
|
}; |
|
|
}; |
|
|
|
|
|
const handleBalanceCommand = async (ctx: BotContext) => { |
|
|
const telegramId = ctx.from?.id; |
|
|
|
|
|
if (!telegramId) { |
|
|
return { |
|
|
message: messageManager.getMessage('error_user_not_found'), |
|
|
options: {} |
|
|
}; |
|
|
} |
|
|
|
|
|
const user = await authService.getUserByTelegramId(telegramId,ctx); |
|
|
|
|
|
if (!user) { |
|
|
return { |
|
|
message: messageManager.getMessage('balance_auth_required'), |
|
|
options: getMainMenuKeyboard() |
|
|
}; |
|
|
} |
|
|
|
|
|
return { |
|
|
message: [ |
|
|
messageManager.getMessage('balance_title'), |
|
|
'', |
|
|
messageManager.getMessage('balance_current').replace('{balance}', (user.balance || 0).toString()), |
|
|
messageManager.getMessage('balance_last_updated').replace('{update_time}', new Date().toLocaleString('ar-SA')) |
|
|
].join('\n'), |
|
|
options: { |
|
|
...getLoggedInMenuKeyboard(), |
|
|
parse_mode: 'HTML' |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
const handleHistoryCommand = async (ctx: BotContext) => { |
|
|
const telegramId = ctx.from?.id; |
|
|
|
|
|
if (!telegramId) { |
|
|
return { |
|
|
message: messageManager.getMessage('error_user_not_found'), |
|
|
options: {} |
|
|
}; |
|
|
} |
|
|
|
|
|
if (!authService.isUserLoggedIn(telegramId,ctx)) { |
|
|
return { |
|
|
message: messageManager.getMessage('history_auth_required'), |
|
|
options: getMainMenuKeyboard() |
|
|
}; |
|
|
} |
|
|
|
|
|
return { |
|
|
message: [ |
|
|
messageManager.getMessage('history_title'), |
|
|
'', |
|
|
messageManager.getMessage('history_description') |
|
|
].join('\n'), |
|
|
options: { |
|
|
...getHistoryKeyboard(), |
|
|
parse_mode: 'HTML' |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const handleLanguageCommand = (ctx: BotContext) => { |
|
|
return handleLanguageSelection(ctx); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|